Jump to content

Session Handler


shadowayex

Recommended Posts

I'm using a session handler that justsomeguy built to try to keep track of users on my site. When I try to use a $_SESSION variable to display a username, I get nothing. The session handler looks like this:

<?php/*#############################################################################Session table format:  id - varchar(40), not null, primary key  content - text, null  timestamp - int, not null#############################################################################*/define("SESS_DB_HOST", "---------------"); #database serverdefine("SESS_DB_USER", "---------------"); #database userdefine("SESS_DB_PASS", "---------------"); #database passworddefine("SESS_DB_NAME", "---------------"); #database namedefine("SESS_DB_TABLE", "---------------"); #session table name (see above for the required fields)global $SESS_DB_CON;$SESS_DB_CON = mysql_connect(SESS_DB_HOST, SESS_DB_USER, SESS_DB_PASS);mysql_select_db(SESS_DB_NAME, $SESS_DB_CON);function custom_session_open($save_path, $session_name){  return(true);}function custom_session_close(){  #do garbage collection  return custom_session_gc(get_cfg_var("session.gc_maxlifetime"));}function custom_session_read($id){  global $SESS_DB_CON;  $result = @mysql_query("SELECT content FROM " . SESS_DB_TABLE . " WHERE id='" . mysql_real_escape_string($id, $SESS_DB_CON) . "'", $SESS_DB_CON);  if ($row = @mysql_fetch_assoc($result))  {	@mysql_query("UPDATE " . SESS_DB_TABLE . " SET timestamp=" . time() . " WHERE id='" . mysql_real_escape_string($id, $SESS_DB_CON) . "'", $SESS_DB_CON);	return((string)$row['content']);  }  return("");}function custom_session_write($id, $sess_data){  global $SESS_DB_CON;  $result = @mysql_query("SELECT COUNT(*) AS nr FROM " . SESS_DB_TABLE . " WHERE id='" . mysql_real_escape_string($id, $SESS_DB_CON) . "'", $SESS_DB_CON);  $count = @mysql_fetch_assoc($result);  if ($count['nr'] > 0)  {	$sql = "UPDATE " . SESS_DB_TABLE;	$sql .= " SET content='" . mysql_real_escape_string($sess_data, $SESS_DB_CON) . "'";	$sql .= ", timestamp=" . time();	$sql .= " WHERE id='" . mysql_real_escape_string($id, $SESS_DB_CON) . "'";	@mysql_query($sql, $SESS_DB_CON);  }  else  {	$sql = "INSERT INTO " . SESS_DB_TABLE;	$sql .= " (id, content, timestamp)";	$sql .= " VALUES ";	$sql .= "('" . mysql_real_escape_string($id, $SESS_DB_CON) . "',";	$sql .= " '" . mysql_real_escape_string($sess_data, $SESS_DB_CON) . "', ";	$sql .= time() . ")";	@mysql_query($sql, $SESS_DB_CON);  }  return true;}function custom_session_destroy($id){  global $SESS_DB_CON;  @mysql_query("DELETE FROM " . SESS_DB_TABLE . " WHERE id='" . mysql_real_escape_string($id, $SESS_DB_CON) . "'", $SESS_DB_CON);  return(true);}function custom_session_gc($maxlifetime){  global $SESS_DB_CON;  $cur = time();  $exp = $cur - $maxlifetime; # this assumes that $maxlifetime is in seconds  @mysql_query("DELETE FROM " . SESS_DB_TABLE . " WHERE timestamp < {$exp}", $SESS_DB_CON);  return(true);}function get_user_count($mins){  global $SESS_DB_CON;    $cur = time();  $limit = $cur - ($mins * 60);  $result = @mysql_query("SELECT COUNT(*) AS nr FROM " . SESS_DB_TABLE . " WHERE timestamp >= {$limit}", $SESS_DB_CON);  $row = @mysql_fetch_assoc($result);  return $row['nr']; }session_set_save_handler("custom_session_open",						 "custom_session_close",						 "custom_session_read",						 "custom_session_write",						 "custom_session_destroy",						 "custom_session_gc");session_start();$_SESSION['1'] = 1;?>

