jdock1 Posted July 28, 2010 Share Posted July 28, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/209157-header-error-has-something-to-do-with-setcookie-cant-figure-it-out/ Share on other sites More sharing options...
Pikachu2000 Posted July 28, 2010 Share Posted July 28, 2010 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? Quote Link to comment https://forums.phpfreaks.com/topic/209157-header-error-has-something-to-do-with-setcookie-cant-figure-it-out/#findComment-1092329 Share on other sites More sharing options...
PFMaBiSmAd Posted July 28, 2010 Share Posted July 28, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/209157-header-error-has-something-to-do-with-setcookie-cant-figure-it-out/#findComment-1092338 Share on other sites More sharing options...
jdock1 Posted July 29, 2010 Author Share Posted July 29, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/209157-header-error-has-something-to-do-with-setcookie-cant-figure-it-out/#findComment-1092826 Share on other sites More sharing options...
Pikachu2000 Posted July 29, 2010 Share Posted July 29, 2010 You can't output anything to the browser before something that causes headers to be sent, not even whitespace. Quote Link to comment https://forums.phpfreaks.com/topic/209157-header-error-has-something-to-do-with-setcookie-cant-figure-it-out/#findComment-1092830 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.