Jump to content

refreshing sessions...


scottybwoy

Recommended Posts

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 :

[code]
<?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");
?>
[/code]

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

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?
Link to comment
Share on other sites

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??
Link to comment
Share on other sites

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

[code]
<?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!");
}

}
?>
[/code]

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?
Link to comment
Share on other sites

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.
Link to comment
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.