Jump to content


Photo

refreshing sessions...


  • Please log in to reply
4 replies to this topic

#1 scottybwoy

scottybwoy
  • Members
  • PipPipPip
  • Advanced Member
  • 532 posts
  • LocationUK

Posted 17 October 2006 - 04:29 PM

I have my sessions stored in a database and a custom handler that puts them in there, trouble is I thinks it's too slow for !isset($_SESSION['$myVar']) to find out if its there or not.  So when I refresh the page I always get the sql error as the primary key is the same so it won't enter the data.  But thats not what I want, I want it to look and see if the data is already there for that particular session key and if so not to bother trying to put it in there.  Any sugestions or tactics that you experienced types use?

Here's my sess handler :

<?php

    function sess_open($save_path, $session_name)
    {
		return true;
    }

    function sess_close()
    {
        return true;
    }

    function sess_read($key)
    {
		global $DEBUG, $SESS_LIFE;
		
		sessCon();

		$statement = "SELECT * FROM sessTbl WHERE sesskey = '$key' AND expiry > '" . time() . "'";
        $result = mssql_query($statement);
		echo "<br />" . msql_error() . "<br />\n";

        if ($DEBUG)
        {
           	echo "sess_read: $statement <br>result: $result<br>";
        }

        if ($result)
        {
			$row = mssql_fetch_assoc($result);
			return $row['value'];
		}

		return false;
	}

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

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

        sessCon();

        $statement = "INSERT INTO sessTbl VALUES ('$key', $expiry, '$value')";
        $result = mssql_query($statement) or die ('Query failed.');

        if ($DEBUG)
        {
        	echo "sess_write: $statement <br>result: $result<br>";
        }

        if (! $result)
        {
			$statement = "UPDATE sessTbl SET expiry = $expiry, value = '$value' WHERE sesskey = '$key' AND expiry > " . time();
            mssql_query($statement) or die ('Query failed.');
		}

        return $result;
	}

    function sess_destroy($key)
    {
    	global $DEBUG;
		sessCon();

        $statement = "DELETE FROM sessTbl WHERE sesskey = '$key'";
        $result = mssql_query($statement);
        if ($DEBUG)
        {
           	echo "sess_destroy: $statement <br>result: $result<br>";
        }

    	return $result;
	}

    function sess_gc($maxlifetime)
    {
    	global $DEBUG;
    	
        sessCon();

		$statement = "DELETE FROM sessTbl WHERE expiry < " . time();
        $qid = mssql_query($statement);
        if ($DEBUG)
        {
           	echo "sess_gc: $statement <br>result: $result<br>";
        }

        return 1;
	}

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

Here's my session data insert :
<?php
if (!isset($_SESSION["SESSION_USER_ID"]) || ($_SESSION["SESSION_USER_ID"] == ""))
			{
				$_SESSION["SESSION_USER_ID"] = $user_id;
				$_SESSION["SESSION_USERNAME"] = $username;
			}
?>
BUt it doesn't work :(

#2 scottybwoy

scottybwoy
  • Members
  • PipPipPip
  • Advanced Member
  • 532 posts
  • LocationUK

Posted 18 October 2006 - 08:30 AM

Basically what I'd like is to know what to use to get my sess_read(key) function to check the database first, but I don't know what php function invokes it.  So far standard php session calls, invoke the custom session handler.  So can someone point me in the right direction?

#3 scottybwoy

scottybwoy
  • Members
  • PipPipPip
  • Advanced Member
  • 532 posts
  • LocationUK

Posted 23 October 2006 - 04:21 PM

my sessions are stored in a table like so :

sesskey
expiry
value

and i have a user table in a seperate db with these columns :

userId
uName
uLName
active
level
...and some others

How would you competant lot suggest I get my script to check the session data before trying to insert it again, if a user presses refresh?

Surely someone has had to get around this before??

#4 scottybwoy

scottybwoy
  • Members
  • PipPipPip
  • Advanced Member
  • 532 posts
  • LocationUK

Posted 24 October 2006 - 09:22 AM

Probe, morning all ;/  This is my login script :

<?php

function login()
{
	$this->authorise($this->getUserName());

	if ($this->authorise->success = TRUE)
	{
		global $username, $user_id;
			
		session_start();

		$username = $this->getUserName();
		$user_id = $this->getUserId($username);
			
		if (!isset($_SESSION["SESSION_USER_ID"]) || ($_SESSION["SESSION_USER_ID"] == ""))
		{
			$_SESSION["SESSION_USER_ID"] = $user_id;
			$_SESSION["SESSION_USERNAME"] = $username;
		}

	} else {
		die ("Get Out!");
	}

}
?>

Which is called by the child class in home.php.  But the second if clause unables it to work.  Initially I did not have the If clause but then realised I need one encase the user refreshes.  So what would you suggest?

#5 scottybwoy

scottybwoy
  • Members
  • PipPipPip
  • Advanced Member
  • 532 posts
  • LocationUK

Posted 25 October 2006 - 11:26 AM

OK Today this script decided not to give me an error, however it now inserts a new entry into the session table.  Say if further on into the Intranet, the user decides to Refresh, will this clear their data? As it would make them a new session Id.  I thaught I understood these sessions and now I'm begining to get confused.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users