Re: Cookies detect and $_SESSION

From: Date: Mon, 05 Aug 2002 23:38:08 +0000
Subject: Re: Cookies detect and $_SESSION
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-111282@lists.php.net to get a copy of this message
Shouldn't you be using $_COOKIE instead of $_SESSION. Yes, sessions rely on cookies, but not always. You don't say _how_ it "doesn't work" but I assume it always tells you the "cookie" was set. You're not setting any cookie values at all. Look at what you're actually doing: $_SESSION['cookie'] = "yes"; if($_SESSION['cookie'] == "yes") Umm..that's _always_ going to be true. If you want to set a cookie value, you got to have a page refresh. Cookies are not set until the next page is requested... So, you could have something like this: //set cookie $_COOKIE['test'] = "test"; //redirect to new page header("Location: http://www.example.com/index2.php"); Then, on index2.php //see if cookie is not set if(!isset($_COOKIE['test'])) { echo "Cookies must be enabled!"; } Hope that helps... ---John Holmes... ----- Original Message ----- From: "Andre Dubuc" <aajdubuc@webhart.net> To: <php-general@lists.php.net> Sent: Monday, August 05, 2002 5:09 PM Subject: [PHP] Cookies detect and $_SESSION > I'm trying to detect whether the user has cookies enabled on my opening page. > > Only as I neared the end of coding, have I discovered that my site is totally > dependent upon cookies since $_SESSION['whatever_has_been_set] uses cookies! > > So far, I've tried setting <?php $_SESSION['cookie'] = "yes"; > session_write_close(); ?> in the first lines before the headers are set (with > <?php session_start(); ob_start(); ?>). > > Later in the code, I've tried to pick up the empty Array that $_SESSION > produces if the user has cookies disabled: > > <?php > > if ($_SESSION['cookie'] == "yes") { } > else { > print ("<h2>Cookies must be enabled to use this site</h2>"); > } > > ?> > > I've also tried (instead of 'else') > elseif ($_SESSION['cookie'] == "") { > print ("<h2>Cookies must be enabled to use this site</h2>"); > } > > and > elseif (is_null($_SESSION['cookie'] ) { > print ("<h2>Cookies must be enabled to use this site</h2>"); > } > > These do not work. Any ideas where I'm going wrong? Any help greatly > appreciated. > > Regards, Andre > > > > -- > PHP General Mailing List (http://www.php.net/) > To unsubscribe, visit: http://www.php.net/unsub.php >

« previous php.general (#111282) next »