Jump to content


This topic is now archived and is closed to further replies.


Encrypting Cookie -- need help

Recommended Posts

I am trying to encrypt a cookie and then read its value in the next page by decrypting it
I am not able to decrypt the cookie value and read it...
Any help would be appreciated very much


// securedata_inc.php

class securedata
    public function __construct()
        $this->key = 'Four score and twenty years ago';
        $this->iv = mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB), MCRYPT_RAND);
    public function encrypt($STR)
        return mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $this->key, $STR, MCRYPT_MODE_CBC, $this->iv);

    public function decrypt($STR)
        return mcrypt_decrypt(MCRYPT_RIJNDAEL_256, $this->key, $STR, MCRYPT_MODE_CBC, $this->iv);

$secure = new securedata;

require_once "securedata_inc.php";

if (!isset($_COOKIE['EMITES'])) {

   $expiry = time()+600; $path = '/';

   $cookieset['sid'] = md5(uniqid(rand(), true));
   $cookieset['user'] = 'Guest';
   $cookieset['logintime'] = time();
   $cookieset['ip'] = $_SERVER['REMOTE_ADDR'];
   $cookieset['useragent'] = $_SERVER['HTTP_USER_AGENT'];

   // Set the cookie
   setcookie('EMITES', $secure->encrypt(serialize($cookieset)), $expiry, $path);
   echo "<p>Cookie placed: ";
} else {
   $cookie = unserialize(stripslashes($secure->decrypt($_COOKIE['EMITES'])));    
   echo '<p>Unserialized cookie:</p><pre>';
   echo '</pre>';
   echo "<p>Session ID: {$cookie['sid']}</p>";
   echo "<p>Login Time: {$cookie['logintime']}</p>";

I am able to set the encrypted cookie.. but not decrypt the cookie and read the values

Thank you

Share this post

Link to post
Share on other sites


Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.