note 42830 added to function.setcookie
| From: | jonathonkeogh at msn dot com | Date: | Mon, 31 May 2004 11:05:57 +0000 |
| Subject: | note 42830 added to function.setcookie | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-70339@lists.php.net to get a copy of this message | ||
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.
?>
----
Manual Page -- http://www.php.net/manual/en/function.setcookie.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+42830
Delete -- http://master.php.net/manage/user-notes.php?action=delete+42830&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+42830&report=yes
Search -- http://master.php.net/manage/user-notes.php