Title: Setting and Retrieving Cookies in PHP
Description: Configuration Script Question.
Lou - June 30, 2007 05:25 PM (GMT)
I am developing test forms using PHP. So far, I have been able to develop forms and use bindings with variables successsfully in forms . I am using the Get Method, deliberately, to set cookie variables in my test forms. I am having problems using and setting cookie variables. I am using the get method, url.firstname, url.lastname binding and variable settings.
My question:
Are there any special settings in PHP or Apache that need to be set, such as those in Dynamic extensions in PHP. ini. Below is the code that I am using?
setcookie('firstName', $_Get['firstName'], time() + (60*60*24));
setcookie('lastName', $_Get['lastName'], time() + (60*60*24));
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>
<body>
<p>Thank you, <?php echo $_GET['firstName']; ?> <?php echo $_GET['lastName']; ?>, for filling out the form.</p>
<p>Check <a href="test_form_processor_cookies.php">cookie</a>. </p>
</body>
</html>
Link page code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>
<body>
<p>Hi,<?php echo $_COOKIE['firstName']; ?><?php echo $_COOKIE['lastName']; ?>!</p>
</body>
</html>
patheticcockroach - July 1, 2007 05:20 PM (GMT)
Do you get any error message ?
The first thing I would try is replace
| CODE |
| setcookie('firstName', $_Get['firstName'], time() + (60*60*24)); |
with
| CODE |
| setcookie('firstName', $_GET['firstName'], time() + (60*60*24)); |
(and same for last name)
Lou - July 4, 2007 10:33 PM (GMT)
I checked all of the code in the three pages involved. The first page, test_form, is a form where the information is entered:
| CODE |
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <title>Untitled Document</title> </head>
<body> <form id="frm_name" name="frm_name" method="get" action="test_form_processor.php"> <label>First Name <input name="firstName" type="text" id="firstName" /> </label> <p> <label>Last Name <input name="lastName" type="text" id="lastName" /> </label> </p> <p> <p> <input type="submit" name="Submit" value="Submit" /> </p> </form> </body> </html>
|
The code in test_form.php works well and the varaibles, firstName and lastname are passed to the second page: test_form_processor.php:
| CODE |
<?php setcookie('firstName', $_Get['firstName'], time() + (60*60*24)); setcookie('lastName', $_Get['lastName'], time() + (60*60*24)); ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <title>Untitled Document</title> </head>
<body> <p>Thank you, <?php echo $_GET['firstName']; ?> <?php echo $_GET['lastName']; ?>, for filling out my form.</p> <p>Check <a href="test_form_processor_cookies.php">cookie</a>. </p> </body> </html>
|
This form is working properly as the first and last name entered into the first form are correctly passed to the second page as "url.name" variables, as indicated by the first and last names placed in form one being entered into the sentence on page two.
The problem is that the cookie variables, firstname and lastname are not being set and cached for transfer to page three:
| CODE |
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <title>Untitled Document</title> </head>
<body> <p>Hi, <?php echo $_COOKIE['firstName']; ?><?php echo $_COOKIE['lastName']; ?>!</p> </body> </html>
|
When the link on page two is clicked, page three, test_form_ processor_cookies.php is accessed, but the cookie variables, firstName and lastName are not being set, cached and transferred to the third page.
I checked the cookie variables in the php code on the third page and they are properly set and are transferring cookies to my hard drive. I can locate the cookies when I check cookies in the browsers. However, the cookie variables are not being transferred back to page three. It is probably something simple that I am overlooking. It is not the browser settings because the cookies are being transferred to my hard drive.
Thanks for your response. I will work on this occasionally, until I resolve the issue. I need to test that cookies work in my testing environment and currently, I cannot prove that cookies are properly working.
Thanks
patheticcockroach - July 5, 2007 05:47 AM (GMT)
I'm sorry I very rarely use cookies in PHP, most often I use sessions. However PHP is case sensitive, so I think that, as I posted, you should try and replace $_Get with $_GET. Otherwise, maybe you could work on the w3schools example here :
http://www.w3schools.com/php/php_cookies.asp (first try the example as it is to check that all is fine with your server and browser, then modify to your needs).
Lou - July 6, 2007 03:06 AM (GMT)
I GET it now. I speed read and often miss very fine detail. I will try it. Also, I will remember that PHP is totally case sensitive.
Thanks
Lou - July 6, 2007 03:12 AM (GMT)
Worked like a charm. Also, the php code in the Set Cookie Header was the wrong color. The editor was giving me a hint all along. Thanks again!
patheticcockroach - July 10, 2007 11:13 AM (GMT)