Page 1 of 1

Can't read from cookie until second load?

Posted: Wed Apr 25, 2007 7:45 pm
by JAB Creations
I'm using GET to set cookies such as index.php?audio=1&bandwidth=2. The header file sets a cookie per each GET in the url. However the page does not read the cookie initially and requires a reload to correctly display the cookie's value.

My files are as so...
1.) header.php

2.) header-cookies.php (included at bottom of header.php and used to process GET in url)

3.) index.php (this loads last and is the page initially requested)

An example of setting a cookie for index.php?audio=1 would be...

Code: Select all

if ($_GET['audio'] == "0") {setcookie('audio','0',time()+2592000,'/');}
else if ($_GET['audio'] == "1") {setcookie('audio','1',time()+2592000,'/');}
What am I be doing wrong?

Posted: Wed Apr 25, 2007 8:19 pm
by John Cartwright
You aren't doing anything wrong, that's how cookies work.

Posted: Wed Apr 25, 2007 8:20 pm
by JAB Creations
Perhaps I should detect if a GET is in the URL first and if not then go by the cookie?

Posted: Wed Apr 25, 2007 8:55 pm
by John Cartwright
You should probably be using sessions in this case for each page request, and set a cookie to remember their last request if they end up closing the browser. I could be off, as I have no idea what your trying to accomplish.

Posted: Thu Apr 26, 2007 10:05 am
by pickle
PHP Docs wrote:Cookies will not become visible until the next loading of a page that the cookie should be visible for.

Posted: Thu Apr 26, 2007 11:29 am
by superdezign
I never knew that. :-p
Another reason I like sessions.

Sessions 1, Cookies 0. :-D