Jump to content

Header error... has something to do with setcookie cant figure it out


jdock1

Recommended Posts

I hate header errors... I can never figure them out, im getting this error;

 

Warning: Cannot modify header information - headers already sent by (output started at /home/damnpeti/public_html/restrict2.php:6) in /home/damnpeti/public_html/restrict2.php on line 62

 

Code:

<?php
$testDB = mysql_connect('localhost', $db_user, $db_pwd);
mysql_select_db ($db_name);
if (!$testDB) {
    die('Could not connect: ' . mysql_error());
}

$surfer_ip = $_SERVER["REMOTE_ADDR"];

$str_sql = "select * from ".$db_table." where ipaddress='".$surfer_ip."'";
$result = mysql_query($str_sql);
if ($row = mysql_fetch_assoc($result)) {
    $blocked_time = strtotime($row['blocked_time']);
    if($blocked_time != 0) 
    {
        $current_time = time();
        if($current_time - $blocked_time > 3600*24) //24 hours past
        {
            $str_sql = "delete from ".$db_table." where ipaddress='".$surfer_ip."'"; 
            mysql_query($str_sql); 
            $str_sql = "insert into ".$db_table." (ipaddress, surf_index) values('".$surfer_ip."', 1)";
            mysql_query($str_sql);   
        }
        else
        {
            die ("<center><div class='errors'>You have accessed this page too many times. To regain access, purchase a license or wait 24 hours.</div></center>");
        }
    }  
    else
    {
        if($row['surf_index'] < 2)
        {
            $str_sql = "update ".$db_table." set surf_index=surf_index+1 where ipaddress='".$surfer_ip."'";
            mysql_query($str_sql);
        }
        else
        {
            $str_sql = "update ".$db_table." set blocked_time='".date ("Y-m-d H:i:s")."' where ipaddress='".$surfer_ip."'";
            mysql_query($str_sql); 
            die ("<center><div class='errors'>You have accessed this page too many times. To regain access, purchase a license or wait 24 hours.</div></center>");
        }
        
    }
}
else
{
    $str_sql = "insert into ".$db_table." (ipaddress, surf_index) values('".$surfer_ip."', 1)";
    mysql_query($str_sql);
}
if(!isset($_COOKIE['surf_no']))
    setCookie('surf_no', '1'); 
else
    setCookie('surf_no', $_COOKIE['surf_no']+1);

if ($_COOKIE['surf_no'] > 2)
    die("<center><div class='errors'>You have accessed this page too many times. To regain access, purchase a license or wait 24 hours.</div></center>");
include 'http://damnitpetitions.com/cut/index3.php';

?>

 

Line 62 is

if ($_COOKIE['surf_no'] > 2)

 

I know it has something to do with the cookie... but Idk? I need the include there. If there include isnt where its at, the script is worthless.

Link to comment
Share on other sites

The include() is after the setcookie() so that isn't relevant. I just pasted the code and ran it locally, and it set the cookie just fine. What are you getting besides the header error? Any other errors at all, or any other type of output to the browser?

Link to comment
Share on other sites

Based on where the error message states the output is started at -

 

output started at /home/damnpeti/public_html/restrict2.php:6 (line 6)

 

You likely have 6 new-lines before the first opening php tag <?php or you have some html before the first opening tag and you didn't post it with the rest of the code in the file.

Link to comment
Share on other sites

Ah I got it. Thanks for your replies. There was HTML before the php tags. Gets me everytime. I still dont understand it, most of the time I can get away with that but for some reason when I use cookies or includes it dont let me do that.

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.