drwo2014 2012-03-28 22:18
浏览 39
已采纳

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

This question already has an answer here:

We're using a script to manage cookies on our joomla install. It works fine. When we install it on another site that isn't joomla we are facing these errors

Notice: Undefined index: prefcookie
Warning: Cannot modify header information - headers already sent by (

Our code is

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

EDIT

That surpreses the error but doesn't solve the problem, the idea is that this checks the visitor for a preference and redirects the visitor to part of the site based on it, this fix just brings the visitor back to the requested file

The full code is

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

EDIT

Set 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;
}
?>

EDIT

how to find requested url

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

send to requested url

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

EDIT

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

UPDATED

==========

<?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");
    } 
}
?>
</div>
  • 写回答

2条回答 默认 最新

  • dongtang5776 2012-03-28 22:21
    关注

    Should probably add a check to make sure that the element exists in the array:

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

    -- Update --

    According to your updates and comments, the following code should work:

    // 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;
    }
    

    For the record, in your original code you had 'path-block' twice, so I assumed it to be the same as 'path-first' as you have it. If this is not desired, you can copy and past the header location for 'path-first' and paste it under 'path-block'.

    Also, it appears your url construct is malformed:

    $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");
    

    A useful encode/decode tool can be found @ meyerweb.com/eric/tools/dencoder

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥30 python代码,帮调试
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条