Jump to content

Recommended Posts

I have moved further with this and just about have it working. Currently running into a wall however.

 

When a user logs in they automatically get logged out as soon as the new page loads after hitting log in.

The reason for this is because of the check that runs on each page to see if the user has been active within a 15 minute period.

 

Databae:

id int(11) NOT NULL auto_increment,
Username text NOT NULL,
lastonline bigint(20) NOT NULL default '0',
  PRIMARY KEY  (id)

 

The login:

if($row['Activated'] > 0)
{
$_SESSION['s_logged_n'] = 'true';
$_SESSION['s_username'] = $username;
$_SESSION['s_name'] = $row['Name']; 
$time = time();  

$insertuser="INSERT INTO online(Username,lastonline) values('$username','$time')";
mysql_query($insertuser) or die("Could not login insert user");
header("Location: index.php");
} 
else 
{

header("Location: l_error.php");

 

I'm pretty sure that is working correctly and adding the time they logged in.

 

This is the script that runs each page:

 

session_start();

   $lastonline = time();
   if ($_SESSION['lastonline'] <= (time() - 900)) // 900 = 15 minutes (60x15)
   {
   $username = $_SESSION['s_username'];
   $query = "DELETE FROM online WHERE username = '".mysql_real_escape_string($_SESSION['s_username'])."';";
   $result = mysql_query($query); 
   $_SESSION['s_logged_n'] = '';
   $_SESSION['s_name'] = '';
   $_SESSION['s_username'] = '';

   session_destroy();

   }
   else
   {  
      $username = $_SESSION['s_username'];
      $_SESSION['lastonline'] = time();
      $query = "INSERT INTO online(Username, lastonline) VALUES('${lastonline}')";
      $updateonline="Update online set username='$username',lastonline='$lastonline'";

      mysql_query($updateonline) or die("Could not update online");
   }

 

It seems the script is skipping the original IF statement to check the time and just destroys the session as soon as the user logs in.

Is their anything missing in either of those scripts that may be causing the problem?

Link to comment
https://forums.phpfreaks.com/topic/197633-online-users/
Share on other sites

Try:

 

In Login:


if($row['Activated'] > 0)
{
$_SESSION['s_logged_n'] = 'true';
$_SESSION['s_username'] = $username;
$_SESSION['s_name'] = $row['Name']; 
$time = time();
$_SESSION['lastonline'] = $time;

$insertuser="INSERT INTO online(Username,lastonline) values('$username','$time')";
mysql_query($insertuser) or die("Could not login insert user");
header("Location: index.php");
} 
else 
{

header("Location: l_error.php");
}

 

each page:

//else
else
   {  
      $username = $_SESSION['s_username'];
  $time = time();
  $updateonline = (isset($_SESSION['lastonline'])) ? "Update online set lastonline='$lastonline' WHERE username='$username'" : "INSERT INTO online(Username, lastonline) VALUES('$username','$time')";
      $_SESSION['lastonline'] = $time;
      mysql_query($updateonline) or die("Could not update online");
   }
   

Link to comment
https://forums.phpfreaks.com/topic/197633-online-users/#findComment-1037477
Share on other sites

Thank you for the reply  jcbones.

So far that is working, i see where my error was. At the moment it now logs the user in and doesn't automatically remove them. I will let it sit for 15 minutes and see what happens and also close the browser for 15 minutes after to see if it works.

 

Will updates once i see the results.

 

Also want to check this is correct, shouldnt the last online time in the database show an actual time and not a string of numbers?

Currently have this in the database:

ID: 5

Username: Jye.

Lastonline: 1270511983

Link to comment
https://forums.phpfreaks.com/topic/197633-online-users/#findComment-1037489
Share on other sites

Update:

It works and logs the user in correctly without removing them right away, the only problem is that it won't remove them after 15 minutes unless they try and request a page. So the user can sit on a single page for 15 minutes or closes the browser, but it won't remove them from the online table until they request a new page after 15 minutes.

Link to comment
https://forums.phpfreaks.com/topic/197633-online-users/#findComment-1037519
Share on other sites

In order to remove them without them submitting a page is a two fold answer.

 

You could run your delete query:

$logout = time() - 900;

$sql = "DELETE FROM `online` WHERE `lastonline` < '$logout'";

 

This will delete all users that haven't accessed any pages within 15 minutes (I currently use this method).

 

The other involves crontab. http://adminschoice.com/crontab-quick-reference

Link to comment
https://forums.phpfreaks.com/topic/197633-online-users/#findComment-1038049
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.