[Webinar] Streamline your web hosting managementRegister Today

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 680
  • Last Modified:

sessions and mysql problem

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
  • 4
  • 2
1 Solution

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

JohnnyAuthor Commented:

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?
Richard QuadlingSenior Software DeveloperCommented:
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 http://www.php.net/manual/en/function.session-set-save-handler.php give some good examples of this.

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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.
JohnnyAuthor Commented:
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
JohnnyAuthor Commented:
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
JohnnyAuthor Commented:
sorry forgot the new question as a result of the info i found with this one

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now