SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Evangelist ckchin's Avatar
    Join Date
    Mar 2002
    Location
    msia
    Posts
    487
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Session and capture it and store it in Mysql

    Hi,

    i would like to write a simple php project that have PHP session and I want capture it and store it in Mysql. This is something like shoppcart.

    Do you have a very simple demo script?
    How to start with PHP session? I read the PHP manual and there are too many function talking abour PHP session, but which one should I use?

    I read certain script that use session array? What is session array actually?

    Thanks in advance.

  2. #2
    SitePoint Guru Richi's Avatar
    Join Date
    Jul 2001
    Location
    Hu,Bp.
    Posts
    842
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i don`t remember from where i got this code, so i`ll post it here !

    PHP Code:
    <?
    /* ------------------------------------------------------------------------
     * session_mysql.php
     * ------------------------------------------------------------------------
     * PHP4 MySQL Session Handler
     * Version 1.00
     * by Ying Zhang (ying@zippydesign.com)
     * Last Modified: May 21 2000
     *
     * ------------------------------------------------------------------------
     * TERMS OF USAGE:
     * ------------------------------------------------------------------------
     * You are free to use this library in any way you want, no warranties are
     * expressed or implied.  This works for me, but I don't guarantee that it
     * works for you, USE AT YOUR OWN RISK.
     *
     * While not required to do so, I would appreciate it if you would retain
     * this header information.  If you make any modifications or improvements,
     * please send them via email to Ying Zhang <ying@zippydesign.com>.
     *
     * ------------------------------------------------------------------------
     * DESCRIPTION:
     * ------------------------------------------------------------------------
     * This library tells the PHP4 session handler to write to a MySQL database
     * instead of creating individual files for each session.
     *
     * Create a new database in MySQL called "sessions" like so:
     *
     * CREATE TABLE sessions (
     *      sesskey char(32) not null,
     *      expiry int(11) unsigned not null,
     *      value text not null,
     *      PRIMARY KEY (sesskey)
     * );
     *
     * ------------------------------------------------------------------------
     * INSTALLATION:
     * ------------------------------------------------------------------------
     * Make sure you have MySQL support compiled into PHP4.  Then copy this
     * script to a directory that is accessible by the rest of your PHP
     * scripts.
     *
     * ------------------------------------------------------------------------
     * USAGE:
     * ------------------------------------------------------------------------
     * Include this file in your scripts before you call session_start(), you
     * don't have to do anything special after that.
     */

    $SESS_DBHOST = "localhost";            /* database server hostname */
    $SESS_DBNAME = "ses";            /* database name */
    $SESS_DBUSER = "";        /* database user */
    $SESS_DBPASS = "";        /* database password */

    $SESS_DBH = "";
    $SESS_LIFE = get_cfg_var("session.gc_maxlifetime");

    function sess_open($save_path, $session_name) {
        global $SESS_DBHOST, $SESS_DBNAME, $SESS_DBUSER, $SESS_DBPASS, $SESS_DBH;

        if (! $SESS_DBH = mysql_pconnect($SESS_DBHOST, $SESS_DBUSER, $SESS_DBPASS)) {
            echo "<li>Can't connect to $SESS_DBHOST as $SESS_DBUSER";
            echo "<li>MySQL Error: ", mysql_error();
            die;
        }

        if (! mysql_select_db($SESS_DBNAME, $SESS_DBH)) {
            echo "<li>Unable to select database $SESS_DBNAME";
            die;
        }

        return true;
    }

    function sess_close() {
        return true;
    }

    function sess_read($key) {
        global $SESS_DBH, $SESS_LIFE;

        $qry = "SELECT value FROM sessions WHERE sesskey = '$key' AND expiry > " . time();
        $qid = mysql_query($qry, $SESS_DBH);

        if (list($value) = mysql_fetch_row($qid)) {
            return $value;
        }

        return false;
    }

    function sess_write($key, $val) {
        global $SESS_DBH, $SESS_LIFE;

        $expiry = time() + $SESS_LIFE;
        $value = addslashes($val);

        $qry = "INSERT INTO sessions VALUES ('$key', $expiry, '$value')";
        $qid = mysql_query($qry, $SESS_DBH);

        if (! $qid) {
            $qry = "UPDATE sessions SET expiry = $expiry, value = '$value' WHERE sesskey = '$key' AND expiry > " . time();
            $qid = mysql_query($qry, $SESS_DBH);
        }

        return $qid;
    }

    function sess_destroy($key) {
        global $SESS_DBH;

        $qry = "DELETE FROM sessions WHERE sesskey = '$key'";
        $qid = mysql_query($qry, $SESS_DBH);

        return $qid;
    }

    function sess_gc($maxlifetime) {
        global $SESS_DBH;

        $qry = "DELETE FROM sessions WHERE expiry < " . time();
        $qid = mysql_query($qry, $SESS_DBH);

        return mysql_affected_rows($SESS_DBH);
    }

    session_set_save_handler(
        "sess_open",
        "sess_close",
        "sess_read",
        "sess_write",
        "sess_destroy",
        "sess_gc");
    ?>


    PHP Code:
    <?
    /* ------------------------------------------------------------------------
     * session_dbm.php
     * ------------------------------------------------------------------------
     * PHP4 DBM Session Handler
     * Version 1.00
     * by Ying Zhang (ying@zippydesign.com)
     * Last Modified: May 21 2000
     *
     * ------------------------------------------------------------------------
     * TERMS OF USAGE:
     * ------------------------------------------------------------------------
     * You are free to use this library in any way you want, no warranties are
     * expressed or implied.  This works for me, but I don't guarantee that it
     * works for you, USE AT YOUR OWN RISK.
     *
     * While not required to do so, I would appreciate it if you would retain
     * this header information.  If you make any modifications or improvements,
     * please send them via email to Ying Zhang <ying@zippydesign.com>.
     *
     * ------------------------------------------------------------------------
     * DESCRIPTION:
     * ------------------------------------------------------------------------
     * This library tells the PHP4 session handler to write to a DBM file
     * instead of creating individual files for each session.
     *
     * ------------------------------------------------------------------------
     * INSTALLATION:
     * ------------------------------------------------------------------------
     * Make sure you have DBM support compiled into PHP4.  Then copy this
     * script to a directory that is accessible by the rest of your PHP
     * scripts.
     *
     * ------------------------------------------------------------------------
     * USAGE:
     * ------------------------------------------------------------------------
     * Include this file in your scripts before you call session_start(), you
     * don't have to do anything special after that.
     */

    $SESS_DBM = "";
    $SESS_LIFE = get_cfg_var("session.gc_maxlifetime");

    function sess_open($save_path, $session_name) {
        global $SESS_DBM;

        $SESS_DBM = dbmopen("$save_path/$session_name", "c");
        return ($SESS_DBM);
    }

    function sess_close() {
        global $SESS_DBM;

        dbmclose($SESS_DBM);
        return true;
    }

    function sess_read($key) {
        global $SESS_DBM, $SESS_LIFE;

        $var = "";
        if ($tmp = dbmfetch($SESS_DBM, $key)) {
            $expires_at = substr($tmp, 0, strpos($tmp, "|"));

            if ($expires_at > time()) {
                $var = substr($tmp, strpos($tmp, "|") + 1);
            }
        }

        return $var;
    }

    function sess_write($key, $val) {
        global $SESS_DBM, $SESS_LIFE;

        dbmreplace($SESS_DBM, $key, time() + $SESS_LIFE . "|" . $val);
        return true;
    }

    function sess_destroy($key) {
        global $SESS_DBM;

        dbmdelete($SESS_DBM, $key);
        return true;
    }

    function sess_gc($maxlifetime) {
        global $SESS_DBM;

        $now = time();
        $key = dbmfirstkey($SESS_DBM);
        while ($key) {
            if ($tmp = dbmfetch($SESS_DBM, $key)) {
                $expires_at = substr($tmp, 0, strpos($tmp, "|"));
                if ($now > $expires_at) {
                    sess_destroy($key);
                }
            }

            $key = dbmnextkey($SESS_DBM, $key);
        }
    }

    session_set_save_handler(
        "sess_open",
        "sess_close",
        "sess_read",
        "sess_write",
        "sess_destroy",
        "sess_gc");
    ?>
    PHP Code:
    <?
    /* ------------------------------------------------------------------------
     * test.php
     * ------------------------------------------------------------------------
     * PHP4 Customer Session Handler Test Script
     * Version 1.00
     * by Ying Zhang (ying@zippydesign.com)
     * Last Modified: May 21 2000
     */

    /* default to DBM handler */
    if (! isset($handler)) {
        $handler = "mysql";
    }

    /* default action is increment */
    if (! isset($action)) {
        $action = "increment";
    }

    /* load up the appropriate session handling script, depending on the handler */
    if ($handler == "dbm") {
        include("session_dbm.php");

    } elseif ($handler == "mysql") {
        include("session_mysql.php");

    } else {
        echo "<li>Unrecognized handler ($handler)";
        die;
    }

    /* start the session and register a simple counter */
    session_start();
    session_register("count");

    /* figure out what we should do, depending on the action */
    switch ($action) {
        case "increment" :
            $count = isset($count) ? $count + 1 : 0;
            break;

        case "destroy" :
            session_destroy();
            break;

        case "gc" :
            $maxlife = get_cfg_var("session.gc_maxlifetime");
            sess_gc($maxlife);
            break;

        default:
            echo "<li>Unknown action ($action)";
            break;
    }
    ?>

    <h1>Session Test Script</h1>
    <ul>
    <li>Handler: <b><?=$handler?></b>
    <li>Action: <b><?=$action?></b>
    <li>Count: <b><?=$count?></b>
    </ul>

    <hr size=1>
    <form>
    <table>
    <tr>
        <td>Handler:</td>
        <td>
            <select name="handler">
            <option value="dbm">DBM</option>
            <option value="mysql">MySQL</option>
            </select>
        </td>
    </tr>
    <tr>
        <td>Action:</td>
        <td>
            <select name="action">
            <option value="increment">Increment</option>
            <option value="destroy">Session Destroy</option>
            <option value="gc">Force Garbage Collection</option>
            </select>
        </td>
    </tr>
    <tr>
        <td></td>
        <td><br><input type="submit"></td>
    </tr>
    </table>
    </form>
    i have more tut about session , if u need more give me ur email !

  3. #3
    SitePoint Evangelist ckchin's Avatar
    Join Date
    Mar 2002
    Location
    msia
    Posts
    487
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks a lot. Luckily I have this from you, else I have no need to get it myself by referring book.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •