Jump to content
xisto Community
Sign in to follow this  
runeco

Errors With Php Setcookie() And Headers! Please Help! Having header errors when I try to set a cookie with PHP function setc

Recommended Posts

Hey guys, I'm having a problem setting cookies with PHP. I am trying to implement a script that will allow someone to log in and access members only pages on my website. One of the aspects is that I have to place a cookie on that persons computer. I know that you can use the PHP function setcookie() to place it on your computer. The problem is that wherever i have setcookie() in my PHP code I get an error:

 

"Warning: Cannot modify header information - headers already sent by (output started at /home/runeco/public_html/cookie.php:6) in /home/runeco/public_html/cookie.php on line 8"

 

i looked up some information about it and everyone says that it is problem that I have may have blank spaces after the closing php tag "?>". I througly checked my editor and could not find any blank spaces. So i decided to test something. I opened a new PHP document and just told it to set a cookie, here is the code:

 

<HTML><HEAD> <TITLE>Cookie Tester</TITLE></HEAD><BODY><?$message = 'hello';setcookie("IM_A_COOKIE", $message, time() + 3600);?></BODY></HTML>

I ran this code and I got the exact error i already stated above. This proves that the problem has to do with the setcookie() function. Is anybody else using setcookie()? If so, how do you get it to work, or are you experiencing the same problems? If there is no solution, I'm going to need to find a different way to make a login system for my site users. In that case, does anyone know how to make a login script with PHP that gets information from a MySQL database and supports members only pages. I would really much rather have a solution for my setcookie() problem, as I may need it for other parts of my website. Thanks in advance, and props to anyone that can solve my problem!

Share this post


Link to post
Share on other sites

In headers and COOKIE functions you cannot output anything before the header has been sent. Here your HTML tags are outputted before the header. Hence you are getting this error.
If you want to solve it try the following Code:

<?$message = 'hello';setcookie("IM_A_COOKIE", $message, time() + 3600);?><HTML><HEAD><TITLE>Cookie Tester</TITLE></HEAD><BODY></BODY></HTML>

Note the change of the PHP header or Setcookie() function is being called before the output has been started.
This will not gvew any error.

Hope this helps ad have a good day.

Share this post


Link to post
Share on other sites

dear runeco I agree electron when you use Setcookie() function Your all codes must goes at top of page and you can include file form another space , you must compile the file and Setcookie() form this file

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | 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.