Consider the following codes I collect from the libs/login.php
file of Kahuk CMS
:
function SetIDCookie($what, $remember) {
global $db;
$domain = preg_replace('/^www/','',$_SERVER['HTTP_HOST']);
// Remove port information.
$port = strpos($domain, ':');
if ($port !== false) $domain = substr($domain, 0, $port);
if (!strstr($domain,'.') || strpos($domain,'localhost:')===0) $domain='';
switch ($what) {
case 0: // Borra cookie, logout
setcookie ("mnm_user", "", time()-3600, "/",$domain); // Expiring cookie
setcookie ("mnm_key", "", time()-3600, "/",$domain); // Expiring cookie
setcookie ("mnm_data", "", time()-3600, "/",$domain); // Expiring cookie
setcookie ("mnm_user", "", time()-3600, "/"); // Expiring cookie
setcookie ("mnm_key", "", time()-3600, "/"); // Expiring cookie
setcookie ("mnm_data", "", time()-3600, "/"); // Expiring cookie
break;
case 1: //Usuario logeado, actualiza el cookie
// Atencion, cambiar aqu�cuando se cambie el password de base de datos a MD5
$strCookie=base64_encode(join(':',
array(
$this->user_login,
crypt($this->user_login, 22),
$this->md5_pass)
)
);
if($remember){
$time = time() + (60 * 60 * 24 * 10); //Cookie will expire in 10 days if remember option is selected at login. 86400 = 1 day
} else {
$time = 0; //Cookie will expire when browser session ends. Note: This may depend on your browser settings. Example: in Chrome if 'Continue where you left off' option is checked the cookie won't expire.
}
// Setting httponly and secure true to add additional security for cookies
if(isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') {
setcookie('mnm_user', $this->user_login, $time, '/',$domain, TRUE, TRUE);
setcookie('mnm_key', $strCookie, $time, '/',$domain, TRUE, TRUE);
setcookie('mnm_data', md5(sha1($this->ip.':'.$this->user_agent)), $time, '/', $domain, TRUE, TRUE);
} else {
setcookie('mnm_user', $this->user_login, $time, '/',$domain, FALSE, TRUE);
setcookie('mnm_key', $strCookie, $time, '/',$domain, FALSE, TRUE);
setcookie('mnm_data', md5(sha1($this->ip.':'.$this->user_agent)), $time, '/', $domain, FALSE, TRUE);
}
break;
}
}
Whenever a new user logged-in in my website, it creates temporary cookies files for 10 days. Now my vps contain nearly a million temporary cookies files which makes the server almost down because the CPU takes too much time to find out correct cookies whenever get a new request.
What is the solutions?
Update:
JFYI, here is the session files and the content in a session file from my VPS server.
5