0

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");
    } 
}
?>
Shikiryu
  • 10,180
  • 8
  • 49
  • 75
James Kirkby
  • 1,716
  • 5
  • 24
  • 46

2 Answers2

0

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

Mike Purcell
  • 19,847
  • 10
  • 52
  • 89
  • You do not have enough information to decide what should be done if the cookie is not set; even if you did, chewing the OP's food for him without any explanation will serve nothing but rep gain. I won't go so far as to downvote, but this answer can really be improved. – Jon Mar 28 '12 at 22:23
  • @Jon: There was no other information in the OP to go off of. The title states: "Undifined index" which my answer clearly corrects. And I did add a snippet explaining the change I made: "Should probably add a check to make sure that the element exists in the array". Can you be more specific about what improvements should be made? – Mike Purcell Mar 28 '12 at 22:28
  • a) How do you know that if the cookie is not set you should take the first branch vs one of the other three? Sure the answer does not provide enough information, but asking wouldn't hurt and b) This code will make the error disappear (possibly doing the wrong thing in the process) and the OP will have learned nothing, which will only encourage them to continue the "CAN I HAZ TEH CODES" routine. – Jon Mar 28 '12 at 22:40
  • I've literally seen 100 answers with fewer than 25 characters, get upvoted and accepted. Isn't it up to the OP author to glean more information from the author of the solution if they have more questions? That's what I thought this comment section was for. – Mike Purcell Mar 28 '12 at 22:45
  • That surpreses the error but doesnt solve the problem, this checks the cookie for a preference and redirects the visitor to part of the site based on it, this code just brings the visitor back to the requested file – James Kirkby Mar 28 '12 at 22:50
  • @MikePurcell: I don't care for generalized statements on what other people upvote or accept because I can think independently. The product of this thought materialized in this instance as (perhaps a bit aggressive) criticism of the answer, complete with rationale. If you think you can use the input then good; if not, I 'm not interested in a stubborness contest. – Jon Mar 28 '12 at 22:55
  • I'm only stating precedence as established by the Q and A format which I have been exposed to here on SO. Input is appreciated. Not being stubborn, I have a right to justify my response, as you had a right to express yours. – Mike Purcell Mar 28 '12 at 23:00
  • @JamesKirkby: It's the exact behavior he had in place if cookie was set to 'path-all'. – Mike Purcell Mar 28 '12 at 23:00
  • @JamesKirkby: The root cause of this problem is that $_COOKIE['prefcookie'] is not being set, can you post that code as well? – Mike Purcell Mar 28 '12 at 23:05
  • @MikePurcell: Sure you have the right. My point is that you could have spent all this energy into improving the answer instead, and we would have all been better off for it. Cheers! – Jon Mar 28 '12 at 23:07
  • @Jon i hope you dont think im just "CAN I HAZ TEH CODES" im a student just looking for some help on a extracurricular project i started. Il post the set cookie code above, but i am fully expecting visitors not to have this cookie before hand in which case they will all need to be directed to a certain url – James Kirkby Mar 28 '12 at 23:12
  • @JamesKirkby: Don't worry about the $_COOKIE code then, can you post the URL you want the user to go to if $_COOKIE has not been set? – Mike Purcell Mar 28 '12 at 23:13
  • @MikePurcell The url they will be forwarded to is created on page, the script is basically checking to see if the visitor has opted into to use the sites cookies if not it is redirecting them to a filtered page that doesnt use cookies. Il post the line above – James Kirkby Mar 28 '12 at 23:19
  • @JamesKirkby: Just to clarify, the `header("Location: http://".$_SERVER["HTTP_HOST"]."$url%2F&b=2");` is the default location you wish redirect a user to, if the specified cookie hasn't been set? – Mike Purcell Mar 28 '12 at 23:24
  • @MikePurcell yes that is correct – James Kirkby Mar 28 '12 at 23:29
  • The funny thing is i have two versions of this "check sequence" im using one where it has a hard wired url to go to, the other that builds the requested url on the fly, the built on request one is the one causing this error – James Kirkby Mar 28 '12 at 23:34
  • @JamesKirkby: Just re-updated solution. – Mike Purcell Mar 28 '12 at 23:48
  • @MikePurcell i used your code (see above in last edit) While once again it stopped the errors it still doesnt redirect me to any page instead just blank, and with error reporting on doesnt show any errors, Also in the switch it doesn't provide a option for path-all cookie – James Kirkby Mar 29 '12 at 00:21
  • @JamesKirkby: Added 'path-all' to switch. I didn't add it before because you didn't have it as part of the `$_COOKIE['prefcookie']` check. If the page is white it means you have a PHP error, check the php error log. – Mike Purcell Mar 29 '12 at 00:51
  • The server doesnt produce error logs i turned on error logging in the php file but doesnt show any errors, – James Kirkby Mar 29 '12 at 03:30
  • When you make a php.ini config change you have to restart the httpd deamon. – Mike Purcell Mar 29 '12 at 04:04
  • @MikePurcell Shared server so no access unfortunately, i turned php error reporting on and still doesnt show anything Ive tried so many different options, and tested them back on our joomla install and they still work fine, just for some reason on this separate site it doesnt want to play ball, i added an update of the code i used, All the examples do the same thing just a different method and all work but just not on that server – James Kirkby Mar 29 '12 at 06:15
  • Please move extended discussions to [chat]. – Tim Post Mar 29 '12 at 12:36
0

You're seeing this notice because the index "prefcookie" does not exist. In all likelihood, on your Joomla install something is setting $_COOKIE['prefcookie'], but your install not on Joomla doesn't have this index.

Furthermore, the warning you are seeing is because you are trying to change the header information after it's already been sent. You can suppress this, however it's probably better to just redirect the user prior to sending the rest of the headers.

Julio
  • 2,261
  • 4
  • 30
  • 56