ThaiSEOBoard.com

พัฒนาเว็บไซต์ => Programming => ข้อความที่เริ่มโดย: astkboy2008 ที่ 06 มกราคม 2010, 19:53:35



หัวข้อ: Make Your Sessions More Security by storing session data in a MySQL
เริ่มหัวข้อโดย: astkboy2008 ที่ 06 มกราคม 2010, 19:53:35
storing session data in a MySQL required a php class
this class is PHP Sessions Management 1.0.1 (http://www.jooria.com/scripts/User-Management-131/PHP-Sessions-Management-918/index.html)
This class implements a new PHP session handler that can replace the default PHP session handler by storing session data in a MySQL database table.

 The session handler works just by creating an object of this class. After that, applications just need to use the same code to store and retrieve session variables.
How Ican Use it?
its very easy just see this example
examples/example.php (http://www.jooria.com/view/file/433/2)
<?php

    // first create the required mySQL table that is used by this class
    // you can do that by running in mySQL the "session_data.sql" file
    // from the "install" folder!
   
    // change the next values to match the setting of your mySQL database
    $mySQLHost = "localhost";
    $mySQLUsername = "username";
    $mySQLPassword = "password";
   
    // this is the name of the database where you created the table
    // used by this class
    $mySQLDatabase = "database";
   
    $link = mysql_connect($mySQLHost, $mySQLUsername, $mySQLPassword);

    if (!$link) {

        die ("Could not connect to database!");

    }

    $db = mysql_select_db($mySQLDatabase, $link);

    if (!$db) {

        die ("Could not select database!");

    }

    // include the session manager class
    require "../class.dbsession.php";
   
    // instantiate a new session object
    // note that you don't need to call the session_start() function
    // as it is called automatically when the object is instantiated
    $session = new dbsession();

    // from now on, use sessions as you would normally
    // the only difference is that session data is no longer saved on the server
    // but in your database, making the data in it more secure
   
    print_r("
        The first time you run the script there should be an empty array (as there's nothing in the
\$_SESSION array)<br />
        After you press \"refresh\" on your browser you will se the values that were
written in the \$_SESSION array<br>
    ");
    print_r("<pre>");
    print_r($_SESSION);
    print_r("</pre>");

    // add some values to the session
    $_SESSION["value1"] = "hello";
    $_SESSION["value2"] = "world";
   
    // now check the table and see that there is data in it!
   
    // to completely delete a session uncomment the following line
   
    //$session->stop();
?>


and the session will store in the mysql after that

downloading
PHPsessions-6-12.zip [5.29 KB] (http://www.jooria.com/downloads/433/PHPsessions-6-12.zip)
or quick view class.dbsession.php [11.42 KB] (http://www.jooria.com/view/file/433/1)