Go Back   PCMech Forums > Help & Discussion > Web Design / Development

Need Some Help? Type Your Keywords Here:

Reply
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
Old 08-20-2005, 04:03 PM   #1
Chop Chop
 
catacon's Avatar
 
Join Date: Jan 2005
Location: St. Louis, MO
Posts: 1,035
Send a message via AIM to catacon Send a message via Yahoo to catacon
PHP Questions

These are in addition to the time question in my other thread (still have not figured that out).

--Login Stuff--

How do I create an effective "Remember Me" selection on the login form?

I have a field in the user database called "loggedin" which is set to "1" when the user logs in and is set to "0" when the user logs out. However, if the user closes the browser and is no longer at the site, it still says the user is logged in. How do I keep the user from being on the "Logged In" section all the time.

Thanks
catacon is offline   Reply With Quote
Old 08-21-2005, 10:25 PM   #2
Come in Ray...
 
faulkner132's Avatar
 
Join Date: Sep 2004
Posts: 1,668
If you are tracking this in a database, really the more effective method may be to update a time when they last performed an activity. This way if they haven't done something for X amount of time you can assume they have logged off.

Not 100% effective, but it should be good enough.
faulkner132 is offline   Reply With Quote
Old 08-22-2005, 07:45 PM   #3
Chop Chop
 
catacon's Avatar
 
Join Date: Jan 2005
Location: St. Louis, MO
Posts: 1,035
Send a message via AIM to catacon Send a message via Yahoo to catacon
So whenever the user makes a post, or sends a PM or changes their theme, I make the last_activity field = NOW(), right? How do I check to see if the last_acticity field is equal to of greater than the target time when I assume they have logged out?

Also, how do I make the remember me script?

Thanks a ton!
catacon is offline   Reply With Quote
Old 08-23-2005, 07:37 AM   #4
Come in Ray...
 
faulkner132's Avatar
 
Join Date: Sep 2004
Posts: 1,668
Right, whenever anything is done, you update a field in your database to the current time. Then when you need to see if they are logged in, you can compare the current time to the time they have logged in the database.

Remember me is usually handled through cookies. Check out php's setcookie function. I usually use 30 days as the default remember me time which would be (time()+60*60*24*30)
faulkner132 is offline   Reply With Quote
Old 08-24-2005, 04:30 PM   #5
Chop Chop
 
catacon's Avatar
 
Join Date: Jan 2005
Location: St. Louis, MO
Posts: 1,035
Send a message via AIM to catacon Send a message via Yahoo to catacon
OK, I got the remember me to work. I just needed to check to see if the cookie was set at the top of each page.

How do I check the users last_activity field against the current time? I'm kind of clueless about that. Also, is there any way to update the last_activity field when the user visits a page? Instead of having it everytime they do something. Because, they could still be logged in, but not be posting (or whatever). Would it be like this at the top of each page:

PHP Code:

if (isset($_SESSION['user_id'])) {
     
$query "UPDATE users SET last_activity=NOW() WHERE user_id={$_SESSION['user_id']}"
I will try that and see what happens. Thanks!
catacon is offline   Reply With Quote
Old 08-25-2005, 07:22 AM   #6
Come in Ray...
 
faulkner132's Avatar
 
Join Date: Sep 2004
Posts: 1,668
Yes, you would have a query like that at the top of every page you want to track actions for. To get the time difference, query the DB for your last_activity field and subtract it from the current time.

Since you are storing the last_activity as a date/time, you will need to use the strtotime function. Your statement would look like:

$lapse = time() - strtotime($rs_row->last_activity)
faulkner132 is offline   Reply With Quote
Old 08-25-2005, 04:02 PM   #7
Chop Chop
 
catacon's Avatar
 
Join Date: Jan 2005
Location: St. Louis, MO
Posts: 1,035
Send a message via AIM to catacon Send a message via Yahoo to catacon
I got the last_activity to be set correctely. Thanks.

How do I check the current time against a certain user?

PHP Code:

if (isset($_COOKIE['cookuser'])) {
     
$query "SELECT TIME_FORMAT(last_activity, '%r') FROM users WHERE user_id={$_COOKIE[cookuser]}";
     
$result = @mysql_query ($query);
     if (
$result) {
        
$row mysql_fetch_array ($resultMYSQL_NUM);

        
$lapse time() - strotime($row['0']);
        if (
$lapse time()) {
           
$query "UPDATE users SET loggedin=0 WHERE user_id={$_COOKIE[cookuser]}";
           
$result = @mysql_query ($query);
       }
     }

Just kind of a guess there. I will try that and see what happens.

Thanks!
catacon is offline   Reply With Quote
Old 08-29-2005, 08:48 AM   #8
Chop Chop
 
catacon's Avatar
 
Join Date: Jan 2005
Location: St. Louis, MO
Posts: 1,035
Send a message via AIM to catacon Send a message via Yahoo to catacon
OK, that didn't work. I'm confused. What do I do after I find the $lapse?

Thanks!
catacon is offline   Reply With Quote
Old 08-29-2005, 09:00 AM   #9
Member (9 bit)
 
Join Date: May 2003
Posts: 420
Send a message via AIM to juicelooser
You should get rid of the loggedin field in your database. Once the user leaves the site, because of the way PHP works, there is no way to change anything in the database to reflect that, unless you wrote a script that runs every few minutes and checks every single user in the database.

You need to decide on a certain amount of time of inactivity that results in a user being marked as "inactive." Then, if you need to get a list of logged out users, you just do a MySQL query searching for all users with a last_activity time of more than x minutes ago.
juicelooser is offline   Reply With Quote
Old 08-29-2005, 10:00 AM   #10
Come in Ray...
 
faulkner132's Avatar
 
Join Date: Sep 2004
Posts: 1,668
catacon,

There is no way this will ever be true:
$lapse = time()

$lapse is intended to be (current time - last activity). Also, I was making the assumption that last_activity in your database is a Date/Time field and you are pulling it from the database using a query such as:

SELECT last_activity FROM users WHERE id=$user_id
(not using any MySQL date/time formatting functions)

This gives you the information in a format the php function strtotime() can recognize.

Your if statement should be something like:

if ($lapse > [some amount of time in ms])
faulkner132 is offline   Reply With Quote
Old 09-01-2005, 07:53 PM   #11
Chop Chop
 
catacon's Avatar
 
Join Date: Jan 2005
Location: St. Louis, MO
Posts: 1,035
Send a message via AIM to catacon Send a message via Yahoo to catacon
Quote:
Originally Posted by juicelooser
You should get rid of the loggedin field in your database. Once the user leaves the site, because of the way PHP works, there is no way to change anything in the database to reflect that, unless you wrote a script that runs every few minutes and checks every single user in the database.

You need to decide on a certain amount of time of inactivity that results in a user being marked as "inactive." Then, if you need to get a list of logged out users, you just do a MySQL query searching for all users with a last_activity time of more than x minutes ago.
What would that query look like?
catacon is offline   Reply With Quote
Old 09-01-2005, 09:24 PM   #12
Member (9 bit)
 
Join Date: May 2003
Posts: 420
Send a message via AIM to juicelooser
Hmmm.

Something like:

SELECT * FROM users WHERE last_activity < 300000

Would find all users that were active in the last 5 minutes (300000 is 5 minutes in milliseconds). I haven't had any time to test this or anything, but I'm pretty sure that's what you're looking for.
juicelooser is offline   Reply With Quote
Old 09-04-2005, 04:40 PM   #13
Chop Chop
 
catacon's Avatar
 
Join Date: Jan 2005
Location: St. Louis, MO
Posts: 1,035
Send a message via AIM to catacon Send a message via Yahoo to catacon
Isn't 5 minutes 30000 in milliseconds? 5m * 60s * 100ms = 30000? Also, what should last_activity be, datetime, time, etc.? Thanks!
catacon is offline   Reply With Quote
Old 09-04-2005, 05:32 PM   #14
Member (9 bit)
 
Join Date: May 2003
Posts: 420
Send a message via AIM to juicelooser
Urrgh, I wrote that query wrong...

It *should* be SELECT * FROM users WHERE (curtime() - last_activity) < 300000

And according to Google, 5 minutes in 300000 milliseconds. You should set last_activity as a "time," I believe--I seem to have forgotten which types to use in MySQL, hopefully someone else can help with that.
juicelooser is offline   Reply With Quote
Old 09-12-2005, 03:47 PM   #15
Chop Chop
 
catacon's Avatar
 
Join Date: Jan 2005
Location: St. Louis, MO
Posts: 1,035
Send a message via AIM to catacon Send a message via Yahoo to catacon
Still having a problem. Whenever I call the check_active function:

PHP Code:

function check_active() {

if (isset(
$_SESSION['user_id'])) {
     
$query "SELECT last_activity, loggedin FROM users WHERE loggedin='1'";
     
$result = @mysql_query ($query);
     if (
$result) { //worked
         
$row = @mysql_fetch_array ($result);
         if (
curtime () - $row['last_activity'] > 300000) {
              
$query "UPDATE users SET loggedin='0'";
         }
     }
}


I doesn't seem to work if I don't connect to mysql. If I do, it just goes to a blank screen. I have the function in a seperate file, and if I connect in the file or in the header file (different file) it does the same thing. What am I doing wrong? Thanks!
catacon is offline   Reply With Quote
Reply

Bookmarks

Still Need Help? Type Your Keywords Here:


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is On
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -5. The time now is 04:56 AM.
Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2012, vBulletin Solutions, Inc.
SEO by vBSEO 3.6.0 PL2