Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

PHP for Response.IsClientConnected

Status
Not open for further replies.

tlhawkins

Programmer
Dec 28, 2000
797
US
Hi,

I usually hate these kind of questions. I know each language is totaly different. However, I need to know if there is a way to do this ASP call in PHP.

Response.IsClientConnected

in ASP that would check if the users browser is still connected to the server.

I'm converting a site from ASP to PHP for a client and wondered if anyone knows a way of doing this in PHP? I'm running PHP in IIS if that makes any difference.

Thanks




Travis Hawkins
 
i know of no way to determine automatically whether a client is actively connected to a web server.

the normal way of emulating this is as follows:

1. register your sessions (or part of them) in a database with a time stamp
2. set an arbitrary time (say 2 minutes) during which you consider a user to be 'active'.
3. query the database for any session activity within the arbitrary time.
4. have a cron job doing a regular garbage clearance on the database.

this sounds complex but it's only a few lines of code in a function that you can then call and recall as you want.

the alternative to a true 'isConnected' is again to use sessions.

in the session construct record the user's presence in a table.
in the session close record the user's departure in a table
the you can query the table as and when you need.

let us know whether you need help doing this.
 
Thanks,

I think the session concept may work for what I need. I'm sure I can put that together. I may do a little more research about what the Response.IsClientConnected call does in the background. As I was thinking about it I realized there is really no way of knowing if the browser hasn't JUST closed unless you send some kind of message to the browser, I can't imagine pinging the users IP address would be ok, or that it would return different results depending on if the browser were open or closed.

Another thought was to have the Browser side send a call to the server every few seconds (just a "hi, I'm still here") message. That could be tracked in the DB and queried against to gain a little more resolution.

So, thanks for getting me thinking about it. I'll post if I find anything interesting

Travis Hawkins
 
yup, an ajax call back to the server would create a semblance of 'activity'.
the session method does tell you when the session has been saved. which will normally once the script that outputs to the browser has been terminated.
i wrote a sessions class that used a database rather than a filesystem once. it might be useful for what you're doing. let me know if you're interested and i'll dig it out of the archives.
 
yeah, I think I'm closing in on it here... if I attached an Ajax call to the server in the browsers onUnload event all it would really have to do is set a session variable flag to "Gone", then the long script should be able to check that variable before continuing.

Can you think of any reason why that wouldn't work? The call should originate in the same session as it would come from the same browser. I wonder if there is anything that would keep PHP from updating the session variable value for the process that was still running? I guess there is a chance that a running script would have a snapshot of those values rather than a live version... then I would have to update the server with the session value and check it from there.

Funny you mention the DB Session Management, I was just rolling my own session management in PHP for the first time the other day. I would love to see your code on that for my own education if nothing else. I was actually trying to get PHP to store the session variables in XML rather than the Serialization that PHP uses. I'm then using the Session XML file to transform an XSL to make SOAP calls and build sections of the page. It's pretty slick, but I'm sure there are improvements I could make. I'm getting Off Topic here :), Thanks for the ideas

Travis Hawkins
 
Hi, a great inspiration for you, might be looking at a simple "users online", session based php/mysql script.

If your users are registerred, you can use session to validate them on client->server and you can update a field in mysql.

PSUEDOCODE for what I'm thinking:
Code:
// always start the session at the very topmost of the pages which you want to use the session variables
session_start();

refreshUserStatus(session_id());

function refreshUserStatus($sid) {
  // code here for insert or update user status
}

Yes, I know.. I dont have your code there, since you will have to make it yoru self.

If you wish to sessioncontrol anonymous users, it's no biggie, that just means you can INSERT it into the table you make in mysql.

I would have two fields then:
SID (varchar I guess(255 length?))
timestamp (this updates on insert or update)

When the updateUserStatus runs, you either check for that SID in the DB and update it, or you just insert a new row.
If you insert and insert, you will have more rows in the db. However, you can make a janitor script which purges old rows, eg. like.. drop the rows where timestamp is a day old or so.

Another way to go, if you have a users db, is:
You can have the last_online as a timestamp field in the table, or another table.. It all depends on how you wish to do it.. You can of course also store all info, if you wish.. eg tie. the session id, logged in times, etc. to the user.. In such case, it's also quite easy:

[tbl_user]
user_id (unique id, integer and increase+1)
user_name (varchar 255)
user_pass (remember to crypt the pass with salt)

[tbl_session]
session_id_key (unique id, integer and increase+1)
session_user_id_fk_id (integer, foreign key for tbl_user)
session_id (varchar(255))
session_last_active (timestamp)

You might want to use other fieldtypes, it all depends on how much data you wish to store.. if you wish to have detailed loggingabilities, you store a lot.. if not, you can even just skip storing the session id and you can then just use a timestamp field in the user table, as when the user has authed (logged in), you know he/she most likely is that person and you can say that if that time is less than 5 minutes ago, you can call him/her as an onlien user.

Hope some of my scribble made sence.

Olav Alexander Mjelde
 
Oh for crying out loud... there is a PHP call for it after all.

It's
connection_aborted()

there are a few connection functions that could be used in this direction but that one sounds like the ticket. Returns True if the client is no longer connected.


Travis Hawkins
 
this will only report on the current client (i.e. the client in respect of which the script was called). it will not give you data on other client connections.

but i agree that it is a perfect answer to your original question. i had (without evidence) understood your question to be about providing a "who's here now" set of functionality.

although the manual doesn't say so, i'm concerned that the function might not work as expected with cgi/fcgi web server integration. i can't see how the php server executed on the command line would be able to derive the connection status. but since the manual does not say that the function is sapi only i guess there's some hoodoo going on to make it work. do post back your findings with this. i can see it being useful in image gallery type scripts (or blogs) where cacheing is undersirable but the page is still 'busy'.

i will post the session handling script when i dig it out.
 
Ahh.. so you knew about this crazy call :)

I've been asking around and nobody seems to know how the Response.IsClientConnected call works either, so there must be some kind of crazyness going on. Knowing MS it's probably just a random 0/1 back there someplace that it's returning %-)

I'll let you know how it works, and look forward to seeing the DB Session handler

Travis Hawkins
 
Because ASP sits inside IIS only MS have far more control over how the environment works. In particular they have access to the socket handle that the remote client is connected on. They will query the socket to see if the remote end is still connected. If response WSAENOTCONN (10057) is returned (and possibly others), ASP will tell you that the client is no longer connected.
The sun ASP ONE product(used to be known as chilli soft) doesn't support the clientconnected call (as it sits in a "disconnected" web server).
If you knew the internals of Apache (and all the other web servers that PHP will work with) or perhaps PHP might be able to, you could probabbly get the handle of the socket that PHP is currently connected to and query it so see if it is still connected.
Uou could possibly create an APache module that you could query with the PHP internal handle. I havn;t looked atthe source of either fo years so wouldn't know where to start.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top