Hello, everyone.
I’m teaching myself PHP from “PHP in a Nutshell” by Paul Hudson (O’Reilly Press), but I do not see the issue that I am having mentioned anywhere in the book.
Just playing around with cookies, getting to know how to set, read, alter, delete, etc. I have a page where I’m setting a cookie as the VERY first thing (immediately after the <?php opening tag.) A few lines down (okay, 71 lines down) I’m attempting to read this cookie. Just read it.. nothing more.
The problem is that the first time the page loads, it says that there is no cookie. If I refresh or reload the page, it works with no problem.. displays the content just fine.
What is causing the cookie to not be there on the primary page load? I’ll include code I have.
Here is the first few lines of the page:
[code]
<?php
$thisValue = “This is a test. It is only a test.”;
setcookie(“thisCookie”, $thisValue);
?>
<html> // blah blah blah, you know the rest..
Here is where I’m trying to read the cookie (note there is a conditional – IF NO COOKIE, display that there is no cookie; if there IS a cookie, display contents.):
[code]
<?php
if(!$_COOKIE[“thisCookie”]) {
echo “There is no cookie set!!<br />”;
}
else {
echo “”” . $_COOKIE[“thisCookie”] . “” is the value of the set cookie.<br />”;
}
?>
This gives an error message:
[quote]
Notice: Undefined index: thisCookie in C:Program FilesApache Software FoundationApache2.2htdocsindex.php on line 72 There is no cookie set!!
Am I missing something? Should I change a setting on the Apache server? (I’m a neophyte with Apache administration, too.)
Thanks,
^_^