cookies not setting...

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
xwhitchx
Forum Commoner
Posts: 38
Joined: Mon Jun 21, 2010 4:30 pm

cookies not setting...

Post 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';

}


?>
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: cookies not setting...

Post 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.
xwhitchx
Forum Commoner
Posts: 38
Joined: Mon Jun 21, 2010 4:30 pm

Re: cookies not setting...

Post 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.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: cookies not setting...

Post 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).
xwhitchx
Forum Commoner
Posts: 38
Joined: Mon Jun 21, 2010 4:30 pm

Re: cookies not setting...

Post 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]
xwhitchx
Forum Commoner
Posts: 38
Joined: Mon Jun 21, 2010 4:30 pm

Re: cookies not setting...

Post 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?
xwhitchx
Forum Commoner
Posts: 38
Joined: Mon Jun 21, 2010 4:30 pm

Re: cookies not setting...

Post 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
Post Reply