Solved

sessions and mysql problem

Posted on 2006-07-20
7
662 Views
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
thx
0
Comment
Question by:Johnny
  • 4
  • 2
7 Comments
 
LVL 14

Expert Comment

by:ThG
Comment Utility

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

http://www.experts-exchange.com/Web/Web_Languages/PHP/Q_20890175.html#10410041
0
 

Author Comment

by:Johnny
Comment Utility
hrrrm

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

i can get the files in session dir:
--------------------------
session_start();
 
/**
 * 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++;
    }
    closedir($handle);
 
    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:
First_Name|s:6:"Johnny";Player_Name|s:4:"Trox";

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?
0
 
LVL 40

Accepted Solution

by:
RQuadling earned 500 total points
Comment Utility
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.

0
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 14

Expert Comment

by:ThG
Comment Utility
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.
0
 

Author Comment

by:Johnny
Comment Utility
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
0
 

Author Comment

by:Johnny
Comment Utility
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
0
 

Author Comment

by:Johnny
Comment Utility
sorry forgot the new question as a result of the info i found with this one
http://www.experts-exchange.com/Web/Web_Languages/PHP/PHP_Databases/Q_21928014.html
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

763 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now