Page 1 of 1

javascript in PHP to warn session about to expire..

Posted: Fri Oct 31, 2008 10:45 am
by pavanpuligandla
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;

Code: Select all

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,

Code: Select all

<body BGCOLOR="#F8F5EF" class="PSPAGE" onload="startClock();">
why is the script not bein executed? please help me ..
thanks n regards.

Re: javascript in PHP to warn session about to expire..

Posted: Fri Oct 31, 2008 2:52 pm
by requinix
Unlike PHP, Javascript doesn't like it when you put newlines in the middle of strings.

Code: Select all

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.'))
You're in the middle of a quoted string: each \n will turn into a newline, like

Code: Select all

if (confirm('There has been no activity for some time.
Click OK if you wish to continue your session,
or click Cancel to log out.
For your security if you are unable to respond to this message
within two minutes you will be logged out automatically.'))
Solution: escape each \ with another \.

Re: javascript in PHP to warn session about to expire..

Posted: Fri Oct 31, 2008 5:23 pm
by pavanpuligandla
hii..
thnx for d come back,

i;ve a doubt, whether javascript written in php script will execute. or not..
yes i made corrections to those slashes but no result..

many tahnks.

Re: javascript in PHP to warn session about to expire..

Posted: Fri Oct 31, 2008 8:45 pm
by requinix
I just noticed what you're trying to do.

Javascript and PHP are two completely different things. You can't use PHP functions in Javascript code.

Re: javascript in PHP to warn session about to expire..

Posted: Sat Nov 01, 2008 7:18 am
by mmj
1. Either do an ajax call at a set interval to check the session.
2. Set the cookie with js.

Re: javascript in PHP to warn session about to expire..

Posted: Sat Nov 01, 2008 7:22 am
by pavanpuligandla
hii..
got solution.

started a timer in my page which counts the session time out 'time', warns the user before 5 minutes, if user doenst respond to that the page will be redirected to sessiontimeout.php.

i think the above code will also work, but not sure how to write embedded php n javascript.
i'm now trackin the session with javascript.
many thanks.