Undefined index and unable to modify headers already sent
<p>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</p>

<blockquote>
  <p>Notice: Undefined index: prefcookie<br>
  Warning: Cannot modify header
  information - headers already sent by (</p>
</blockquote>

<p>Our code is</p>

<pre><code>&lt;?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&amp;b=2");
elseif($_COOKIE['prefcookie'] == "path-block")
    header("Location: http://".$_SERVER["HTTP_HOST"]."$url%2F&amp;b=2");
else
    header("Location: http://".$_SERVER["HTTP_HOST"]."$url%2F&amp;b=3");
?&gt;
</code></pre>

<p>EDIT</p>

<p>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 </p>

<p>The full code is</p>

<pre><code>&lt;?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-&gt;getBrowser() == Browser::BROWSER_GOOGLEBOT ) 
    {
      return;
    }
    elseif( $browser-&gt;getBrowser() == Browser::BROWSER_SLURP ) 
    {
       return;
    }
    elseif( $browser-&gt;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&amp;b=2");
        elseif($_COOKIE['prefcookie'] == "path-block")
            header("Location: http://".$_SERVER["HTTP_HOST"]."$url%2F&amp;b=2");
        else
            header("Location: http://".$_SERVER["HTTP_HOST"]."$url%2F&amp;b=3");} 
 }
?&gt;
</code></pre>

<p>EDIT</p>

<p>Set pref cookie</p>

<pre><code>&lt;?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&amp;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&amp;b=3");
    exit;
}
?&gt;
</code></pre>

<p>EDIT</p>

<p>how to find requested url</p>

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

<p>send to requested url</p>

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

<p>EDIT</p>

<pre><code>&lt;?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-&gt;getBrowser() == Browser::BROWSER_GOOGLEBOT ) 
{
    return;
}
elseif( $browser-&gt;getBrowser() == Browser::BROWSER_SLURP ) 
{
    return;
}
elseif( $browser-&gt;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&amp;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&amp;b=2");
                break;

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

<p>UPDATED</p>

<p>==========</p>

<pre><code>&lt;?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-&gt;getBrowser() == Browser::BROWSER_GOOGLEBOT ) 
    {
        return;
    }
    elseif( $browser-&gt;getBrowser() == Browser::BROWSER_SLURP ) 
    {
        return;
    }
    elseif( $browser-&gt;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"]."&amp;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"]."&amp;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"]."&amp;b=2");
    else
            header("Location: http://".$_SERVER["HTTP_HOST"]."/path/filter.php?u=http%3A%2F%2Fwww.path.org".$_SERVER["REQUEST_URI"]."&amp;b=2");
    } 
}
?&gt;
</code></pre>
