/    Sign up×
Community /Pin to ProfileBookmark

PHP n00b has curious cookie issue..

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..
[/code]

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 />”;
}
?>
[/code]

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

[/quote]

Am I missing something? Should I change a setting on the Apache server? (I’m a neophyte with Apache administration, too.)

Thanks,

^_^

to post a comment
PHP

3 Comments(s)

Copy linkTweet thisAlerts:
@NogDogMay 18.2010 — setcookie() adds the specified cookie to the set of HTTP headers which will be sent to the browser when output begins. It does [b]not[/b] do anything to set the value in $_COOKIE. That gets set when the browser sends a request to the same host and sends the cookie as part of [b]its[/b] HTTP request headers. So it's a sort of "round trip" thing: your page sends the cookie to the browser, then the browser sends it back to the web server, and thus into the $_COOKIE array, on any subsequent request as long as the cookie is still unexpired.
Copy linkTweet thisAlerts:
@WolfShadeauthorMay 18.2010 — Thanks for the reply, NogDog. Just curious, then. If I wanted to store some form field data into cookies immediately after the form is submit, how would I go about doing that? Just as if it were a variable? ($_COOKIE("thisCookie") = $_POST['fieldName'])?

Thanks,

^_^

NEVERMIND ---------- I think I just found the part of the book that explains this. Yeah, it'll be available, but NOT on the page that loads the first time. It'll be set, but inaccessible until the "round trip" is complete - ie, after a refresh OR the next page is reached.

Sorry.. I have a lot of respect for PHP, and am really enjoying this learning experience.. but it seems odd that the data wouldn't be available immediately.. ASP, CF, JavaScript can all access that set data immediately (I'm _NOT_ saying anything disparaging about PHP.. just wishing out loud that it could do the same thing.)

Thanks, again, for your help.
Copy linkTweet thisAlerts:
@NogDogMay 18.2010 — Yes, though perhaps you might want to consider using sessions for such. That way only one cookie, the session ID cookie, has to be sent back and forth with each page request, and that is handled automatically by the PHP session_start() function. Then you can just save any data you want to persist between page requests via the $_SESSION array.

PS: use square brackets, e.g. [b]$_COOKIE['this_cookie'][/b].
×

Success!

Help @WolfShade spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...