Jump to content

Recommended Posts

Hi,
I am an average programmer, i am having problems with a script which i took from [url=http://www.zend.com/zend/spotlight/code-gallery-wade8.php?article=code-gallery-wade8&kind=sl&id=2787&open=1&anc=0&view=1]Custom Session Handling[/url] I made some modification for my needs, but i am facing some problems, The script is executing twice and after 6 seconds its not removing the old sessions (which it should).
What is wrong ? i am not getting, I am stuck and frustrated. any help would be greatly appreciated.Thanks in advance.

DS

[code]
filname=testsession.php
<?php

/* Create new object of class */
ini_set("session.use_only_cookies",false);
require_once("session_api.php");
$ses_class = new session(0.1);

if (!isset ($_SESSION['counter'])) {
    $_SESSION['counter'] = 0;
}

$_SESSION['counter'] = $_SESSION['counter']+1;

echo $_SESSION['counter'];
echo "<br>";
echo $ses_class->ses_maxTime;
echo "<br>";
echo ini_get ("session.gc_maxlifetime");
echo "<br>";
echo ini_get("session.use_only_cookies");

//*********TESTING********
    $fwhandle = @fopen("test.txt","a");
if($fwhandle)
{
if (fwrite($fwhandle, "script called\n") === FALSE)
echo "Cannot write to file";

fclose($fwhandle);
}
else
echo "Cannot open to write file";
    //*********CLOSE TESTING********
?>

filename=session_api.php
<?php

//http://www.zend.com/zend/spotlight/code-gallery-wade8.php?article=code-gallery-wade8&kind=sl&id=2787&open=1&anc=0&view=1
//http://us2.php.net/manual/en/function.session-set-save-handler.php#60316

//session_set_save_handler(
// array("Session", "_open"),
// array("Session", "_close"),
// array("Session", "_read"),
// array("Session", "_write"),
// array("Session", "_destroy"),
// array("Session", "_gc")
// ) or die("Failed to register session handler");
//
//session_start();

class Session
{
    var $db_host = "www.somehost.com";
    var $db_user = "username";
    var $db_pass = "password";
    var $db_dbase = "mysessions";
    /* MySQL Table name for session */
    var $ses_table = "sessions";

var $isdb_con = true;
    var $app_name = "dummy_name";

    var $ses_maxTime=6;
    var $ses_onlyCookie;

/* Class Constructor with max Time out of session in minutes */
function Session($minutes=0)
{
$in_secs = $minutes * 60;
if($in_secs<=0)
$this->ses_maxTime = ini_get("session.gc_maxlifetime");
else
{
ini_set("session.gc_maxlifetime",$in_secs);
$this->ses_maxTime = $minutes * 60;
}

$this->ses_onlyCookie = ini_get("session.use_only_cookies");

//register_shutdown_function('session_write_close');
ini_set("session.gc_probability", 100);
session_set_save_handler(array(&$this, '_open'),
                            array(&$this, '_close'),
                            array(&$this, '_read'),
                            array(&$this, '_write'),
                            array(&$this, '_destroy'),
                            array(&$this, '_gc'));

//Start the session
  session_start();

}

    /* Create a connection to a database */
    function db_connect()
    {
        $mysql_connect = @mysql_pconnect ($this->db_host, $this->db_user, $this->db_pass);
        $mysql_db = @mysql_select_db ($this->db_dbase);

        if (!$mysql_connect || !$mysql_db)
        {
            return FALSE;
        }
        else
        {
            return TRUE;
        }
    }

    /* Open session, if you have your own db connection
      code, put it in here! */
    function _open($path, $name)
    {
    //*********TESTING********
    $fwhandle = @fopen("test.txt","a");
if($fwhandle)
{
if (fwrite($fwhandle, "_open method called\n") === FALSE)
echo "Cannot write to file";

fclose($fwhandle);
}
else
echo "Cannot open to write file";
    //*********CLOSE TESTING********

        if ($this->isdb_con == "Y")
        {
            $this->db_connect();
        }

        return TRUE;
    }

    /* Close session */
    function _close()
    {
    //*********TESTING********
    $fwhandle1 = @fopen("test.txt","a");
if($fwhandle1)
{
if (fwrite($fwhandle1, "_close method called\n") === FALSE)
echo "Cannot write to file";

fclose($fwhandle1);
}
else
echo "Cannot open to write file";
    //*********CLOSE TESTING********

        /* This is used for a manual call of the
          session gc function */
        $this->_gc(0);

        return TRUE;
    }

    /* Read session data from database */
    function _read($ses_id)
    {
    //*********TESTING********
    $fwhandle2 = @fopen("test.txt","a");
if($fwhandle2)
{
if (fwrite($fwhandle2, "_read method called\n") === FALSE)
echo "Cannot write to file";

fclose($fwhandle2);
}
else
echo "Cannot open to write file";
    //*********CLOSE TESTING********

        $session_sql = "SELECT * FROM " . $this->ses_table
                    . " WHERE ses_id = '$ses_id'";

// echo "<br/>$session_sql<br/>";

        $session_res = @mysql_query($session_sql);
        if (!$session_res)
        {
            return '';
        }

        $session_num = @mysql_num_rows ($session_res);
        if ($session_num > 0)
        {
            $session_row = mysql_fetch_assoc ($session_res);

            if(isset($_SERVER['REMOTE_ADDR']))
$ipnum = "$_SERVER[REMOTE_ADDR]";
else
$ipnum = "";

if($session_row["ip_number"] !=$ipnum)
{
//don't show anything
return '';
}

            $ses_data = $session_row["ses_value"];

            return $ses_data;
        }
        else
        {
            return '';
        }
    }

    /* Write new data to database */
    function _write($ses_id, $data)
    {
    //*********TESTING********
    $fwhandle3 = @fopen("test.txt","a");
if($fwhandle3)
{
if (fwrite($fwhandle3, "_write method called\n") === FALSE)
echo "Cannot write to file";

fclose($fwhandle3);
}
else
echo "Cannot open to write file";
    //*********CLOSE TESTING********

        $session_sql = "UPDATE " . $this->ses_table
                    . " SET ses_time=" . time()
                    . ", ses_value='$data' WHERE ses_id='$ses_id'";
echo "<br/>$session_sql<br/>";
        $session_res = @mysql_query ($session_sql);

        if (!$session_res)
        {
            return FALSE;
        }
        if (mysql_affected_rows ())
        {
            return TRUE;
        }

if(isset($_SERVER['REMOTE_ADDR']))
$ipnum = "'$_SERVER[REMOTE_ADDR]'";
else
$ipnum = Null;

        $session_sql = "INSERT INTO " . $this->ses_table
                    . " (ses_id, ses_time, ses_start, ses_value,app_name,ip_number)"
                    . " VALUES ('$ses_id', " . time()
                    . ", " . time() . ", '$data','$this->app_name',$ipnum)";

// echo "<br/>$session_sql<br/>";

        $session_res = @mysql_query ($session_sql);
        if (!$session_res)
        {
            return FALSE;
        }
        else
        {
            return TRUE;
        }
    }

    /* Destroy session record in database */
    function _destroy($ses_id)
    {
    //*********TESTING********
    $fwhandle4 = @fopen("test.txt","a");
if($fwhandle4)
{
if (fwrite($fwhandle4, "‘_destroy’ method called\n") === FALSE)
echo "Cannot write to file";

fclose($fwhandle4);
}
else
echo "Cannot open to write file";
    //*********CLOSE TESTING********

        $session_sql = "DELETE FROM " . $this->ses_table
                    . " WHERE ses_id = '$ses_id'";
        $session_res = @mysql_query ($session_sql);

        if (!$session_res)
        {
            return FALSE;
        }
        else
        {
            return TRUE;
        }
    }

    /* Garbage collection, deletes old sessions */
    function _gc($life) //ignore $life
    {
    //*********TESTING********
    $fwhandle5 = @fopen("test.txt","a");
if($fwhandle5)
{
if (fwrite($fwhandle5, "‘_gc’ method called\n") === FALSE)
echo "Cannot write to file";

fclose($fwhandle5);
}
else
echo "Cannot open to write file";
    //*********CLOSE TESTING********

    $strtim ="-". $this->ses_maxTime ." seconds";

        $ses_life = strtotime($strtim);
        $session_sql = "DELETE FROM " . $this->ses_table . " WHERE ses_time < $ses_life and app_name='$this->app_name'";
        echo "<br/>$session_sql<br/>";
        $session_res = @mysql_query ($session_sql);

        if (!$session_res)
            return FALSE;

//also see all sessions for max of 24 hours (default)
$strtim1 ="-". get_cfg_var("session.gc_maxlifetime") ." seconds";

$ses_life1 = strtotime($strtim1);
$session_sql1 = "DELETE FROM " . $this->ses_table . " WHERE ses_time < $ses_life1";
        $session_res1 = @mysql_query ($session_sql1);

        if (!$session_res1)
            return FALSE;
        else
            return TRUE;
    }
}
?>

[/code]
Link to comment
https://forums.phpfreaks.com/topic/15524-why-is-script-executing-twice/
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.