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


Undefined index and unable to modify headers already sent

我们在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");} 
 }
?>

编辑

设置首选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

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

发送到请求的url

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");
    } 
}
?>

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

// 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 block",所以我假设它与您的"path first"相同。如果不需要,您可以复制并通过"path first"的标头位置,然后将其粘贴到"path block"下。

此外,你的url构造似乎格式错误:

$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 中找到

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

此外,您看到的警告是因为您正试图在标头信息发送后更改它。您可以抑制这种情况,但最好在发送其余标头之前重定向用户。