The page trying to display looks like this:

<?php include("sessions.php");include("connect.php");$user=$_SESSION['user'];?>  <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"><head><meta http-equiv="Content-Type" content="text/html;charset=utf-8" /><title>Welcome <?php echo $user; ?></title><style type="text/css">#menu1 a {color:black;background-color:white;text-decoration:none;text-indent:1ex;}#menu1 a:active {color:black;text-decoration:none;}#menu1 a:hover {color:black;background-color:#FFFF99}#menu1 a:visited {color:black;text-decoration:none;}</style><script src="mmenu.js" type="text/javascript"></script><script src="menuItems.js.php" type="text/javascript"></script></head><body><div>Hello <?php echo $user; ?><br /></body></html>

The $_SESSION['user'] was set on the login page. It was set as whatever the user logged in at. If I remove the include("sessions.php") and replace it with session_start() Then the page will display the username of whoever logged in. But this I can't keep track of who is on and who is not. What am I doing wrong?

Link to comment
Share on other sites

Well, when I go to set a session, for example $_SESSION['logged_in'] = true;, it evidently doesn't set it. Because I get the following error when I try to call it:Undefined index: logged_in in file & line information

Link to comment
Share on other sites

If it's not setting it then obviously there's a problem. Use the error code on every page, in fact put it in the top of the session include file to make sure that all error messages are being printed, and try to log in. As you're logging in look at the records in the database for the session, after you log in there should be a record with the information in it that you just saved (in the content field). If you want to verify what the session handlers are doing then add some print statements to them so that each time one runs it prints out what it was sent. You won't be able to redirect, but at least you can see what the session handlers are doing.Also - make sure that content is a text field, and not a blob.

Link to comment
Share on other sites

It is a text field. Does collation matter? it's utf8_bin right now. And the content field (which is a text type) says BLOB - 0 Bytes. I can take a screenshot of it if you need verification. And all my pages are headed with a statement that checks if $_SESSION['logged_in'] == true; and if it's not, it makes the user try logging in again. That's what it does. And the code spits out the same error, saying that logged_in is undefined. If I take my check out and just let it load the page, it says user is undefined. Could my hosting site be stopping the handler from working properly?EDIT: I can see what's in the content field if I quick Change. It's empty.

Link to comment
Share on other sites

Put echo statements into each of the session handler functions to make sure that they are being called. See what gets printed out after you do that.
I put in the echo statements. These are the results:custom_session_open is being called.custom_session_read is being called.Warning: session_start(): Cannot send session cache limiter - headers already sent (output started at /home/www/testtools.freehostia.com/sessions.php:23) in /home/www/testtools.freehostia.com/sessions.php on line 114Notice: Undefined index: logged_in in /home/www/testtools.freehostia.com/home.php on line 5custom_session_write is being called and is writing 44691af3beaef2b5126363086bba7691 and . After the and was supposed to be whatever was in the $sess_data variable. Evidently nothing.custom_session_close is being called.custom_session_gc is being called.In that order.
Link to comment
Share on other sites

Remove this line at the end:$_SESSION['1'] = 1;I'm not sure why, but that line seems to be causing problems.
That did it. It rights now. Now, how do I use this to make a list of who's online and who's all viewing a certain page :). Sorry, I'm a pain. But as soon as I get that, then I should be good. Oh and also I need to have someting to check if a certain user is online.
Link to comment
Share on other sites

That doesn't track who is online and who is viewing a certain page, it keeps track of how many people were active over the last X minutes. If you want to keep track of who those people are and which page they are viewing then you'll have to record that information yourself. When someone loads a page make a note of which page and who they are. You can save their session ID if you want to link them up to their session.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...