homepage Welcome to WebmasterWorld Guest from 54.163.84.199
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Pubcon Platinum Sponsor 2014
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
macmuso




msg:1251458
 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:

if(isset($username))

{?>

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

<? }
else{
setcookie(username,$name,time()+$cookielife);
?>

...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?

 

andreasfriedrich




msg:1251459
 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:

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

Andreas

macmuso




msg:1251460
 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
if(isset($username))
or the
if(isset(!$username)) and switch the two possible pages around?
andreasfriedrich




msg:1251461
 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
else
{?>
<form><input name="name" type="input"><input type="submit"></form>
<?}?>
Andreas
macmuso




msg:1251462
 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