Jump to content

Online


Ashoar

Recommended Posts

Ok i have a users online section for my code.

I have a table called online that contains one row, called Username.

 

When a user logs on their name is entered into this database. On the main page i can then fetch all users online and display them.

When you log out, your user name is removed from the online table.

 

The problem is, if you sit idle on one page too long or do not logout correctly, your name will continue to stay on the list even if your logged out.

When you log back in your name will the be on the list 2 times. It will continue like that untill you properly log out, where any of your names will be removed from the list.

 

I want a way to stop this from happening, so that if you are logged out from being idle to long or closing the window when you leave it will remove you from the online table.

 

I was suggested to add a time stamp but i could not get anything like that working.

 

So could some one show a decent way of completing this?

 

Here is the log in and log out code:

 

Login:

<?php 
session_start(); 
include 'config.php';
if(isset($_POST['login']))
{

$username = trim(addslashes($_POST['username']));
$password = md5(trim($_POST['password']));

$query = mysql_query("SELECT * FROM Users WHERE Username = '$username' AND Password = '$password' LIMIT 1") or die(mysql_error());

$row = mysql_fetch_array($query);

// now we check if they are activated

if(mysql_num_rows($query) > 0)
{

if($row['Activated'] > 0)
{

$_SESSION['s_logged_n'] = 'true';
$_SESSION['s_username'] = $username;
$_SESSION['s_name'] = $row['Name'];   

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

echo '
<html>
<head>
<title>Login</title>
<link href="style.css" rel="stylesheet" type="text/css">

</head>

<body>
<div id="error"><p>Sorry, you must activate your account first. Please check your email for the email.</p>
<p>Didn'."'".'t get your validation email? <a href="resend.php">Click here</a> to resend the validation email.</p></div>
</body>
</html>
';

}

} else {

echo '
<html>
<head>
<title>Login</title>
<link href="style.css" rel="stylesheet" type="text/css">
</head>

<body>
<div id="error"><p>There was an error processing your login, it appears that your username and/or password was incorrect. Please try again.</p>
<p>Didn'."'".'t get your validation email? <a href="resend.php">Click here</a> to resend the validation email.</p>

</div>
</body>
</html>
';

}

} else {
?>
<div id="wrapper">
<?php
include "header.php";

?>

<html>
<head>
<title>Login</title>
<link href="style.css" rel="stylesheet" type="text/css">
</head>

<body>

<div id="wrapper">

<table class='maintable'>
<tr class='headline'><td width=100%>Login</td></tr>

<form method="post" action="<?= $_SERVER['PHP_SELF'] ?>">
  <tr class='mainrow'><td><p>Username:<br>
  <input name="username" type="text" class="textBox" id="username">

  <tr class='mainrow'><td><p>Password:<br>
    <input name="password" type="password" class="textBox" id="password">
</p>
  <p>
    <input name="login" type="submit" class="textBox" id="login" value="Submit">
  </p>
  </form>
  <p>Didn't get your validation email? <a href="resend.php">Click here</a> to resend the validation email.</p>
  <p>Need an account? <a href="register.php">Click here</a> to register, it's completely free! </p>
</div>
</table>
</div>

</body>
</html>

<? } mysql_close($l); ?>

<?php
include "footer.php";
?>

 

Logout:

<?php
include 'config.php';
session_start();
$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();

header("Location: index.php");
?>

<div id="wrapper">
<?php
include "header.php";
?>

<?php
include "footer.php";
?>

Link to comment
Share on other sites

There are user online scripts posted all over the place - http://www.hotscripts.com/category/php/scripts-programs/counters/real-time/

 

Perhaps you are having a problem with sessions or with the database that is preventing what you have tried from working. Does your existing login/user code work when going from page to page?

Link to comment
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.