The code of the session handler is
[php]<?php
// Create a connection to the database
function mysql_session_open($session_path, $session_name) {
global $conn;
$conn=mysql_pconnect("localhost", "admin", "******") or die (mysql_error());
mysql_select_db("pantarei") or die (mysql_error());
return true;
}
// Doesn't do anything
function mysql_session_close() {
global $conn;
mysql_close($conn);
return true;
}
// Select the data from the database
function mysql_session_select($SID) {
global $conn;
$query = "
SELECT sessions_value FROM sessions
WHERE sessions_id = '".$SID."' AND sessions_expiration > ". time();
$result = mysql_query($query, $conn);
if (mysql_num_rows($result)==1) {
$row = mysql_fetch_assoc($result);
$value = $row['sessions_value'];
return $value;
} else {
return "";
}
}
// Write or update the data
function mysql_session_write($SID, $value) {
global $conn;
$lifetime = get_cfg_var("session.gc_maxlifetime");
$expiration = time() + $lifetime;
$query = "
INSERT INTO sessions (sessions_id, sessions_expiration, sessions_value)
VALUES('".$SID."', '".$expiration."', '".mysql_real_escape_string($value)."')";
$result = @mysql_query($query, $conn);
if (!$result) {
$query = "
UPDATE sessions SET
sessions_expiration = '".$expiration."',
sessions_value = '".mysql_real_escape_string($value)."' WHERE
sessions_id = '".$SID."' AND sessions_expiration >". time();
$result = mysql_query($query, $conn);
}
}
// Destroy the session
function mysql_session_destroy($SID) {
global $conn;
$query = "
DELETE FROM sessions
WHERE sessions_id = '".$SID."'";
$result = mysql_query($query, $conn);
}
// Automaticaly destroy expirated sessions
function mysql_session_garbage_collect($lifetime) {
global $conn;
$query = "
DELETE FROM sessions
WHERE sessions_expiration < ".time()." OR sessions_value=''";
$result = mysql_query($query, $conn);
return @mysql_affected_rows($result);
}
// Create the session handler
session_set_save_handler("mysql_session_open", "mysql_session_close", "mysql_session_select", "mysql_session_write", "mysql_session_destroy", "mysql_session_garbage_collect");
?>[/php]
The query that create the table to store the sessions is
CREATE TABLE sessions
(sessions_id VARCHAR(32) NOT NULL,
sessions_user_id INT(10) UNSIGNED,
sessions_expiration INT NOT NULL,
sessions_value TEXT NOT NULL,
PRIMARY KEY(sessions_id));
The session handler seems to work fine, the problem is in the login script, it should write in the column sessions_user_id the id of the user that just logged in or return an error but it doesn't work and it doesn't return any error.
Here is the script
[php]<?php
include "session_handler.php";
session_start();
include "connection.php";
$usernm=$_POST['user'];
$passw=$_POST['pass'];
$query_select="SELECT * FROM login WHERE login_user='$usernm' AND login_pass='$passw'";
$query_result=mysql_query($query_select, $conn) or die (mysql_error());
if (mysql_num_rows($query_result)!="0") {
$resarr=mysql_fetch_assoc($query_result) or die (mysql_error());
extract($resarr);
$_SESSION['auth']=$login_auth;
$_SESSION['user_id']=$login_id;
$query_sess="
UPDATE sessions
SET sessions_user_id='".$_SESSION['auth']."'
WHERE sessions_id='".mysql_real_escape_string(session_id())."'";
$res=mysql_query($query_sess, $conn) or die (mysql_error());
header("Location: http://***.***.*.*/mypage.php");
exit();
} else {
header("Location: http://***.***.*.*/error.php?error=login");
exit();
}
?>[/php]
If I run the query to set the user id in the column sessions_user_id on mysql query browser sometimes it works, sometimes get me an error that say to check the correct sintax to use after... and it print the session id string WITHOUT the last character. I have no idea what can be the cause of that.
I have also another question: I setted up all the web pages to delete the session started in the beginning if the user is not logged in with a simple if (!isset($_SESSION['auth'])) session_destroy();
The purpose of this is keep the less numbers of rows in the sessions table but then I thought that that might slow down all the pages because require one more query to execute every time.
Do you think I shoul keep this or change it?
Thanks