hannan Posted April 5, 2011 Share Posted April 5, 2011 Hello friends, I am new to PHP and have developed a website... i am stuck at one place. I want to know if the user changes the url in the adress bar and goes to another webpage.. Can we detect that? This is very important for my website because I calsulate the duration of login based on whether user goes to another page or logs out. Quote Link to comment https://forums.phpfreaks.com/topic/232740-how-to-track-user-going-to-different-pages-through-the-browser/ Share on other sites More sharing options...
gizmola Posted April 5, 2011 Share Posted April 5, 2011 No you can't detect if someone changes the url. What you can do is utilize ajax to make periodic updates to a script on your site, which you can use as an "activity" monitor. Very few people actually logout of a website -- they just navigate away or close the browser. You should not depend on a logout. Quote Link to comment https://forums.phpfreaks.com/topic/232740-how-to-track-user-going-to-different-pages-through-the-browser/#findComment-1197111 Share on other sites More sharing options...
RussellReal Posted April 5, 2011 Share Posted April 5, 2011 No you can't detect if someone changes the url. What you can do is utilize ajax to make periodic updates to a script on your site, which you can use as an "activity" monitor. Very few people actually logout of a website -- they just navigate away or close the browser. You should not depend on a logout. well if you're gonna go with ajax, you could just go with an onbeforeunload event... but I don't know the browser support of that event. Quote Link to comment https://forums.phpfreaks.com/topic/232740-how-to-track-user-going-to-different-pages-through-the-browser/#findComment-1197112 Share on other sites More sharing options...
gizmola Posted April 5, 2011 Share Posted April 5, 2011 RusselReal, Those are not the same things at all. A timed ajax call can update status every 30 seconds or minute or 5 minutes. That technique is frequently used to support html/near realtime chat pages. Quote Link to comment https://forums.phpfreaks.com/topic/232740-how-to-track-user-going-to-different-pages-through-the-browser/#findComment-1197113 Share on other sites More sharing options...
RussellReal Posted April 5, 2011 Share Posted April 5, 2011 RusselReal, Those are not the same things at all. A timed ajax call can update status every 30 seconds or minute or 5 minutes. That technique is frequently used to support html/near realtime chat pages. yes I know what AJAX is, but AJAX is simply Javascript with use of XML Streams... I have used the same general idea on one of my sites as what you have mentioned, but I stuck to the Original Poster's guidelines with my answer.. I wasn't bashing your answer. He wanted to know when the user changed URLs, yes ofcourse you can't actually GET the new URL, but you can use this to know when the user: A. CLOSES THE BROWSER B. NAVIGATES AWAY FROM THE CURRENT PAGE. That seems to be his ultimate goal here then he could use AJAX inside of the onbeforeunload to send a request to the server, the server doesn't even have to give a response, make it a 1 way transaction saying that the user has left that page Quote Link to comment https://forums.phpfreaks.com/topic/232740-how-to-track-user-going-to-different-pages-through-the-browser/#findComment-1197116 Share on other sites More sharing options...
gizmola Posted April 5, 2011 Share Posted April 5, 2011 Russell, well if you're gonna go with ajax, you could just go with an onbeforeunload event... but I don't know the browser support of that event. The way I read your reply, it seemed to be saying that you could substitute one for the other. I will agree with you that you can use that function in most of the current browsers, but regardless, an ajax call is going to be needed. At the point that you are going to make an ajax call to indicate that the person has an active session, I think you might as well update the status periodically, but onbeforeunload() is a viable solution as well. Quote Link to comment https://forums.phpfreaks.com/topic/232740-how-to-track-user-going-to-different-pages-through-the-browser/#findComment-1197128 Share on other sites More sharing options...
hannan Posted April 7, 2011 Author Share Posted April 7, 2011 I have no idea with ajax... is there any php codin... Or can anyone help me with the code in ajax. Quote Link to comment https://forums.phpfreaks.com/topic/232740-how-to-track-user-going-to-different-pages-through-the-browser/#findComment-1198135 Share on other sites More sharing options...
gizmola Posted April 7, 2011 Share Posted April 7, 2011 I have no idea with ajax... is there any php codin... Or can anyone help me with the code in ajax. This is what I would recommend: http://jquery.com/ http://api.jquery.com/category/ajax/ You write a script that does session_start(), checks that there's a valid user and if so, calls an update function you write that will update the status of the user -- usually via a datetime column in the database named something like lastseen. In your jquery you make calls to this script based on the setInterval() javascript function. One reason not to use the onbeforeunload() event as suggested by russelreal, is that this would be problematic in certain circumstances where for example, someone opens up multiple tabs to your site for the same session, which is easy to do with current browsers. If you assume that because someone closed a tab they were logging out, you could get in a situation where you logged someone out who still had an open window on your site. Quote Link to comment https://forums.phpfreaks.com/topic/232740-how-to-track-user-going-to-different-pages-through-the-browser/#findComment-1198366 Share on other sites More sharing options...
RussellReal Posted April 7, 2011 Share Posted April 7, 2011 I have no idea with ajax... is there any php codin... Or can anyone help me with the code in ajax. This is what I would recommend: http://jquery.com/ http://api.jquery.com/category/ajax/ You write a script that does session_start(), checks that there's a valid user and if so, calls an update function you write that will update the status of the user -- usually via a datetime column in the database named something like lastseen. In your jquery you make calls to this script based on the setInterval() javascript function. One reason not to use the onbeforeunload() event as suggested by russelreal, is that this would be problematic in certain circumstances where for example, someone opens up multiple tabs to your site for the same session, which is easy to do with current browsers. If you assume that because someone closed a tab they were logging out, you could get in a situation where you logged someone out who still had an open window on your site. yes but they would be inactive until they visit another page anyway when they visit the next page their session will be touched and they'll be "active" again. Quote Link to comment https://forums.phpfreaks.com/topic/232740-how-to-track-user-going-to-different-pages-through-the-browser/#findComment-1198433 Share on other sites More sharing options...
gizmola Posted April 7, 2011 Share Posted April 7, 2011 I have no idea with ajax... is there any php codin... Or can anyone help me with the code in ajax. This is what I would recommend: http://jquery.com/ http://api.jquery.com/category/ajax/ You write a script that does session_start(), checks that there's a valid user and if so, calls an update function you write that will update the status of the user -- usually via a datetime column in the database named something like lastseen. In your jquery you make calls to this script based on the setInterval() javascript function. One reason not to use the onbeforeunload() event as suggested by russelreal, is that this would be problematic in certain circumstances where for example, someone opens up multiple tabs to your site for the same session, which is easy to do with current browsers. If you assume that because someone closed a tab they were logging out, you could get in a situation where you logged someone out who still had an open window on your site. yes but they would be inactive until they visit another page anyway when they visit the next page their session will be touched and they'll be "active" again. Doubtful. Usually a logout destroys the session data. Otherwise, it's not really a logout. Quote Link to comment https://forums.phpfreaks.com/topic/232740-how-to-track-user-going-to-different-pages-through-the-browser/#findComment-1198485 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.