Bumping Inactive Users

Bump users who are logged in and are inactive

Page 1 of 1

7 Replies - 1655 Views - Last Post: 13 July 2009 - 12:19 AM Rate Topic: -----

#1 Shiznit  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 67
  • Joined: 14-September 07

Bumping Inactive Users

Posted 11 July 2009 - 08:15 PM

Is there a way that I can bump users who are logged into my site if they've been inactive for more than an hour or so? One of the functions on the site is currently tied to whether or not a user is logged in - players are required to be logged in for their resources to increase

I'm currently only using simple session variables that get set when the user logs in, but I'm guessing that maybe I need to use cookies? I don't have alot of experience with either so any help is greatly appreciated.

Please let me know if there's any code I need to provide

Is This A Good Question/Topic? 0
  • +

Replies To: Bumping Inactive Users

#2 Crimson Wings  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 13
  • View blog
  • Posts: 215
  • Joined: 11-July 09

Re: Bumping Inactive Users

Posted 12 July 2009 - 12:46 AM

Hi.

There is one thing you can do in *UNIX servers and with PHP. It's called a cron job. a Cron job is a command that runs a PHP script every set of time. I think there is some sort of controller to do so in Windows servers, but its worth checking out if you are interested.

Personally, I haven't used cron jobs yet, but I will with my site's features.
Was This Post Helpful? 0
  • +
  • -

#3 JBrace1990  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 112
  • View blog
  • Posts: 760
  • Joined: 09-March 08

Re: Bumping Inactive Users

Posted 12 July 2009 - 08:29 AM

On each of your pages, add some code that checks the current time() against the time() in the database.

Update the time in the database whenever the player clicks something IF, and only if, he's not inactive. then in your resources, if the time in the database from his last click is greater than you want it to be (probably 5 or 10 minutes?), then he doesn't get resources.
Was This Post Helpful? 0
  • +
  • -

#4 Shiznit  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 67
  • Joined: 14-September 07

Re: Bumping Inactive Users

Posted 12 July 2009 - 03:03 PM

JBrace1990...

Your response definitely sounds like what I need. Unfortunately, I'm unsure of how to proceed. In my login script I have the following:

//after the user's information is verified, set session variables
$_SESSION['uid'] = $user_row['id'];
$_SESSION['logged_in'] = 1;
$_SESSION['name'] = $user_row['username'];
$_SESSION['ip'] = $_SERVER['REMOTE_ADDR'];
$_SESSION['user_type'] = $user_row['type_id'];
$_SESSION['timeout'] = time();

//insert user into log table					  
$id = $_SESSION['uid'];
$logged_in = $_SESSION['logged_in'];
$name = $_SESSION['name'];
$ip = $_SESSION['ip'];
$login_query = "INSERT INTO user_log (user_id,ip,logged_in) VALUES ('$id','$ip',NOW())";
$login_result = mysql_query($login_query) or die (mysql_error());
									
//update user record with new login info
$user_update_query = "UPDATE users SET `last_login`=NOW(),`last_ip`='".$ip."', `active`=1 WHERE id='".$id."'";
$user_update_result = mysql_query($user_update_query) or die (mysql_error());

//if everything is successful, send user to main member page
header("Location: ../member_index.php");



As you can see, I have set a $_SESSION['timeout']

In index.php I have added the following check:

//check to see if $_SESSION['timeout'] is set
if (isset($_SESSION['timeout']))
  {
	$inactive = 3600; //60 minutes
	$session_life = time()-$_SESSION['timeout'];
	if ($session_life>$inactive)
		  {
		session_destroy();
		header ("Location: index.php?msg=7");
		$logout_query = "DELETE FROM user_log WHERE user_id=".$_SESSION['uid']."";
		$logout_result = mysql_query($logout_query) or die (mysql_error());
	  }
  }



I've also added a meta-refresh on index.php to refresh at 3605 (index.php is the container file for all content, including member_index.php)

Am I even close to what it is that I'm trying to achieve?

Thanks!



View PostJBrace1990, on 12 Jul, 2009 - 07:29 AM, said:

Update the time in the database whenever the player clicks something IF, and only if, he's not inactive.


Ok let's see if I can show you just how dumb I can be sometimes :P

How do I know if they've clicked on something, and what it is that they've clicked on?

A friend had suggested that I create an action log for just such things, including player login/outs - could you direct me on where to look to do something like that?

I appreciate your patience :)
Was This Post Helpful? 0
  • +
  • -

#5 JBrace1990  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 112
  • View blog
  • Posts: 760
  • Joined: 09-March 08

Re: Bumping Inactive Users

Posted 12 July 2009 - 05:48 PM

let me break it down for you then =)

1. A user logs on at 3:00. The "time" column in the database is updated to the current time of the server (called by the time() function).
2. The next time a player loads any page, you'll check if he's online or not by a statement like below
if(time() - $SERVER_TIME_FROM_DB < 300){
//add resources
}
else
{
//he's offline.
}


Was This Post Helpful? 1
  • +
  • -

#6 Shiznit  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 67
  • Joined: 14-September 07

Re: Bumping Inactive Users

Posted 12 July 2009 - 11:45 PM

View PostJBrace1990, on 12 Jul, 2009 - 04:48 PM, said:

let me break it down for you then =)


After plugging away for quite a few more hours, I was finally able to come up with:
<?php
  session_start();
  if (!isset($_SESSION['uid']))
	  {
		header("Location: index.php?msg=4");
	}
  
  include_once('includes/config.php');
  $conn=mysql_connect(DB_HOST,DB_USER,DB_PASS);
  mysql_select_db(DB_BASE);
  
  $redirect = $_SERVER['REQUEST_URI'];

	if (isset($_SESSION['last_activity']))
		{
			$inactive = 1800;
			$session_life = time()-$_SESSION['last_activity'];
			
			if ($session_life > $inactive)
				{
					header ("Location: handlers/logout_handler.php?inactive=1");
				}
			else
				{
					$last_activity_update_query = "UPDATE users SET `last_activity`=NOW() WHERE id=".$_SESSION['uid']."";
					$last_activity_update_result = mysql_query($last_activity_update_query) or die ('Last Activity Update Query: '.mysql_error());
				}
		}
	else
		{
			header("Location: index.php?msg=4");
		}

?>



This code is now in sessions.php which is included in every file and is working like a charm.

As you'd suggested, I did have to add a new field to my users table - last_activity - and with every click, this field is updated with the new time. With each page access, this script checks to see if the difference between the time now and the value of last_activity is greater than 1800, the user is redirected to the logout handler which automatically logs the user out and their session variables are unset

Also, the index page still contains a meta refresh for 1805; because index.php also includes this session file, it will run the script automatically :)

I will be soooooooooo relieved when this game is completed! lol

But thank you for pointing me in the right direction and being kind while I was being dumb :)

This post has been edited by Shiznit: 12 July 2009 - 11:50 PM

Was This Post Helpful? 0
  • +
  • -

#7 JBrace1990  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 112
  • View blog
  • Posts: 760
  • Joined: 09-March 08

Re: Bumping Inactive Users

Posted 12 July 2009 - 11:49 PM

No problem =)

everyone can be a little stupid at sometimes, and it actually wasn't being stupid lol
Was This Post Helpful? 0
  • +
  • -

#8 Shiznit  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 67
  • Joined: 14-September 07

Re: Bumping Inactive Users

Posted 13 July 2009 - 12:19 AM

I forgot to mention that since a user gets bumped after being inactive, the next time they logon the last_activity field is updated with the current time and the process starts all over again lol :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1