Page 1 of 1

Setting Cookies

Posted: Wed Aug 29, 2007 8:45 am
by gplev
Everah | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Code: Select all

<?php

if (isset($_COOKIE["test"]))
  echo "Welcome " . $_COOKIE["test"] . "!<br />";
else
{
session_start();
$ses_id = session_id();
setcookie("test", $ses_id);
echo "New User";
}
?>
is there something with the way it is written. It seems that after I close out of IE or Firefox. The cookie is lost or is reset. Any ideas.


Everah | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Posted: Wed Aug 29, 2007 8:52 am
by xpgeek
You don't need to set cookie.
Use $_SESSION variable to store session variables.
Cookies would sent automatically.

Posted: Wed Aug 29, 2007 9:02 am
by gplev
so how I rewrite this script to do that.

Re: Setting Cookies

Posted: Wed Aug 29, 2007 12:01 pm
by RobertGonzalez
gplev wrote:

Code: Select all

<?php

if (isset($_COOKIE["test"]))
  echo "Welcome " . $_COOKIE["test"] . "!<br />";
else
{
session_start();
$ses_id = session_id();
setcookie("test", $ses_id);
echo "New User";
}
?>
is there something with the way it is written. It seems that after I close out of IE or Firefox. The cookie is lost or is reset. Any ideas.
Yeah, I have an idea. You are setting a browser session lifetime cookie. That means that the cookie will stay alive as long as the browser window is open. Once the browser closes, all browser session cookies get canned by the browser.

Read the manual on setcookie(). There is a pretty good amount of information about how to set cookies and cookie expiry times.