/    Sign up×
Community /Pin to ProfileBookmark

how do I update the database after user leaves my site?

a user is browing my site then he leaves my site by either closing the browser or clicking on a link which takes him to another site.

After the user leaves my site, I need to update some information in the database. Is there some function in php I can use which will execute when the user leaves my site?

to post a comment
PHP

6 Comments(s)

Copy linkTweet thisAlerts:
@bokehApr 10.2008 — No!
Copy linkTweet thisAlerts:
@YourChildauthorApr 10.2008 — ...???? are you serious? then how would i go about doing this?
Copy linkTweet thisAlerts:
@bokehApr 10.2008 — Your database shouldn't rely on client behaviour. Can you give more details?
Copy linkTweet thisAlerts:
@YourChildauthorApr 10.2008 — ok...

I'm tweaking up a chatroom. I've added a chatlist, which is a list of users currently in the chatroom. When the user enters the chatroom, I add his userID to the chatroom table in the db.

Now, when he leaves the chatroom and browses to another page within this website, I can easily remove his name from the chatroom table.

But if he closes the browser or navigates to another website, I still want to do the same thing - remove his name from the chatroom.

Ahhh this is more of an overview thing to discuss..i cant really show codes because its all over the place and it wouldnt help...

I can access the db using ajax no problem...I just need a way to get into a javascript function right when the user exits the page...

I suppose I can use some javascript to call a function which will send a message to the server using ajax to update that chatroom table.

I've tried 'onunload = somefunction()'

But this method isnt working out too well because I am using the same template for each page ...and onunload is placed in the <body> tag like this:

<body onunload="somefunction()">

which means its going to call somefunction each time he browses to a different page.

I just want to update the chatroom table whenever he closes the website while he's in chatroom.php.
Copy linkTweet thisAlerts:
@bokehApr 11.2008 — Just have a timestamp field in the database and close users that haven't touched the DB in > [I]n[/I] seconds.
Copy linkTweet thisAlerts:
@YourChildauthorApr 11.2008 — This would probably be good, if we're dealing with a site that wants to automatically log off a user if the user show signs of inactivity - by not uploading a new page.

I think this is what bank website uses ..it logs me off automatically when i am idling on their site...

But for a chatroom, I may want to dwell in a chatroom all day ...in that case, my only option is to use a function that will execute automatically when the user closes the browser...

the only thing I can think of is 'onunload = exitUser(userID)" .. then use ajax and access the db...

I was hoping for an alternative way to do this because users might turn off javascript =
×

Success!

Help @YourChild spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...