Page 1 of 1

I can create cookies but not call them - why??

Posted: Fri Dec 30, 2005 3:03 am
by dazang
Ok, here is my dilema. It is driving me absolutely batty. I am creating my own cms with PHP / MySQL and I am getting the hang of it. I am using cookies to pass variables. I can set the cookies, and verify it in my browser that they are set just fine.

Firstly, my cms is setup with a config file which is always included first with index.php & then the theme file is included, which includes the HTML. So since I need to set the cookies before the HTML, I put the default cookies set in the config file. They use if statements:

Code: Select all

// COOKIE HEADERS
session_start();
if ( $_COOKIE['trans'] == null ) 		setcookie("trans", "LITV", 0, "/");
if ( $_COOKIE['book'] == null ) 		setcookie("book", "Genesis", 0, "/");
if ( $_COOKIE['chapter'] == null ) 	setcookie("chapter", "1", 0, "/");
I also have a form that passes post data and is transferred over to the cookies. I verify that this changes the cookie properly.

However, when I set any given page to call the cookie information, it won't work and I am going crazy.

Here is an example call:

Code: Select all

$query				=		"SELECT $fields FROM " . $_COOKIE["trans"] . "$queryextra";
This is an example variable I set for a mysql query. The query works perfectly if I replace the cookie calls with a static string.

What gives?

Thanks in advance!!

:D

Posted: Fri Dec 30, 2005 3:41 am
by Jeroen Oosterlaar
Do you access the cookies within the same request in which you set them? If so, that won't work if I remember correctly.

no

Posted: Fri Dec 30, 2005 3:50 am
by dazang
No, I call them in a separate PHP file after this one has already set it.

Re: no

Posted: Fri Dec 30, 2005 3:55 am
by Jeroen Oosterlaar
dazang wrote:No, I call them in a separate PHP file after this one has already set it.
Okay, but what I meant was: do you invoke both calls within the same request? For example: visitor V requests your page which includes the configuration file. In that case each call is in a separate file, but within the same request. Setting a cookie in the included file and trying to read it in the including file will not work within that a single request. If you want to get around this obstacle, I suggest you set the cookies in the cookie-array manually as well:

For example:

Code: Select all

if($_COOKIE["book"] == null)
{
    setcookie("book", "Genesis", 0, "/");
    $_COOKIE["book"] = "Genesis";
}
(This is to make sure the first script that includes the configuration file for the first time also has access to the cookie values.)

Posted: Fri Dec 30, 2005 4:02 am
by Jeroen Oosterlaar
As a reference see:
http://nl3.php.net/manual/en/function.setcookie.php

Below table 1 it is stated:
Once the cookies have been set, they can be accessed on the next page load with the $_COOKIE or...

Posted: Fri Dec 30, 2005 4:38 am
by dazang
Thanks! I had that in mind the whole time, but for some reason I thought that it was in a different process if I refreshed the page. But what you suggested worked, for part of my problem. That worked on setting the initial value to the cookie & enabled php files to call the cookie.

However, now I am perplexed on my form posting new info to the cookie. In file that processes the form data would I do:

Code: Select all

$_COOKIE["trans"]	=	$_POST['trans'];
or setcookie again?

Code: Select all

setcookie("trans", $_POST["trans"], time()+3600, "/");

Posted: Fri Dec 30, 2005 4:40 am
by dazang
I guess I should let you know I tried both of those & the first one doesn't change anything & the 2nd one creates another cookie with the same name & value.

Posted: Fri Dec 30, 2005 4:58 am
by dazang
Well, I kind of found the answer to my last question. I know I have to redo the setcookie function, however I am still seeing strange output. The cookie does update, and I can see it. Also, it is no longer making another cookie. So that is good. However the output always remains the same. The data should be updating according to the new value of the cookie, and its not.

:?

Posted: Fri Dec 30, 2005 5:18 am
by Jeroen Oosterlaar
Does the value of the POST-variable change? Maybe you should do an echo of $_POST["trans"] after you have set the new cookie and compare that value to the new cookie value, to make sure the value really changes. If not, the problem is not located at the cookie setting.