Re: Cookies detect and $_SESSION

From: Date: Tue, 06 Aug 2002 01:35:49 +0000
Subject: Re: Cookies detect and $_SESSION
References: 1 2 3  Groups: php.general 
Request: Send a blank email to php-general+get-111313@lists.php.net to get a copy of this message
Hi John, This is done on the first page: <?php session_start(); ob_start(); ?> /* html . . head stuff . . . . <?php if ($_SESSION['cookie'] == "") { print("<h2>Please 'Enable Cookies' to use this site</h2>"); $_SESSION['cookie'] = "1"; session_write_close(); } if($_SESSION['cookie'] != "") {} ?> This code sort of works. When I first load the page with cookies enabled, I get the 'Enable Cookies please' message. However, if I reload or move to another page, and return, it disappears because of the '$_SESSION['cookie'] = "1" '. With cookies disabled, it works as it should. Perhaps this is the best it will get without further fancy footwork, but it would be nice not to have the 'error' message display for those who do have cookies enabled. Any suggestions? Thnaks again for your help, Regards, Andre . . . On Monday 05 August 2002 08:36 pm, 1LT John W. Holmes wrote: > > Hi John, > > > > That makes sense. I guess it's brain-bloat. I've never bothered using > > $_COOKIE simply because I thought I wasn't using them. So I'll give that > > a try. > > > > But getting back to my earlier question, when I switch off cookies in > > Opera > > > or Konqueror browser, and print_r($_SESSION); the result is Array(). Now, > > that's telling me that the 'yes' was not passed to the $_SESSION. How > > come > > I > > > cannot set a condition of 'null' or "" to determine/use this > > condition? > > I've > > > tried switch, and if. They don't detectt it. Why? I've added some > > comments below as well. > > It's kind of hard to follow that without seeing your actual code. Are you > doing print_r on the second page? > > ---John Holmes... > > > Regards, Andre > > > > On Monday 05 August 2002 07:38 pm, 1LT John W. Holmes wrote: > > > You don't say _how_ it "doesn't work" but I assume it always > > > tells you > > the > > > > "cookie" was set. > > > > Correct. > > > > > 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. > > > > Not always. If $_SESSION['cookie'] does not get 'passed' to $_SESSION > > (which > > > it doesn't when I view it wih print_r($_SESSION);). > > > > > 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...

« previous php.general (#111313) next »