homepage Welcome to WebmasterWorld Guest from
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

Basic cookies
Problems setting cookies with PHP

 3:36 am on Jan 12, 2003 (gmt 0)

I have a site, and what I want to do is set it up so that the first time a user goes there, they have to type in their name, and from then on, whenever they visit index.php their name is remembered, and displayed as, 'welcome back, joe bloggs'. What I have done is set up the site so the php script goes as follows:



...the main page I want to display - including "welcome back, <? echo $name?> ...

<? }

...The page with the form, which has method="get" and action="index.php"....

<? }

Something is wrong here, because when I load the page, it asks me for my name, then I click submit, and the same page comes up and asks for my name again. However, if I click refresh or submit again, the correct page loads! What is making this happen? Different things happen when I shift the setcookie() around, am I putting it in the right place?



 4:09 am on Jan 12, 2003 (gmt 0)

Let me welcome you to WebmasterWorld [webmasterworld.com] macmuso.

If you havenīt done it yet I suggest you read Marciaīs WebmasterWorld Welcome and Guide to the Basics [webmasterworld.com] post which contains a lot of helpful information.

Have you checked whether caching is an issue.

I got something similar to work using code along these lines:

if(name) {
cookie = name
if(cookie) {
print cookie
} else {
print form



 6:57 am on Jan 12, 2003 (gmt 0)

Also... after the first visit, all it says is "welcome back,!". Do I have to code something that loads the cookie or is it available all the time? Is it going to make any difference if I use the
or the
if(isset(!$username)) and switch the two possible pages around?

 6:32 pm on Jan 12, 2003 (gmt 0)

On the page that is displayed after the form is submitted the cookie does not exist yet. The server will send the cookie in the response header with this page.

To make the name appear on that page you need to use the value supplied by the submitted form. That is what the cookie = name does in my pseudo code.

Here is how the pseudo code would look like in real PHP code.

# prevent caching
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");
# If the form is submitted $_GET['name'] will contain the name
if($_GET['name']) {
# the cookie will be added to the response header which
# is sent back to the browser along with this page
setcookie('username', $_GET['name']);
# since the cookie does not exist on the client side yet
# we set the variable to the submitted value
$_COOKIE['username'] = $_GET['name'];
# if we have a username, display it
if(isset($_COOKIE['username'])) {?>
<p>Hallo <?=$_COOKIE['username']?></p>
# otherwise display the form to get the username
<form><input name="name" type="input"><input type="submit"></form>

 4:19 am on Jan 15, 2003 (gmt 0)

Thanks it works great now :)

Global Options:
 top home search open messages active posts  

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved