Online users.
Does anyone know how to do online user programming such as how forums can tell if a user is currently online?
I am developing a web application and it uses a log. I want the log to say exactly when the user leaves the URL.
I cannot use onunload because that will not fire if it detects a page re-routing.
[322 byte] By [
PeejAvery] at [2007-11-19 22:18:59]

# 1 Re: Online users.
Hi peejavery,
i'm not sure whether i'm right here but you could add an additional field, "status" in your database. So it should flagged 1 if the user logged in and update the field to 0 if he/she logged out or the session/cookie expires.
So in your script for checking whether he/she is online....you should do a database search such as the following.
$sql = `SELECT <username> FROM <tablename> WHERE <status>="1"`;
After that you could do a display of all the names that are fetched from the database.
I hope that i'm correct here...if not...we can continue to exchange pointers... :)
e_har at 2007-11-10 3:57:29 >

# 2 Re: Online users.
I hope that i'm correct here...if not...we can continue to exchange pointers... :)
Well, you are correct to an extent. I already have the status field. You are forgetting that PHP is a server-side language. I have to refresh the page to change the status from online to offline. That cannot be done with onunload.
# 3 Re: Online users.
kewl...you're getting my brains to work...
Refresh script...i'm not sure whether this stupid concept of mine could work but i can try it later today...
The concept is as follows:
$specialmeta=sprintf("<META HTTP-EQUIV=\"Refresh\" CONTENT=\"100;URL=http://%s%s\">",getenv(SERVER_NAME),getenv(SCRIPT_NAME));
ShowHeader($specialmeta);
Then in the ShowHeader function...you just redirect back to the page which you're at...
Hope that i get it right. :p :blush:
e_har at 2007-11-10 3:59:32 >

# 4 Re: Online users.
I think you are still missing the point. The problem is not to tell the browser to go to the logout page but to detect the browser close and then go to the logout display.
# 5 Re: Online users.
Whoa dude...i'm really confused here...could you elucidate me?
Tell me what you what step by step...maybe i know...maybe i don't...just trying my luck here...hehe...
e_har at 2007-11-10 4:01:34 >

# 6 Re: Online users.
1. User signs in and a log is written to MySQL stating date, time, user, and status. The status, of course, is "logged in."
2. When user leaves the URL I need another addition to the log, this time with a status of "logged out."
I am pretty sure that there has to be server communication backwards to the client.
# 7 Re: Online users.
The concept is as follows:
$specialmeta=sprintf("<META HTTP-EQUIV=\"Refresh\" CONTENT=\"100;URL=http://%s%s\">",getenv(SERVER_NAME),getenv(SCRIPT_NAME));
ShowHeader($specialmeta);
Then in the ShowHeader function...you just redirect back to the page which you're at...
There are still a couple of problems.
1. There should be no refreshing whatsoever. I don't want to refresh. I want the browser to detect when the browser closes or the URL is changed.
2. ShowHeader is not even a PHP function. What is ShowHeader?
# 8 Re: Online users.
1. User signs in and a log is written to MySQL stating date, time, user, and status. The status, of course, is "logged in."
This is a sample code i just made up...i didn't even test the code yet as i don't have a webserver now. But i hope that you understand what i mean.
index.php
It's for users to login.
<?php
echo '<html><head><title>Online Users Sample</title></head><body>';
echo '<form method="post" action="login.php">';
echo 'UserName: <input type="text" name="username"><br>';
echo 'Password: <input type="password" name="passwd">';
echo '</form></body></html>';
?>
After they login, the form will go to
login.php
<?php
require_once 'db.php';
session_start();
$username = $_POST['username'];
$passwd = $_POST['passwd'];
$time = date("d M Y");
$date = date("H:i:s");
if (isset($username) && isset($passwd) && $username != '' && $passwd != '') {
$sql = 'SELECT * FROM account WHERE username = ' . $username . ' AND passwd = '. $passwd;
$result = mysql_query($sql, $connection)
or die('Could not find user information. ' . mysql_error());
if ($row = mysql_fetch_array($result)) {
$sql2 = 'UPDATE account SET status=1 where username=' . $username;
mysql_query($sql2);
or die('Could not update status. '. mysql_error());
header('Location: destination.php');
}
} else {
$specialmeta=sprintf("header(\"Refresh: 5; http://%s%s\")", getenv(SERVER_NAME),getenv(SCRIPT_NAME));
echo $specialmeta;
echo 'You did not enter the correct username or password.';
exit;
}
?>
This one requires some explaining for those who are new to PHP and would like to learn what peejavery is doing(I hope i understand what he is trying to do.)
I didn't include db.php as the contents are what you would specify for connection to your database. So now after successfully logging in, you'll be redirected to destination.php
This is for the first part.
2. When user leaves the URL I need another addition to the log, this time with a status of "logged out."
I am pretty sure that there has to be server communication backwards to the client.
I'm not sure what you want here..."leaves the URL"...you mean you want the status for every link in your website? That is quite troublesome but is not difficult.
If it's just logged out of your website, you could do something like this
<?php
session_start();
session_unset();
session_destroy();
// After this line, do a UPDATE statement to your database and change the status to 0.
?>
I do hope that i got it right this time...hehe... :p
e_har at 2007-11-10 4:04:30 >

