javascript in PHP to warn session about to expire..

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
pavanpuligandla
Forum Contributor
Posts: 130
Joined: Thu Feb 07, 2008 8:25 am
Location: Hyderabad, India

javascript in PHP to warn session about to expire..

Post 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.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

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

Post 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 \.
User avatar
pavanpuligandla
Forum Contributor
Posts: 130
Joined: Thu Feb 07, 2008 8:25 am
Location: Hyderabad, India

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

Post 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.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

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

Post 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.
mmj
Forum Contributor
Posts: 118
Joined: Fri Oct 31, 2008 4:00 pm

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

Post by mmj »

1. Either do an ajax call at a set interval to check the session.
2. Set the cookie with js.
User avatar
pavanpuligandla
Forum Contributor
Posts: 130
Joined: Thu Feb 07, 2008 8:25 am
Location: Hyderabad, India

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

Post 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.
Post Reply