scottybwoy Posted October 17, 2006 Share Posted October 17, 2006 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]<?phpif (!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 :( Quote Link to comment Share on other sites More sharing options...
scottybwoy Posted October 18, 2006 Author Share Posted October 18, 2006 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? Quote Link to comment Share on other sites More sharing options...
scottybwoy Posted October 23, 2006 Author Share Posted October 23, 2006 my sessions are stored in a table like so :sesskeyexpiryvalueand i have a user table in a seperate db with these columns :userIduNameuLNameactivelevel...and some othersHow 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?? Quote Link to comment Share on other sites More sharing options...
scottybwoy Posted October 24, 2006 Author Share Posted October 24, 2006 Probe, morning all ;/ This is my login script :[code]<?phpfunction 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? Quote Link to comment Share on other sites More sharing options...
scottybwoy Posted October 25, 2006 Author Share Posted October 25, 2006 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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.