# 9 Re: Online users.
As I have stated before, you are missing the point.
You are writing a script that logs in and logs out while writing these results to the database. I already have that.
What I need...
When the user closes the browser or types a different URL, I want him to be redirected to my logout page and then sent to the URL he typed in the address bar.
# 10 Re: Online users.
What I need...
When the user closes the browser or types a different URL, I want him to be redirected to my logout page and then sent to the URL he typed in the address bar.
Thousand apologies if i mistaken what you wanted...
Although the idea seems interesting....but...it sounds very strange to me for you to use PHP for this feature. If the user types a foreign URL which is not related to yours...how is it possible to redirect it back to the URL or to the logout page?
I think it's only possible if it's client-side scripting. :blush:
e_har at 2007-11-10 4:06:34 >

# 11 Re: Online users.
I think it's only possible if it's client-side scripting. :blush:
Well, that is impossible because all unload commands reject the firing of a redirection. I think it has to be server-side communication to the client machine.
# 12 Re: Online users.
Correct me if i'm wrong here as i haven't done web programming for ages....The browser is a disconnected client. This is the nature of the http protocol.
Firstly, as i've really not been doing web programming for yrs, what i give as reasons may be valid back then and not now as things changes. Don't hold me responsible if the code really changes or the code is deprecated.
Secondly, f you have somebody using a browser other than IE or Firefox, unload may not work as it's not implemented in Opera or Maxthon...few yrs ago.
Thirdly, there are some pop-up blockers such as the Google's tool bar which will prevent onunload from executing.
The only standard way to control this is with your session time out setting on the server. There is NO way to determine accurately if the user has closed the browser. You can not intercept the menu commands(Unless you're doing Hooking...but with scripting??), the close window icon click in the chrome of the browser nor detect if the user loses their network connection or simply shuts off their machine or power failure.
The only other way i can think of that may solve your problem is this piece of code but it'll only work for IE.
The onbeforeunload is called before the close is executed for the page, the onunload is called at the same time as the close is issued.
Depending on what you are doing, sometimes a function that you execute in a onunload does not finish because the window is closed before the function has time to excute.
Here are 2 way how i would use onbeforeunload()
First way
<HTML>
<HEAD>
<SCRIPT>
function closeIt()
{
event.returnValue = "Any string value here forces a dialog box to
appear before closing the window.";
}
</SCRIPT>
</HEAD>
<BODY onbeforeunload="closeIt()">
<a href="http://www.dev-archive.com/forum/">Click here to navigate to
www.dev-archive.com/forum/</a>
</BODY>
</HTML>
Second Way
<SCRIPT LANGUAGE=JavaScript FOR=window EVENT=onbeforeunload>
if(window.event.clientY < 0 && window.event.clientY < -80) {
alert("Trapping Closing event");
}
</SCRIPT>
e_har at 2007-11-10 4:08:38 >

# 13 Re: Online users.
As you can see before, I stated you cannot use the onunload commands because all browsers reject any automated scripting. If using the onunload command, there must be user interaction.
I have posted in the server-side scripting because it will have to be server-side interaction. I am working with Safari and so are my clients. Therefore it has to be server-side and not client depending.