Monday, April 2, 2012

Transefer PHP class instances as session data using serialization, compression and encryption techniques


The intension of this article is to explain how the PHP class instances(objects) are transferred among PHP pages with the use of sessions. All the objects (including contents) will be serialized, then compressed and encrypted prior to store in the user session.
When retrieving the object from the session variable, you need to remember that it is in the base64 decrypted format. Once you decrypt the object, you will get the object as a deflated string(compressed data). Then you need to inflate (decompress) the deflated string. Then you will get the serialized object(instance). Then you need to deserialized (unserialized) the instance to construct the original instance.

 please refer the below project structure.


we will go through each of the source code files as below.

ConfigData.php
<?php
/**
 * Created by
 * User: Chathuranga Tennakoon
 * Email: chathuranga.t@gmail.com
 * Blog: http://chathurangat.blogspot.com
 * Date: 03/29/12
 * Time: 9:13 AM
 * IDE:  JetBrains PhpStorm.
 */
class ConfigData
{

    private  $name;
    private  $email;
    private  $website;


    public function setEmail($email)
    {
        $this->email = $email;
    }

    public function getEmail()
    {
        return $this->email;
    }

    public function setName($name)
    {
        $this->name = $name;
    }

    public function getName()
    {
        return $this->name;
    }

    public function setWebsite($website)
    {
        $this->website = $website;
    }

    public function getWebsite()
    {
        return $this->website;
    }

}

?>

TestInterface.php
<?php
/**
 * Created by
 * User: Chathuranga Tennakoon
 * Email: chathuranga.t@gmail.com
 * Blog: http://chathurangat.blogspot.com
 * Date: 03/29/12
 * Time: 9:13 AM
 * IDE:  JetBrains PhpStorm.
 */
interface TestInterface
{

public function getInstance();
public function setInstance(ConfigData $cfgData);

}
?>


TestImpl.php
<?php
/**
 * Created by
 * User: Chathuranga Tennakoon
 * Email: chathuranga.t@gmail.com
 * Blog: http://chathurangat.blogspot.com
 * Date: 03/29/12
 * Time: 9:13 AM
 * IDE:  JetBrains PhpStorm.
 */
include "interfaces/TestInterface.php";
include "config/ConfigData.php";


class TestImpl implements TestInterface
{
     private $config = NULL;

    public function getInstance()
    {

        return $this->config;
    }


    public function setInstance(ConfigData $cfgData){

        $this->config = $cfgData;
    }

}


?>


testSend.php
<?php
/**
 * Created by
 * User: Chathuranga Tennakoon
 * Email: chathuranga.t@gmail.com
 * Blog: http://chathurangat.blogspot.com
 * Date: 03/29/12
 * Time: 9:13 AM
 * IDE:  JetBrains PhpStorm.
 */
session_start();

include "classes/TestImpl.php";

$testImpl =  new TestImpl();

$config = new ConfigData();
$config->setName("chathuranga tennakoon");
$config->setEmail("chathuranga.t@gmail.com");
$config->setWebsite("http://chathurangat.blogspot.com");


$testImpl  = new TestImpl();
$testImpl->setInstance($config);

$_SESSION["TestImplObject"] = base64_encode(gzdeflate(serialize($testImpl)));

?>


testReceive.php
<?php

session_start();

include "classes/TestImpl.php";


$testImpl = new TestImpl();

//printing the session data as it is 
echo "<b>Session Data (Encrypted) </b>[".$_SESSION["TestImplObject"]."]<br/><br/>";

//decrypt the session data with base64 decryption mechanism
echo "<b>Decrypted Session Data in DEFLATE data format (Deflated Session Data) </b>[".base64_decode($_SESSION["TestImplObject"])."]<br/><br/>";

//inflating the deflated string
echo "<b>Inflated Value of the Deflated Value (Unserialized Object) </b>[".gzinflate(base64_decode($_SESSION["TestImplObject"]))."]<br/><br/>";

//unserializing the serialized object
echo "<b>Unserializing the Serialized Object (Serialized Object) </b>[".unserialize(gzinflate(base64_decode($_SESSION["TestImplObject"])))."]<br/><br/>";

//do all above operations in a single statement(line)
$testImpl = new TestImpl();
$testImpl = unserialize(gzinflate(base64_decode($_SESSION["TestImplObject"])));


echo "TestImpl Instance (Serialized Shared Instance) <br/>";

//getting the instance of class ConfigData
$configInstance =  new ConfigData();
$configInstance = $testImpl->getInstance();

//retrieving the object reference ID of the $configInstance
echo "Object Reference ID of the retrieved instance of ConfigData class [".spl_object_hash($configInstance)."] <br/> <br/> <br/>";

echo "<b>Retrieved Member variable data of the instance of ConfigData class</b> <br/><br/>";

echo " <b>Name </b>[".$configInstance->getName()."]<br/><br>";
echo " <b>Email </b>[".$configInstance->getEmail()."]<br/><br>";
echo " <b>Website </b>[".$configInstance->getWebsite()."]<br/><br>";

?>
 


output of the testReceive.php is as follows.




The sample application is available to be downloaded through the following link.

Download Source Code


 Hope this will be helpful for you !!!
 Thanks and Regards
 Chathuranga Tennakoon
 chathuranga.t@gmail.com

No comments:

Post a Comment