PHP - Longer Lasting Cookies
From Global Programming Syntax
To create longer lasting cookies, you will need to store the cookies on the server and so that if the user disables cookies, even thoe their cookies are not active, the can still be retrieved by their ip address on the server. However if their ip address changes, the data will be lost unless they turn cookies back on in which case they will be able to access the cookies again. 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 these 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;
setcookie('ssl_cookie', gethostbyaddr($_SERVER['REMOTE_ADDR']), time() + 31536000);
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_updateip()
{
mysql_query("UPDATE `cookies` SET `ip`='".gethostbyaddr($_SERVER['REMOTE_ADDR'])."' WHERE `ip`='".$_COOKIE['ssl_cookie']."'");
setcookie('ssl_cookie', gethostbyaddr($_SERVER['REMOTE_ADDR']), time() + 31536000);
}
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']."'");
}
unset($cookiesqlexplode);
}
unset($cookiedateexplode);
if (isset($_COOKIE['ssl_cookie']))
{
$cookieresult=mysql_query("SELECT `value` FROM `cookies` WHERE `ip`='".$_COOKIE['ssl_cookie']."' AND `name`='".$cookiename."'");
} else {
$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);
if (isset($_COOKIE['ssl_cookie']))
{
mysql_query("UPDATE `cookies` SET `value`='".$cookienewvalue."' WHERE `name`='".$cookiename."' AND `ip`='".$_COOKIE['ssl_cookie']."'");
} else {
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;
}
}
if (isset($_COOKIE['ssl_cookie']))
{
mysql_query("UPDATE `cookies` SET `expires`='".$cookieexpiresy.",".$cookieexpiresm.",".$cookieexpiresj.",".$cookieexpiresg.",".$cookieexpiresi."'
WHERE `name`='".$cookiename."' AND `ip`='".$_COOKIE['ssl_cookie']."'");
} else {
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']."'");
}
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.
<?
//update ip address header.
ssl_cookie_updateip();
//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');
//Note the above functions must be placed before any browser output.
//==========
//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);
//This function returns the value of a cookie for this particular user.
ssl_cookie_value('cookie name');
?>
