I'm building a website for me and my friends that should include a forum and a chat. I'm developing the list of the friend but I don't know how to display wich users are online and which not. The users are autenticated with sessions variable. To be able to check wich users have a session open with the autentication variable $_SESSION I created a session handler that store the session datas in the database. In the table where I store the session I added a field that for default is null and should be filled by the login script with the user id so than I can check which users are online. Unfortunatly the login script work but doesn't fill the field. no error is displayed (I have all the errors activated).
I wanted to know if there is any easyer way to check which user is online. I know just PHP language and not much of it (I can't understand the object oriented PHP).
Thanks

Dani AI

Generated

Quick summary for : two things are far more likely than a mysterious MySQL bug — a wrong variable and a timing issue with when the session row actually exists in the DB. was right to flag the SQL style (use of quoted numbers), but the real blocker is what value you update and when you do it.

Most likely causes

  • The login code updates the sessions table with the wrong session variable (you set $_SESSION['auth'] but you intend the user id).
  • The session storage handler only writes the sessions row when PHP saves the session (normally at script end). If you run an UPDATE earlier, the row might not exist yet and the query simply affects 0 rows (no SQL error).

Practical fixes

  • Ensure you update with the actual user id (e.g. $_SESSION['user_id']) and avoid quoting integers directly: use intval() when building the query.
  • Better: set the user id into $_SESSION and force PHP to save the session before you touch the sessions table. Example sequence:
$_SESSION['user_id'] = $login_id;
session_regenerate_id(true);   // recommended for fixation protection
$sid = session_id();
session_write_close();         // forces the session handler to write the row
mysql_query("UPDATE sessions SET sessions_user_id = " . intval($login_id) . " WHERE sessions_id = '" . mysql_real_escape_string($sid) . "'");

Alternatives and further tips

  • Simpler and faster for an “online users” list: update a small user_online table or a last_activity column in your users table on every page/AJAX ping, and show users with recent timestamps. This avoids parsing serialized session blobs.
  • Check your sessions_id column length (PHP session ID length can vary) and avoid deleting rows just because sessions_value='' in your garbage collector.
  • Move away from old mysql_* calls: use MySQLi or PDO with prepared statements, hash passwords (password_hash/password_verify), and secure your session cookies.

This addresses ’s request for actionable code and follows up on ’s observation while explaining the timing/logic root cause.

hi ....i am fresher in php...i am also working on creation of forums..can u send me ur entire code so that it will be very helpful for me and i will also look at ur problem and try my best to solve it.
thanks
navvy.

Your doing it the correct way. Post your code and mabey we can fix it for ya.

The code of the session handler is

<?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");
?>

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
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: ");
exit();
} else {
header("Location: ");
exit();
}
?>

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)) 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

session_user_id is an Integer and you have qoute around it in the SQL query. That could be the problem.

It's not showing in error because you have it redirecting to a new page immeadiately.

And don't worry about destroying the session if the user isn't logged in. It won't slow the pages down(at least not noticebly) but it's not worth it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.