There are several ways to do this best, in my opinion, based on security: NOTE: THIS REALLY CONTINUES .... I know that the syntax is wrong, it's just for you to get an idea.
$con = mysql_connect("localhost","sampleuser","samplepass"); if (!$con) { $error = "Could not connect to server"; } mysql_select_db("sampledb", $con); $result = mysql_query("SELECT * FROM `sampletable` WHERE `username`='".$_SESSION['user_id']."'"); $userdeets = mysql_fetch_array($result); if($_SESSION['sessionvalue'] != $userdeets['sessionvalue']) { session_destroy(); Header('Location: logout.php'); } else { $result2 = mysql_query("UPDATE `sessionvalue` WHERE `username`='".$_SESSION['user_id']."' SET `sessionvalue` = RANDOMVALUE''"); $sesval = mysql_fetch_array($result2); $_SESSION['sessionvalue'] = $seshval }
Now I know that this is not the code, but essentially what you need to do to be safe and have this ability:
- Each time a page load checks the session value, it corresponds to the value in the database.
- Each time a page load sets a new session value based on a randomly generated DB value. You will also need to save the username in the session.
- If the session ID does not match, you destroy the session and redirect it.
- if it matches your new session id.
if you want to deny the user, you can set the value of sessionvalue in the database to a value similar to "BANNED". this value will not allow them to log in. that way, you can control the user through a simple web form, and you can also easily generate a list of banned users, etc. etc. I wish I had more time to explain this. I hope this helps.
mikjryan
source share