Page 1 of 1

cookies not setting...

Posted: Fri Oct 11, 2013 7:44 pm
by xwhitchx
Hello everyone. I'm trying to get a simple cookie to work on my website to keep the user logged in. But I seem to be hitting a wall :banghead: Below is the code I'm trying to get work to see if I can get cookies to work. So far this does nothing. All other cookie from other websites work just fine, but mine just will not set. So I'm stuck, and would like to know if anyone has any ideas of what I can do to fix this and make things work.





Code: Select all

<?php




setcookie('test','cookie', time()+3600 , '/' , '.website.net' , 0 );

if(isset($_COOKIE['test'])){

echo 'worked';


}
else
{

echo 'not working';

}


?>

Re: cookies not setting...

Posted: Fri Oct 11, 2013 8:16 pm
by Celauran
From the manual:
Once the cookies have been set, they can be accessed on the next page load with the $_COOKIE or $HTTP_COOKIE_VARS arrays.

Re: cookies not setting...

Posted: Fri Oct 11, 2013 9:09 pm
by xwhitchx
That also would work if the page was refreshed right? If so I have refreshed the page and it still shows nothing. Also in google chrome I have looked at the resources/cookies and there is nothing there.

Re: cookies not setting...

Posted: Fri Oct 11, 2013 9:49 pm
by requinix
Make sure you have error_reporting set to -1 or E_ALL and display_errors turned on.

Then try the page with Chrome's "Network" tab open. Assuming you don't get any errors, the response for your page should include a Set-Cookie with your test=cookie value. If not then there was something wrong in the PHP and you really should have gotten an error about it.
Then check the resources tab to see if it did get set. If not then the browser rejected the cookie for whatever reason and it probably has to do with the cookie parameters (eg, domain, path, or expiration).

Re: cookies not setting...

Posted: Sat Oct 12, 2013 2:23 am
by xwhitchx
So I got this text. It does not like what I have going on on my index page. I can just have this redirect to a different page and then go to the index after the cookie is made. right?



[text]Warning: Cannot modify header information - headers already sent by (output started at /homepages/2/d260141947/htdocs/index.php:2) in /homepages/2/d260141947/htdocs/page/home.php on line 8[/text]

Re: cookies not setting...

Posted: Sat Oct 12, 2013 11:17 am
by xwhitchx
I can now get the cookie to set but only for one page after that the cookie is gone... What would make that happen. I have set the time to +3600 so it should still be there for an hour right?

Re: cookies not setting...

Posted: Sat Oct 12, 2013 12:40 pm
by xwhitchx
nvm I got it i beleave. but thanks for the help. I forgot about the error_report. I'm still very new to this =P