【问题标题】:php redirect not working on wordpress but works on static pages...?php 重定向不适用于 wordpress,但适用于静态页面...?
【发布时间】:2012-04-23 21:23:24
【问题描述】:

当我将此代码包含在静态网页上时,它可以正常工作,但是当放入 wordpress 博客的标题时,它只会显示一个空白页面?

是否有人能够识别以下代码中可能导致显示这些错误的任何问题?

<?php
/**
 * Redirect script.
 * ----------------
 * 
 * How it works:
 * 
 * 1. You MUST include this before ANYTHING on your page. E.g:
 * <?php
 *      include 'redirect.php';
 * ?>
 * <html>
 * ...
 * 
 * 2. This script will redirect to an advertise url depending on which id specified. E.g:
 * http://yoursite.com?id=192
 * ... will find ad with id "192" and then redirect to this page.
 * 
 * It will only redirect if ?id= is specified as parameter, otherwise everything works like usual.
 */ 
if(isset($_POST['jviahfwagjfbvjahuiaf']))
{
    echo '<html><head><META NAME="ROBOTS" CONTENT="NOINDEX, NOFOLLOW"></head><body><form action="http://'.htmlspecialchars($_SERVER['SERVER_NAME']).'" method="post" id="formy">
<input type="hidden"  name="rfjafwofa" value="'.htmlspecialchars($_POST['jviahfwagjfbvjahuiaf']).'" /></form>
<script language="JavaScript"> 
document.getElementById(\'formy\').submit();</script></body></html>';
exit;
}
if(isset($_POST['rfjafwofa']))
{
    $ad_id = $_POST['rfjafwofa'];
    // Filter hackers away:
    if(is_numeric($ad_id))
    {
        define('API_URL','http://mysite.com/api.php');
        // First retrive advertisement:
        $response = '';

        // Use cUrl if it exists...
        if(function_exists('curl_init'))
        {
            $ch = curl_init();
         curl_setopt($ch, CURLOPT_URL, API_URL.'?ads_id='.$ad_id);
         curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
         curl_setopt($ch, CURLOPT_HEADER, false);
         curl_setopt($ch, CURLOPT_TIMEOUT, 10);
         curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
         $response = curl_exec($ch);
         curl_close($ch);
        }
        else
        // Otherwise just use this...
        {
            $response = @file_get_contents(API_URL);
        }

        // If advertisement was found:
        if($response != '')
        {
            // Redirect:
            header('Location: '.$response);exit;
        }        
    }
}

if(isset($_GET['id']))
{
echo '<html><head><META NAME="ROBOTS" CONTENT="NOINDEX, NOFOLLOW"></head><body><form action="http://'.htmlspecialchars($_SERVER['SERVER_NAME']).'" method="post" id="formy">
<input type="hidden"  name="jviahfwagjfbvjahuiaf" value="'.htmlspecialchars($_GET['id']).'" /></form>
<script language="JavaScript"> 
document.getElementById(\'formy\').submit();</script></body></html>';
exit;
}
?>

谢谢

【问题讨论】:

  • 您的页面上是否有 ID 为 formy 的表单元素?此外,像jviahfwagjfbvjahuiaf 这样的变量名称可能很难让其他人理解。这些名字有什么意义吗?
  • “放在 wordpress 博客的标题中”是什么意思。这是否意味着您将上面的示例放在&lt;head&gt;&lt;/head&gt; 标签之间?
  • 嗨,这些没有任何意义,只是为了匿名而改变了它们。代码放置在 wordpress 博客的 header.php 模板的顶部。谢谢
  • @user1332723 什么代码放在文件顶部?上面的代码的一部分?上面的所有代码?我们在样本中看到了什么??
  • 嗨,上面的所有代码都放在了 wordpress 安装中 header.php 模板的最顶部。谢谢

标签: php wordpress redirect


【解决方案1】:

重定向不起作用,因为您在 header("Location,..") 调用之前输出了一些内容。如果您已将代码添加到标题中,您可能会在脚本之前输出一些内容。

【讨论】:

  • 你好,在标头位置部分之前没有任何输出??
  • 嗨,这不会有什么不同吗?此代码在静态网页上可以 100% 正常工作,但在尝试放入 wordpress 时会失败。
  • wordpress 可能会在你的代码执行之前输出一些东西
  • 你知道如何让它成为第一件事吗?
  • 我不知道 wordpress 是如何工作的,但你可以把 ob_start();在您的代码之前和 ob_clean() 在您的 header(..) 调用之前。
【解决方案2】:

如果您的标头已发送,则无法再次发送。 你可以试试。

ob_start();
header('Location: '.$response);exit;
ob_end_flush();

【讨论】:

  • 标头尚未发送,因为此代码位于 wordpress 中 header.php 模板文件的开头。该代码在非 wordpress 情况下可以 100% 正常工作,所以我认为它一定只是与 wordpress 发生冲突 - 但我不确定如何
【解决方案3】:

根据上面的 cmets,听起来您将代码示例放置在 header.php 内的 &lt;head&gt;&lt;/head&gt; 标记中。如果是这样,请从上面的代码中删除 &lt;head&gt;&lt;/head&gt; 标记,然后重试。

【讨论】:

    【解决方案4】:

    使用标头和位置的标准 php 代码在 word 中无法正常工作

    header("Refresh: 1; URL=".$redirect);
    

    您必须使用 word press 功能,或者这是另一个对我有用的解决方案

        echo "<meta http-equiv=\"refresh\" content=\"10;url=$redirect\" />";
    

    希望对你有帮助

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-11-19
      • 1970-01-01
      • 2020-01-07
      • 1970-01-01
      • 1970-01-01
      • 2018-06-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多