note 42359 deleted from function.setcookie by vincent

From: Date: Thu, 13 May 2004 13:24:18 +0000
Subject: note 42359 deleted from function.setcookie by vincent
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-69565@lists.php.net to get a copy of this message
Note Submitter: ---- # PROBLEM got some problems with the illustrated example: - when user has stored a bookmark with [YourURL]/?cooktest=1 on his webclient, then the cookie-test was not properly executed. # REASON the reason was, that when the user used the described bookmark, the script was thinking, the user passed already the cookie check (note the URL parameter and also the older example). # SOLUTION to solve this, i used two session vars and "adapted" two older, illustrated examples: 1. first i initalized two session vars (superglobals) a.e. within a global script: <? $_SESSION['bCookieTestDone'] = false; $_SESSION['bCookieEnabled'] = false; ?> 2. then i used following script to check if it's possible to set cookies on the webclient: <? function cookietest(){ // is it the first visit (check session var)? if(!$_SESSION['bCookieTestDone']){ // then try to set a cookie: setcookie('check_cookie', 'on', time()+3600); $_COOKIE['check_cookie'] = 'on'; // when it's set if(isset($_COOKIE['check_cookie'])){ // set the session var to 'true' ... $_SESSION['bCookieEnabled'] = true; } else{ // ... when not to 'false' $_SESSION['bCookieEnabled'] = false; } // finaly store that the user has already visited the page (set session var) $_SESSION['bCookieTestDone'] = true; } } // call the function cookietest(); ?> # REMARKS i didn't descriped how to start the session (function session_start();)!! bear also in mind of the session_save_path(); ... ;-)!

« previous php.notes (#69565) next »