WeberDev.com PHP and MySQL Code

LOG IN
BEGINNER GUIDES  |  PHP CLASSES  |  CODE SEARCH  |  ARTICLES SEARCH  |  PHP FORUMS  |  PHP MANUAL  |  PHP FUNCTIONS LIST  |  WEB SITE TEMPLATES
Start typing to search for PHP and MySQL Code Snippets and Articles Search
Submit a code Example / Snippet Submit Your Code
Search Engine Optimization Monitor SEO Monitor
Web Site UpTime Monitor UpTime Monitor
WeberDev's Monthly code contest PHP Code Contest
Your Personal Examples List My Favorite Examples
Your Personal Articles List My Favorite Articles
Edit Account Info Update Your Profile
PHP Code Search
Web Development Forums
Learn MySQL Playing Trivia
PHPBB2 Templates
Web Development Index
Web Development Resources
Web Development Content
PHPClasses
PHP Editor
PHP Jobs
Vision.To Design
Ajax Tutorials
PHP Programming Help
PHP/MySQL Programming
Webmaster Resources
Webmaster Forum
XML meta language
website builder
Mobile Dev World

Go Back Add a Comment Send this example to a friend Add this Article to your personal favoritest for easy future access to your favorite Code Examples and Articles. Submit a code example Print this code example.
BACK ADD A COMMENT SEND TO A FRIEND ADD TO MY FAVORITES ADD CODE EXAMPLES PRINT
Title : PHP Cookies - Simple cookie write/read methods that allow basic encryption
Categories : PHP, Cookies, Security, Encryption Click here to Update Your Picture
Vinod Mohan
Date : Dec 07th 2008
Grade : 1 of 5 (graded 2 times)
Viewed : 4820
File : No file for this code example.
Images : No Images for this code example.
Search : More code by Vinod Mohan
Action : Grade This Code Example
Tools : My Examples List

  Submit your own code examples 
 

Saving and retrieving cookies is extremely simple in PHP using the setcookie() function and $_COOKIE super global variable. But what if you do no want to let the user see the data, let alone alter it? Here is simple cookie write/read methods that allow basic encryption. To delete a cookie, give a negative life

<?php
// Global Cookie Life in seconds, can be overridden in the function call
$cookieLife = 2592000; // 30days

// Global key for encryption, can be overridden in the function call
$encryptKey = '';//'mysecretkey';

/*
@param $name - Name of cookie
@param $val - Value of the cookie
@param $time - Life of the cookie in seconds
@param $key - Key for encrypting cookie
@return TRUE on success else FALSE
*/
function writeCookie($name, $val, $time = null, $key = null){
   
$time = is_null($time) ? $GLOBALS['cookieLife'] : $time;
   
$key = is_null($key) ? $GLOBALS['encryptKey'] : $key;
   
$val = gzcompress(serialize($val) . $key);
   
$val = urlencode(base64_encode($val));

   
// Save the cookie if header is not already sent
   
if(!headers_sent()) {
        return
setcookie($name, $val, time() + $time, '/');
    }else{
        return
FALSE;
    }
}
/*
@param $name - Name of Cookie
@param $key - Key to decrypt, should be same as the one used for encrypting
@param $default - Default value, if the specified cookie does not exist
*/
function readCookie($name, $key = null, $default=null){
    if(isset(
$_COOKIE[$name])){
       
$key = is_null($key) ? $GLOBALS['encryptKey'] : $key;
       
$val = $_COOKIE[$name];
       
$val = base64_decode(urldecode($val));
       
$val = substr(gzuncompress($val), 0, strlen($val) - strlen($key));
       
$val = unserialize($val);
        return
$val;
    }else{
        return
$default;
    }
}
?>



usage example
<?php
// Counter using global key
$counter1 = readCookie('counter1');
$counter1 = is_null($counter1) ? 0 : $counter1;
$str = "Counter 1 - $counter1 (increments by 1)<br/>";
writeCookie('counter1', ++$counter1);

// Counter with key passed during function call
$counter2 = readCookie('counter2', 'mykey');
$counter2 = is_null($counter2) ? 0 : $counter2;
$str .= "Counter 2 - $counter2 (Increments by 2)<br/>";
$counter2 += 2;
writeCookie('counter2', $counter2, null, 'mykey');

// Read cookie, with default value if the specified cookie does not exist
$counter3 = readCookie('counter3', null, 1);
$str .= "Counter 3 - $counter3 (Doubles)<br/>";
$counter3 *= 2;
writeCookie('counter3', $counter3);

echo
$str;

// Invalid Cookie, does not work because output already started at previous line
// If you have output buffering enabled, it will work though
$counter = readCookie('counter', null, 1);
$str .= "Counter Invalid - $counter (Remains 1 always)<br/>";
$counter *= 2;
echo
"Counter Invalid - $counter (Supposed to double, but always shows 2 since header already sent)";
writeCookie('counter', $counter);
?>



Secure Login
Categories : PHP, MySQL, Cookies, Security
A PHP function to encrypt and decrypt a number or string or a combination of the two.
Categories : PHP, Encryption, Security
A very simple PHP single password cookie based login without usernames.
Categories : PHP, Cookies, Security, Beginner Guides
A damaged image generator (class) for validating text. CAPTCHA - Completely Automated Public Turing test to tell Computers and Humans Apart
Categories : PHP, PHP Classes, Security, GD image library, Security
Scramble Eggs - php class to scramble/encode
Categories : PHP, PHP Classes, Security, Encryption
PHP Function to Encrypt/Decrypt a string without a known key. The string itself has his own different key for every character.
Categories : PHP, Algorithms, Security, Authentication, Encryption
Anti SQL injection-PHP
Categories : PHP, MySQL, Security
Password protection for Phorum 3.1.x with userlevels and log.
Categories : PHP, MySQL, Authentication, Security
MD5 based block cypher in 128 bit CFB
Categories : PHP, Encryption, MD5
Simple Cookie example
Categories : PHP, Beginner Guides, Cookies
Generate image with random number (CAPTCHA)
Categories : PHP, GD image library, Graphics, Security
Encrypt/Decrypt string with a given key by using mcrypt module of PHP (suitable to store encrypted data into any type of databases).
Categories : PHP, Encryption
Form Security - Match A Value For Success
Categories : PHP, Authentication, HTML and PHP, Sessions, Security
A Simple Script that stores encrypted messages in databases
Categories : PHP, Databases, MySQL, Security
Protect your email links from being spidered by spam email robots!
Categories : PHP, Security, Mail, Email