Forum Moderators: coopster

Message Too Old, No Replies

Page is not updating the latest value of the cookie?

Using GET to request a page, set the cookie, no value till second load?

         

JAB Creations

12:38 am on Apr 26, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



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...

if ($_GET['audio'] == "0") {setcookie('audio','0',time()+2592000,'/');}
else if ($_GET['audio'] == "1") {setcookie('audio','1',time()+2592000,'/');}

What might I be doing wrong?

- John

dreamcatcher

6:43 am on Apr 26, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi John,

From the PHP website:

Cookies will not become visible until the next loading of a page that the cookie should be visible for. To test if a cookie was successfully set, check for the cookie on a next loading page before the cookie expires. Expire time is set via the expire parameter. A nice way to debug the existence of cookies is by simply calling print_r($_COOKIE);.

Try reloading the page when you set the cookie.

dc

capulet_x

7:01 am on Apr 26, 2007 (gmt 0)

10+ Year Member



You could try echoing javascript straight to the browser to make the cookie. Kind've like:

if something{
echo "<script> document.cookie = 'mycookie=mycookievalue; path=/'</script>";
}

But still, you may have to wait for the PHP to finish...I'm not sure.

JAB Creations

7:09 am on Apr 26, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thanks though I've decided to take a slight different route. Some example code. ;)

- John

<?php
if ($_GET['powerkeys'] == "1") {echo $powerkeys;}
else if ($_COOKIE["powerkeys"]=="1") {echo $powerkeys;}
?>