PHP - Server-side cookies
From Global Programming Syntax
Server-side cookies as I call them are website settings stored on a server in a simular fashion as settings website settings stored on a browser. To achieve this, you need a server-side-language (such as PHP) and a database storage solution such as MySQL. You may also want to note that if a user changes their ip address then their server-side cookie data will be lost and will need to be recreated. To install this script, first you will need to setup a database. So create a php file with the following code and configure the following code to connect to your database.
<?
$dbhost='localhost'; //database host (usually localhost)
$accountname='root'; //database username.
$password=''; //database password
$database='my_database'; //database name - not table
//configure the above variables.
$linkID = @mysql_connect($dbhost,$accountname,$password)
or die("Could not connect to MySQL server");
@mysql_select_db($database) or die("Could not select database");
mysql_query('CREATE TABLE `'.$database.'`.`cookies` (`name` TEXT NOT NULL, `value` TEXT NOT NULL,
`ip` TEXT NOT NULL, `expires` TEXT NOT NULL) ENGINE = MyISAM') or die(mysql_error());
echo "Table named 'cookies' has been created successfully."
?>
After configuring the above code, upload the php file (with the above code in it) to your server and open the file. That will create the 'cookies' table then you may delete that file and proceed with the following code using the same database connect settings.
To use the script, first simply copy the below code at the top of the page you wish to use server-side cookies.
<?
//server-side cookies
$dbhost='localhost';
$accountname='root';
$password='';
$database='my_database';
$linkID = @mysql_connect($dbhost,$accountname,$password)
or die("Could not connect to MySQL server");
@mysql_select_db($database) or die("Could not select database");
function ssl_cookie_make($cookies_minutes_till_expire,$cookies_houres_till_expire,$cookies_days_till_expire,$cookiename,$cookievalue)
{
$cookiename=str_replace("'",'"',$cookiename);
$cookies_months_till_expire=0;
$cookieexpiresy=date(Y);
$cookieexpiresm=date(m)+$cookies_months_till_expire;
$cookieexpiresj=date(j)+$cookies_days_till_expire;
$cookieexpiresg=date(G)+$cookies_houres_till_expire;
$cookieexpiresi=date(i)+$cookies_minutes_till_expire;
if ($cookieexpiresi>59)
{
while ($cookieexpiresi>59)
{
$cookieexpiresg+=1;
$cookieexpiresi-=60;
}
}
if ($cookieexpiresg>24)
{
while ($cookieexpiresg>24)
{
$cookieexpiresj+=1;
$cookieexpiresg-=24;
}
}
if ($cookieexpiresj>30)
{
while ($cookieexpiresj>30)
{
$cookieexpiresm+=1;
$cookieexpiresj-=31;
}
}
if ($cookieexpiresm>12)
{
while ($cookieexpiresm>12)
{
$cookieexpiresy+=1;
$cookieexpiresm-=12;
}
}
mysql_query("INSERT INTO `cookies` SET `name`='".$cookiename."', `value`='".$cookievalue."', `ip`='".gethostbyaddr($_SERVER['REMOTE_ADDR'])."',
`expires`='".$cookieexpiresy.",".$cookieexpiresm.",".$cookieexpiresj.",".$cookieexpiresg.",".$cookieexpiresi."'");
}
function ssl_cookie_value($cookiename)
{
$cookiename=str_replace("'",'"',$cookiename);
$cookiesql=mysql_query("SELECT * FROM `cookies`");
$cookiedateexplode=explode(',',date(Y.','.m.','.j.','.G.','.i));
while ($cookierow=mysql_fetch_array($cookiesql))
{
$cookiesqlexplode=explode(',',$cookierow['expires']);
if ($cookiesqlexplode[0]<=$cookiedateexplode[0] && $cookiesqlexplode[1]<=$cookiedateexplode[1] && $cookiesqlexplode[2]<=$cookiedateexplode[2]
&& $cookiesqlexplode[3]<=$cookiedateexplode[3] && $cookiesqlexplode[4]<=$cookiedateexplode[4])
{
mysql_query("DELETE FROM `cookies` WHERE `expires`='".$cookierow['expires']."'");
// AND `ip`='".$cookierow['ip']."' AND `name`='".$cookierow['name']."' AND `value`='".$cookierow['value']."'
}
unset($cookiesqlexplode);
}
unset($cookiedateexplode);
$cookieresult=mysql_query("SELECT `value` FROM `cookies` WHERE `ip`='".gethostbyaddr($_SERVER['REMOTE_ADDR'])."' AND `name`='".$cookiename."'");
$cookierow=mysql_fetch_array($cookieresult);
return $cookierow['value'];
}
function ssl_cookie_changevalue($cookiename,$cookienewvalue)
{
$cookiename=str_replace("'",'"',$cookiename);
mysql_query("UPDATE `cookies` SET `value`='".$cookienewvalue."' WHERE `name`='".$cookiename."' AND `ip`='".gethostbyaddr($_SERVER['REMOTE_ADDR'])."'");
}
function ssl_cookie_changeexpire($cookiename,$cookies_minutes_till_expire,$cookies_houres_till_expire,$cookies_days_till_expire)
{
$cookiename=str_replace("'",'"',$cookiename);
$cookies_months_till_expire=0;
$cookieexpiresy=date(Y);
$cookieexpiresm=date(m)+$cookies_months_till_expire;
$cookieexpiresj=date(j)+$cookies_days_till_expire;
$cookieexpiresg=date(G)+$cookies_houres_till_expire;
$cookieexpiresi=date(i)+$cookies_minutes_till_expire;
if ($cookieexpiresi>59)
{
while ($cookieexpiresi>59)
{
$cookieexpiresg+=1;
$cookieexpiresi-=60;
}
}
if ($cookieexpiresg>24)
{
while ($cookieexpiresg>24)
{
$cookieexpiresj+=1;
$cookieexpiresg-=24;
}
}
if ($cookieexpiresj>30)
{
while ($cookieexpiresj>30)
{
$cookieexpiresm+=1;
$cookieexpiresj-=31;
}
}
if ($cookieexpiresm>12)
{
while ($cookieexpiresm>12)
{
$cookieexpiresy+=1;
$cookieexpiresm-=12;
}
}
mysql_query("UPDATE `cookies` SET `expires`='".$cookieexpiresy.",".$cookieexpiresm.",".$cookieexpiresj.",".$cookieexpiresg.",".$cookieexpiresi."'
WHERE `name`='".$cookiename."' AND `ip`='".gethostbyaddr($_SERVER['REMOTE_ADDR'])."'");
$cookiesql=mysql_query("SELECT * FROM `cookies`");
$cookiedateexplode=explode(',',date(Y.','.m.','.j.','.G.','.i));
while ($cookierow=mysql_fetch_array($cookiesql))
{
$cookiesqlexplode=explode(',',$cookierow['expires']);
if ($cookiesqlexplode[0]<=$cookiedateexplode[0] && $cookiesqlexplode[1]<=$cookiedateexplode[1] && $cookiesqlexplode[2]<=$cookiedateexplode[2]
&& $cookiesqlexplode[3]<=$cookiedateexplode[3] && $cookiesqlexplode[4]<=$cookiedateexplode[4])
{
mysql_query("DELETE FROM `cookies` WHERE `expires`='".$cookierow['expires']."'");
// AND `ip`='".$cookierow['ip']."' AND `name`='".$cookierow['name']."' AND `value`='".$cookierow['value']."'
}
unset($cookiesqlexplode);
}
unset($cookiedateexplode);
}
?>
Then after the above code has been placed at the top of your page, it is then time to learn how to use it so data can easily be saved to the server. Below is a script showing all of the functions and comments on how to use them. The one thing you must note with server-side cookies is that if a user changes their ip address then their server-side cookie data will be lost.
<?
//This function creates a server-side cookie.
//ssl_cookie_make(mins_expire, houres_expire, days_expire, cookie_name, cookie_value);
ssl_cookie_make(6,0,0,'cookie name','value');
//This function returns the value of a cookie for this particular user.
ssl_cookie_value('cookie name');
//This function changes the cookie value for this particular user.
ssl_cookie_changevalue('cookie name','new value');
//This function changes when the cookie expires for this particular user.
//ssl_cookie_changeexpire(cookie_name,mins_expire, houres_expire, days_expire);
ssl_cookie_changeexpire('cookie name',0,-9,0);
?>
