Jump to content

Use JS to start MySQL queries.


shadowayex

Recommended Posts

I was wondering if it was possible to use the onload and onunload options to start mysql queries. The reason I want to do this is I want to keep track of when a user comes to a page and leaves a page (like the deals this forum has that tell us who's browsing in the forum).

Link to comment
Share on other sites

This thing about the forum monitoring you are mentioning is a feature of the phpBB forum. Unless I am mistaken w3schools must be using phpBB. So they do it with php, not JScript. Now ... I am not an expert so don't take my word for it but I don't think you can do such a thing since Javascript is browser-side scripting and not server-side and I do not think there is any way around it.I mean .. imagine someone with malicious intentions checking your .js file and seeing your database password >_<

Link to comment
Share on other sites

This thing about the forum monitoring you are mentioning is a feature of the phpBB forum. Unless I am mistaken w3schools must be using phpBB. So they do it with php, not JScript. Now ... I am not an expert so don't take my word for it but I don't think you can do such a thing since Javascript is browser-side scripting and not server-side and I do not think there is any way around it.I mean .. imagine someone with malicious intentions checking your .js file and seeing your database password >_<
True true... Then how do that do it. I wanna know T_T.
Link to comment
Share on other sites

It would be possible although kinda hard to secure, but maybe something like a session variable would work. When the page is requested, have PHP log a random hash, the current timestamp, and any other info in the database, leaving a second timestamp field empty. Drop the random string into the JavaScript so it will send it back to the server via AJAX onunload. When the AJAX call is executed, check whether a timestamp field corresponding to the hash is still empty; if it is, fill it with the current timestamp.

Link to comment
Share on other sites

If I'm not mistaken, this forum takes the timestamp on the server on each page load. Then, to determine who is currently browsing the forum, they select from the database all the users with timestamps that are within the last 15 minutes.

Link to comment
Share on other sites

Also, this forum uses the Invision service, not phpbb. But yeah, every time the page loads when it checks if you're logged in it also records the time and where you are. If you want to use Javascript to do something like that (which isn't necessary because PHP does it just fine), then you can have Javascript send an AJAX request to a PHP script on the server to update that.

Link to comment
Share on other sites

Yeah I figured out how to do it strictly PHP. My solution was to start the page with a query that updates a field in my database called LastActive with a timestamp. Then I have queries that select users that have been active for the past 15 minutes. I wanted to make it so if the user closed out of the site or logged out, it'd send a query to an added database field starting whether they are online or not, but I don't know how to make it send the query when the page is closed. That's where the JavaScript idea came from.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...