未定义的索引,无法修改已发送的标头

时间:2012-03-28 22:18:40

标签: php

我们正在使用脚本来管理我们的joomla安装上的cookie。它工作正常。当我们在另一个不是joomla的网站上安装它时,我们正面临这些错误

  

注意:未定义的索引:prefcookie
  警告:无法修改标头   信息 - 已经发送的标题(

我们的代码是

<?php
error_reporting(-1);

$url="/path/filter.php?u=http%3A%2F%2F".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
if ($_COOKIE['prefcookie'] == "path-all")
{
    return;
}
elseif ($_COOKIE['prefcookie'] == "path-first")
    header("Location: http://".$_SERVER["HTTP_HOST"]."$url%2F&b=2");
elseif($_COOKIE['prefcookie'] == "path-block")
    header("Location: http://".$_SERVER["HTTP_HOST"]."$url%2F&b=2");
else
    header("Location: http://".$_SERVER["HTTP_HOST"]."$url%2F&b=3");
?>

修改

这可以解决错误,但不能解决问题,我们的想法是检查访问者的偏好,并根据它将访问者重定向到网站的一部分,此修复只会将访问者带回所请求的文件

完整的代码是

<?php
require_once('path/geoip/geoplugin.class.php');
$geoPlugin_array = unserialize( file_get_contents('http://www.geoplugin.net/php.gp?ip=' . $_SERVER['REMOTE_ADDR']) );
if ( $geoPlugin_array['geoplugin_continentCode'] == 'EU' )
{
    require_once("path/includes/browser.php"); 
    $browser = new Browser();
    if( $browser->getBrowser() == Browser::BROWSER_GOOGLEBOT ) 
    {
      return;
    }
    elseif( $browser->getBrowser() == Browser::BROWSER_SLURP ) 
    {
       return;
    }
    elseif( $browser->getBrowser() == Browser::BROWSER_MSNBOT ) 
    {
        return;
    }
    else
    {
        $url="/path/filter.php?u=http%3A%2F%2F".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
        // This will avoid undefined index
        if ((!isset($_COOKIE['prefcookie'])) || ($_COOKIE['prefcookie'] == "path-block")) {
                return;
        }
        elseif($_COOKIE['prefcookie'] == "path-first")
            header("Location: http://".$_SERVER["HTTP_HOST"]."$url%2F&b=2");
        elseif($_COOKIE['prefcookie'] == "path-block")
            header("Location: http://".$_SERVER["HTTP_HOST"]."$url%2F&b=2");
        else
            header("Location: http://".$_SERVER["HTTP_HOST"]."$url%2F&b=3");} 
 }
?>

修改

设置pref cookie

<?php 
if($_GET['optin'] == "all")
{
    setcookie("prefcookie", "path-all", time()+60*60*24*30);
    header("Location: http://".$_SERVER["HTTP_HOST"]);
}
elseif($_GET['optin'] == "first")
{
    setcookie("prefcookie", "path-first", time()+60*60*24*30);
    header("Location: http://".$_SERVER["HTTP_HOST"]."/path/filter.php?u=http%3A%2F%2Fwww.fatcowmedia.com%2F&b=2");
}
elseif($_GET['optin'] == "block")
{
    setcookie("prefcookie", "path-block", time()+60*60*24*30);
    header("Location: http://".$_SERVER["HTTP_HOST"]."/path/filter.php?u=http%3A%2F%2Fwww.fatcowmedia.com%2F&b=3");
    exit;
}
?>

修改

如何查找请求的网址

$url="/path/filter.php?u=http%3A%2F%2F".$_SERVER["HTTP_HOST"].$_SERVER['REQUEST_URI'];

发送到请求的网址

header("Location: http://".$_SERVER["HTTP_HOST"]."$url%2F&b=2");

修改

<?php
error_reporting(E_ALL);
$url="/path/filter.php?u=http%3A%2F%2F".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
require_once('path/geoip/geoplugin.class.php');
$geoPlugin_array = unserialize( file_get_contents('http://www.geoplugin.net/php.gp?ip=' . $_SERVER['REMOTE_ADDR']) );
if ( $geoPlugin_array['geoplugin_continentCode'] == 'EU' )
{
    require_once("path/includes/browser.php"); 
    $browser = new Browser();
if( $browser->getBrowser() == Browser::BROWSER_GOOGLEBOT ) 
{
    return;
}
elseif( $browser->getBrowser() == Browser::BROWSER_SLURP ) 
{
    return;
}
elseif( $browser->getBrowser() == Browser::BROWSER_MSNBOT ) 
{
    return;
}
else
{
    // If expected cookie isn't set yet, send em to landing page
    if (!isset($_COOKIE['prefcookie'])) {
            header("Location: http://".$_SERVER["HTTP_HOST"]."$url%2F&b=2");
    }

    // If we made it this far, we have our expected cookie, we can implement a switch
    switch ($_COOKIE['prefcookie']) {

            case 'path-block':
            case 'path-first':
                header("Location: http://".$_SERVER["HTTP_HOST"]."$url%2F&b=2");
                break;

            default:
                header("Location: http://".$_SERVER["HTTP_HOST"]."$url%2F&b=3");
                break;
    }
} 
}
?>

已更新

==========

<?php
require_once('path/geoip/geoplugin.class.php');
$geoPlugin_array = unserialize( file_get_contents('http://www.geoplugin.net/php.gp?ip=' . $_SERVER['REMOTE_ADDR']) );
if ( $geoPlugin_array['geoplugin_continentCode'] == 'EU' )
{
    require_once("path/includes/browser.php"); 
    $browser = new Browser();
    if( $browser->getBrowser() == Browser::BROWSER_GOOGLEBOT ) 
    {
        return;
    }
    elseif( $browser->getBrowser() == Browser::BROWSER_SLURP ) 
    {
        return;
    }
    elseif( $browser->getBrowser() == Browser::BROWSER_MSNBOT ) 
    {
        return;
    }
    else
    {
        if (!isset($_COOKIE['prefcookie'])) {
            header("Location: http://".$_SERVER["HTTP_HOST"]."/path/filter.php?u=http%3A%2F%2Fwww.path.org".$_SERVER["REQUEST_URI"]."&b=2");
        }
        elseif($_COOKIE['prefcookie'] == "path-all")
        {
            return;
        }
        elseif($_COOKIE['prefcookie'] == "path-first")
            header("Location: http://".$_SERVER["HTTP_HOST"]."/path/filter.php?u=http%3A%2F%2Fwww.path.org".$_SERVER["REQUEST_URI"]."&b=2");
    elseif($_COOKIE['prefcookie'] == "path-block")
        header("Location: http://".$_SERVER["HTTP_HOST"]."/path/filter.php?u=http%3A%2F%2Fwww.path.org".$_SERVER["REQUEST_URI"]."&b=2");
    else
            header("Location: http://".$_SERVER["HTTP_HOST"]."/path/filter.php?u=http%3A%2F%2Fwww.path.org".$_SERVER["REQUEST_URI"]."&b=2");
    } 
}
?>

2 个答案:

答案 0 :(得分:0)

应该添加一个检查以确保该元素存在于数组中:

// This will avoid undefined index
if ((!isset($_COOKIE['prefcookie'])) || ($_COOKIE['prefcookie'] == "path-all")) {
    return;
}

- 更新 -

根据您的更新和评论,以下代码应该有效:

// If expected cookie isn't set yet, send em to landing page
if (!isset($_COOKIE['prefcookie'])) {
    header("Location: http://".$_SERVER["HTTP_HOST"]."$url%2F&b=2");
}

// If we made it this far, we have our expected cookie, we can implement a switch
switch ($_COOKIE['prefcookie']) {

    case 'path-block':
    case 'path-first':
        header("Location: http://".$_SERVER["HTTP_HOST"]."$url%2F&b=2");
        break;

    case 'path-all':
        header("Location: http://".$_SERVER["HTTP_HOST"]);
        break;

    default:
        header("Location: http://".$_SERVER["HTTP_HOST"]."$url%2F&b=3");
        break;
}

对于记录,在您的原始代码中,您有两次“路径阻止”,因此我认为它与“路径优先”相同,就像您拥有它一样。如果不需要,您可以复制并越过“path-first”的标题位置,并将其粘贴到“path-block”下。

此外,您的网址构造似乎格式错误:

$url="/path/filter.php?u=http%3A%2F%2F".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];

// If you decode the following construct it looks like (I decoded entities for clarity)
// http://www.example.com/path/filter.php?u=http://www.example.com/path/filter.php/&b=2
// %2F decodes to a slash "/", so the /&b=2 appears malformed, it should be /?b=2
header("Location: http://".$_SERVER["HTTP_HOST"]."$url%2F&b=2");

可以在meyerweb.com/eric/tools/dencoder

找到有用的编码/解码工具

答案 1 :(得分:0)

您看到此通知是因为索引“prefcookie”不存在。很可能,在你的Joomla安装上有些东西是设置$ _COOKIE ['prefcookie'],但是你在Joomla上的安装没有这个索引。

此外,您看到的警告是因为您尝试在已经发送之后更改标头信息。您可以取消此操作,但在发送其余标题之前重定向用户可能更好。