Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

PHP not setting cookies

Status
Not open for further replies.

MJAZ

Programmer
Aug 1, 2006
73
US
Hello! I have noticed that many of my scripts intermittently break because PHP is not setting cookies. It is only for the PHP scripts that I use for the website I am working on where this happens.

Here is a test script where I test for cookies:

Code:
<?php
setcookie("foo","bar",time() + 86400);
echo $_COOKIE["foo"];
?>

Each time I run this script, I get a "Notice: Undefined index: foo on line 3" error. What could I be doing wrong?
 
cookies are set in the headers sent to the browser. and retrieved in the headers sent by the browser. their values will NEVER be available to the script that sets them.
 
I know that, but even after I refresh that page the cookie does not show up. I opened my browser's cookie dalog box and no cookie was found. Also, no session cookies are being set.

Matt
 
there is nothing wrong in your cookie setting code provided no content has output to the browser at the time cookies were sent.

i cannot see any session code so can't comment on that.

have you ensured that you have set errors to be displayed and that error reporting is set to E_ALL or E_STRICT for debugging? these directives are in your php.ini file. make sure to edit the right one (as shown in phpinfo()) and to restart your webserver after any amendment.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top