sessions and mysql problem

Posted on 2006-07-20
Last Modified: 2013-12-12
i have a session logon that works fine

i can save the info to mysql just fine and when a member uses the logout it takes the info out just fine.

what im looking to find out is how can i detect if a session expired(a member closed the browser without loging out)
and delete the nessary mysql info.

how can i do this please
if you need any code or have any questions by all means ask please
Question by:Johnny
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
LVL 14

Expert Comment

ID: 17151096

this is a FAQ. you can not detect when sesssion expires. Try looking to the following PAQ if that helps.

Author Comment

ID: 17151195

i have a few ideas but i was hoping for a better one...

i can get the files in session dir:
 * Calculate the number of users online by
 * counting the number of session files. Returns the
 * number of users on success, or -1 on failure.
function getUsersOnline() {
    $count = 0;
    $handle = opendir(session_save_path());
    if ($handle == false) return -1;
    while (($file = readdir($handle)) != false) {
        if (ereg("^sess", $file)) $count++;
    return $count;
echo "users online = " . getUsersOnline();
by changeing the above code to look inside the files for below info
useing session_save_path() and insted of counting the files look in them

inside session file:

now useing the above code couldnt someone look inside the files and see if one maches the user in htis case "Trox"
if it finds it it does not change the mysql info if it does not it changes it as a mantenance thing first at lets say when a page is opened(php page)

this would in affect garbage collection for if a users is curently online or not...and has a session or not
im not advanced enoph to understand how to do this, any help would be really nice please

wouldnt this idea work?
LVL 40

Accepted Solution

Richard Quadling earned 500 total points
ID: 17152344
If you are using a session class, then you shouldn't need to do this.

Sessions expire after a certain amount of time. Garbage collection will automatically deal with them.

Personally, I would use the session_set_save_handler() function to register the class dealing with the sessions.

The user notes on give some good examples of this.

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

LVL 14

Expert Comment

ID: 17152851
just as a side note, from PHP manual:

> Note: If you are using the default file-based session handler, your filesystem must keep track of access times (atime). Windows FAT does not so you will have to come up with another way to handle garbage collecting your session if you are stuck with a FAT filesystem or any other filesystem where atime tracking is not available. Since PHP 4.2.3 it has used mtime (modified date) instead of atime. So, you won't have problems with filesystems where atime tracking is not available.

this means that you are messing with session files you must be sure not to open them in write mode, but only in "r" mode otherwise garbage collection won't work anymore.

Author Comment

ID: 17154034
some how i think we got really lost on what im looking to do...

let me start off with saying i have windows xp ntfs files system with php 5.05

now heres what i did and need.

i made a logon system to lock pages for a memebership, this is bery basic (LOW security stuff) just a games spoilers.

so i made it logon at that time its sets sessons of players name and password then it saves msql info of is a person login to the web site(flips a varible to 1 for on and 0 for logoff) and saves the time this was done so we know how long they have been on the site.
 so ive came up witha  prrblem of changing this info if a user is not on the web site anymore or has closed the browser. i have set my sessions to garbage collect the files everytime the sesions are fired so next time a user comes in it will be actrute(as so far we have only 6 members and will most lilly not have more then 25).

so i need to be able to change the mysql var to know the user has loged off. i saw you can know where the files are for the sesions so why can we read them via php and see if a users inside of it.. if it is it leaves the file alone and if there not there it re ajusts the mysql varible, thus makingit a live whos online and accurate too.

theres gotta be some way alot of bbs(or fourms) have whos online(with in 5 mins) this would be a bit more acurate tho this way would it not.

the getUsersOnline() function seams to COUNT the files in the session directory. so why cant we then know that we open this file and look for the players name then use that to search the mysql db tables for the info we need to adjust or not.
i just dont know enoph about php to do this..the proper code. (when ive asked questins here ive been amazed that its completly diffrent code then what i evan thought it be.. due to speed and other things in side of codeing. but in this case im looking for jUST to be done code. but again i havent a clue on that at all here)
thanks for any help provided

Author Comment

ID: 17158273
ok i have changed this to a mysql session handler

i still have the problem of updateing my mysql data to reflect the change of logout..

so i need to see if the users inthe session info still and if not update the info in logon info tables...

please see this ee question maybe you can ll help fix this problem

for now im going to give RQuadling  and a grade of B (as my question WAS NOT handled and not answered correctly at all
i was looking to update mysql info with finding a way to detect the expired session..i can do this now in mysql session handler(got the right path to find that info from RQuadling) again grade B was due to i did come up with a way to do this and no one answered it full..and insted of asking for a refund(or to close this question) i gave the points to RQuadling
i hate asking for a refund or close question(should be a way to automate this with a check box EE if your listening)
thanks for the help

Author Comment

ID: 17158283
sorry forgot the new question as a result of the info i found with this one

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

These days socially coordinated efforts have turned into a critical requirement for enterprises.
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

756 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question