Jump to content

[SOLVED] How do I log users out after they have closed the browser?


dpacmittal

Recommended Posts

hum...if my test script worked on your xamp, then it's not the server. what is your current code for retrieve.php? where does the posted message go (aka does retrieve.php handle that too)?

 

 

retrieve.php:

<?php
session_start();
$id=$_SESSION['id'];
include("connect.php");
ob_implicit_flush(true);

$curr_time=time();
$updquery=mysql_query("Update users set lastactive='$curr_time' where user_id='$id'");
$delquery=mysql_query("delete from users where (lastactive+20)<'$curr_time'",$con);
$lastmsg=$_SESSION['lastmsg'];

for($i=1;$i<=20;$i++)
{
$q=mysql_query("Select Max(lineno) from abc");
$row2=mysql_fetch_row($q);
if($row2[0]==$lastmsg)
{
usleep(1000000);
}
else
{
$res1=mysql_query("Select * from abc where lineno>'$lastmsg' and lineno<='$row2[0]' order by lineno ASC");
while($row = mysql_fetch_array($res1))
{
echo $row['line']."<hr width=97% size=1 noshade>";
}
$_SESSION['lastmsg']=$row2[0];
exit;
}
}
?>

 

 

retrieve.js:

function sendpost() {
var txt=document.chat.line.value;
document.chat.line.value="";
$.post("post.php", { line: txt } );
}


function sub(e) {
var keycode= e.which?e.which:window.event.keyCode;
if(keycode==13)
sendpost();
}


function retrieve()
{$.ajax({
  type: "GET",
  url: "retrieve.php",
  success: function(data) {
$("#chatwin").append(data);
},
complete: function(xhr,status) {
if(status=="success")
retrieve();
}
});
}


function startRetrieve() {
setInterval("user_fetch()",1000);
retrieve();
}

function user_fetch()
{
var uf=new httprequest();
var url="User_online.php";
uf.open("GET",url,true);
uf.onreadystatechange=function() {
if(uf.readyState==4 && uf.status==200)
{
document.getElementById("online_list").innerHTML=uf.responseText;
}
}
uf.send(null);
}

 

chat.php (the main file which user loads in the browser) :

<?
session_start();
if(isset($_SESSION['uname']) && isset($_SESSION['id']))
{
$id=$_SESSION['id'];
$uname=$_SESSION['uname'];
include("connect.php");
$q=mysql_query("select * from users where user_id='$id' and user_name='$uname'");
if(mysql_num_rows($q))
{
?>
<html>
<head>
<script type="text/javascript" src=xmlobject.js></script>
<script type="text/javascript" src=jquery.js></script>
<script type="text/javascript" src=retrieve.js></script>
<style type="text/css">
<!--
#online_list {
position:absolute;
width:200px;
height:531px;
z-index:1;
left: 1045px;
top: 18px;
background-color: #000000;
color: #FF9900;
}
#chatwin {
position:absolute;
width:1000px;
height:432px;
z-index:2;
left: 13px;
top: 13px;
color: #6633CC;
background-color: #000000;
	overflow:auto;
padding:10px;
}
body {
background-color: #333333;
}
#logoffLink {
position:absolute;
width:44px;
height:25px;
z-index:3;
left: 126px;
top: 447px;
}
#autoscroll {
position:absolute;
width:109px;
height:25px;
z-index:4;
left: 14px;
top: 447px;
}
#msgbox {
position:absolute;
width:646px;
height:54px;
z-index:5;
left: 14px;
top: 482px;
}

-->
</style>

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"></head>
<body onload=startRetrieve()>

<div id="chatwin">
</div>
<div id="autoscroll">
  <form name=autoscr>
    <input name="scr" type="checkbox" value="" checked>
    Auto Scroll
  </form>
</div>
<div id="logoffLink"><a href="javascript:window.open('','_parent','');window.close();">Logoff</a></div>

  <div id="msgbox">
    <form name="chat" onKeyPress="sub(event)">
  <textarea name="line" cols="100"></textarea>
  <input type="button" value="Send" onClick=sendpost()>
</form>
</div>
  <p> </p>
  <p> </p>
  <p> </p>
  <p> </p>
  <p> </p>
  <p> </p>
  <p> </p>
  <p> </p>
  <p> </p>
  <p> </p>
  <p> </p>
  <p> </p>
  <p> </p>
  <p> </p>
  <p> </p>
  <p> </p>
  <div id="transcript"></div>
  <div id="online_list"></div>

</body>
</html>
<?
}
else
{
unset($_SESSION);
session_destroy();
header('location:login.php');
}
}
else
{
header('location:login.php');
}
?>

 

 

You were wrong when you said usleep(1000) delays 1 second. I referred to the php manual, it said usleep takes microseconds as arguments.

Interesting thing is when I put usleep(1000) instead of usleep(1000000), the script works fine. Which means that message is posted only after execution of retrieve.php completes.

Link to comment
Share on other sites

You were wrong when you said usleep(1000) delays 1 second. I referred to the php manual, it said usleep takes microseconds as arguments.

Interesting thing is when I put usleep(1000) instead of usleep(1000000), the script works fine. Which means that message is posted only after execution of retrieve.php completes.

oops, got my microseconds & milliseconds mixed up

 

not sure then. i don't see anything that is wrong with the code.

Link to comment
Share on other sites

not sure then. i don't see anything that is wrong with the code.

 

Yeah, nothing wrong with the code. I found out the solution. After 3 days of googling, I finally found one website which has something about my problem.

 

http://www.ovalpixels.com/blog/2008/11/15/highly-responsive-ajax-applications-without-excess-bandwidth-waste/

 

Just have a look and you'll learn something new. PHP's inbuilt session handler won't allow two php scripts that use same sessions run simultaneously. It will just queue them until the previous file has completed executing. That was hell of a problem. I couldn't have thought about that.

 

I feel like sh*t.

 

Thanks for your regular response. You helped a lot.

Link to comment
Share on other sites

And, if you hadn’t read this post, you’d have had some hard time finding this out. It is just that in every normal application, you can be 97% sure you will be using sessions. And in most of the time, you will rely on the built-in php sessions’ handler. So far, so good. But the small problem is that you cannot simultaneously be running different scripts ( or more than one instantiation of a script ) which use one and the same user session ( that, again, was a discovery by Mr. Pagebaker ). PHP will just queue all subsequent requests which try to interact with that session, until the first one has been closed. Well, that is not much of an asynchroneous technique either then. Getting back to our chat application - if you want to long-poll the server and wait for new messages ( what we will call read() ) and simultaneously send new messages to the server ( write() ), the application will not write() until the read() request has finished ( which we have set to 5 mins, if in the worst case there is no new data to be received ) - that is absolutely not what we need.

 

wow...how did i not know that! i can't believe i haven't run into that issue before. i wonder if you could work around it with cookies instead

Link to comment
Share on other sites

I couldn't think, even in my dreams, that sessions would be causing such a problem. There's a workaround using cakephp (actually database based sessions) which is mentioned on the same page which I provided link to.The workaround is also on the same page. Hopefully, I would be able to resume the development of my application now, which was stopped due to this problem.

Thanks, again.

 

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.