note 42830 deleted from function.setcookie by philip

From: Date: Mon, 12 Jul 2004 20:14:01 +0000
Subject: note 42830 deleted from function.setcookie by philip
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-73121@lists.php.net to get a copy of this message
Note Submitter: jonathonkeogh@msn.com ---- If you need to go over the 4 kilobyte limit, or past 300 cookies, why not store the cookies on your own server? I haven't tested any of the examples, so please do not complain if they do not work. Example (for session cookies): <?php // Directory for storing session cookies... $cookie_dir = "../sessions"; if(!isset($_COOKIE['php_session_id'])) { // Find the NEXT session ID... if(!file_exists($cookie_dir."/ids.txt")){ $res=fopen($cookie_dir."/ids.txt", "w+"); fwrite($res, 0); fclose($res); } $session_id = file_get_contents($cookie_dir."/ids.txt")+1; // Store the user's session ID... setcookie('php_session_id', $session_id); // Create the user's session directory: mkdir($cookie_dir."/".$session_id); } // Create a class for the functions... class s { function setcookie($name, $value="") { global $cookie_dir, $session_id; $res=fopen($cookie_dir."/".$session_id."/".$name, "w+") fwrite($res, $value); fclose($res); } function getcookie($name) { global $cookie_dir, $session_id; if(!file_exists($cookie_dir."/".$session_id."/".$name)) { return false; } return file_get_contents($cookie_dir."/".$session_id."/".$name); } } $session_cookie = new s; // Examples of usage... // // This... print $session_cookie->getcookie('username'); // ...would return false, because the value has NOT been set yet. // // This... print $session_cookie->setcookie('email_address', 'xyz@xgdfgdfgdjf.xyz'); // ...would return nothing, because it is setting a cookie. // // This... print $session_cookie->getcookie('email_address'); // ...would return 'xyz@xgdfgdfgdjf.xyz', because it was set earlier on. ?> Example (for long term cookies): <?php // Directory for storing session cookies... $cookie_dir = "../cookies"; /* Use IP address for long term cookies...something that won't change, and the cookies definatly can not be removed by the client! */ $cookie_id = $_SERVER['REMOTE_ADDR']; // Create a class for the functions... class c { function setcookie($name, $value="") { global $cookie_dir, $session_id; $res=fopen($cookie_dir."/".$session_id."/".$name, "w+") fwrite($res, $value); fclose($res); } function getcookie($name) { global $cookie_dir, $session_id; if(!file_exists($cookie_dir."/".$session_id."/".$name)) { return false; } return file_get_contents($cookie_dir."/".$session_id."/".$name); } } $cookie = new c; // Examples of usage... // // This... print $cookie->getcookie('username'); // ...would return false, because the value has NOT been set yet. // // This... print $cookie->setcookie('email_address', 'xyz@xgdfgdfgdjf.xyz'); // ...would return nothing, because it is setting a cookie. // // This... print $cookie->getcookie('email_address'); // ...would return 'xyz@xgdfgdfgdjf.xyz', because it was set earlier on. ?>

« previous php.notes (#73121) next »