pavanpuligandla Posted October 31, 2008 Share Posted October 31, 2008 Hii, i'm using javascript code in php to warn a user that his session is about to expire in x minutes, for that i tried a script like this, but it is not getting executed. herez the php script; echo "<SCRIPT LANGUAGE='JAVASCRIPT'> var a, pageTimer; //declare var function startClock() { pageTimer = 0; //start at 0 a = window.setInterval('tick()',60000); //run func tick every minute.... (60 sec x 1000 ms = 1min) } function tick() { pageTimer++; //increment timer if (pageTimer == 28) {warnuser()}; //if 28 min without activity } function warnuser() { if (confirm('There has been no activity for some time.\nClick OK if you wish to continue your session,\nor click Cancel to log out.\nFor your security if you are unable to respond to this message\nwithin two minutes you will be logged out automatically.')) { // restart session session_start(); } else { //end session force user to login page include 'sessiontimeout.php'; } } </SCRIPT>"; ?> herez my html code, <body BGCOLOR="#F8F5EF" class="PSPAGE" onload="startClock();"> why is the script not bein executed? please help me .. thanks n regards. Link to comment https://forums.phpfreaks.com/topic/130903-javascript-in-php-to-warn-session-about-to-expire/ Share on other sites More sharing options...
kenrbnsn Posted October 31, 2008 Share Posted October 31, 2008 You can not execute PHP inside Javascript like that. You have to use Ajax techniques. Ken Link to comment https://forums.phpfreaks.com/topic/130903-javascript-in-php-to-warn-session-about-to-expire/#findComment-679488 Share on other sites More sharing options...
monkeytooth Posted October 31, 2008 Share Posted October 31, 2008 You are spot on with the concept. But what I have notice that a lot of people do, is either in a 1px high frame with no borders, scrollbars etc.. just have the java reload that frame every x seconds or mins and have it run its functions that way, if the session is about to expire give the warning if it does have it redirect the parent page to a different page to kill the session. Note Sessions only die when you kill them or the browser is closed.. they will after some time "expire" but generally I could go to your page get my sessions values run away for a bit to another page and come back and still be fine.. That said one thing to consider is setting a session value of x, x being a time stamp.. that way when users come back onto your site after awhile first thing you do is check to see if that session var is there if it is what time was it set and if it was set x amount of time or longer ago kill it. Link to comment https://forums.phpfreaks.com/topic/130903-javascript-in-php-to-warn-session-about-to-expire/#findComment-679489 Share on other sites More sharing options...
rhodesa Posted October 31, 2008 Share Posted October 31, 2008 once the page loads, you can no longer use JavaScript. In other words, you can use PHP to generate the JAVASCRIPT code, but once it's generated, and the browser starts processing it, you can't use PHP anymore. What you can do is make an AJAX call, if they say yes, to a basic file that just touches the session. If they say no, do a window.location.href='logout.php'; Link to comment https://forums.phpfreaks.com/topic/130903-javascript-in-php-to-warn-session-about-to-expire/#findComment-679490 Share on other sites More sharing options...
pavanpuligandla Posted October 31, 2008 Author Share Posted October 31, 2008 okay, i revised my code, <script language="JavaScript"> var a, pageTimer; //declare var function startClock() { pageTimer = 0; //start at 0 a = window.setInterval("tick()",60000); //run func tick every minute.... (60 sec x 1000 ms = 1min) } function tick() { pageTimer++; //increment timer if (pageTimer == 3) {warnuser()}; //if 28 min without activity } function warnuser() { if (confirm("There has been no activity for some time.\nClick 'OK' if you wish to continue your session,\nor click 'Cancel' to log out.\nFor your security if you are unable to respond to this message\nwithin two minutes you will be logged out automatically.")) { //ping server in iframe to restart session refreshme.document.location.href = "http://localhost/MyDreamProject/Mainmenuframe.php" } else { document.location.href="http://localhost/MyDreamProject/examadminlogout.php" } } </script> <body id ="refreshme" onload="startClock();"> every thing is fine, but the session is not refreshing as i set session for a time limit of 30 minutes, the user is warned at 28th minute, if he clicks okay, then session is gettin ended after 2 minutes as usual. ??? Link to comment https://forums.phpfreaks.com/topic/130903-javascript-in-php-to-warn-session-about-to-expire/#findComment-679502 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.