Welcome to WebmasterWorld Guest from 54.147.20.131

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

Basic cookies

Problems setting cookies with PHP

     

macmuso

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

10+ Year Member



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

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

WebmasterWorld Senior Member 10+ Year Member



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

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

10+ Year Member



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

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

WebmasterWorld Senior Member 10+ Year Member



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

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

10+ Year Member



Thanks it works great now :)
 

Featured Threads

Hot Threads This Week

Hot Threads This Month