Page 1 of 1

javascript within php not working!

Posted: Sun Feb 22, 2009 12:46 am
by rwahdan
dear all,

i need to make javascript code within if statement in php so that when a user session is over, he will be redirected to another page! its not working and there is no error! the one i am talkting about is bold and underlined. thanks for the help.

code:
<?php

session_start();


if ($_SESSION['timeout'] > (time() - 60))
{
echo "You are logged in.";
}
else
{
echo "Your session has timed out.";
//echo '<script type=\"text/javascript\">';
//echo 'window.location="http://islam4everyone1429.net/authanticate/sorry.php"';
//echo '</script>';
}

if (!isset($_SESSION['db_is_logged_in'])
|| $_SESSION['db_is_logged_in'] !== true) {

// not logged in, move to login page
header('Location: http://islam4everyone1429.net/authanticate/sorry.php');
exit;
}
?>

<html>
<body>

<p>This is the main application page. You are free to play around here since you
are an autenthicated user :-) </p>

</body>
</html>

Re: javascript within php not working!

Posted: Sun Feb 22, 2009 1:19 am
by semlar
And you're not using header to redirect the page.. why?

Re: javascript within php not working!

Posted: Sun Feb 22, 2009 7:41 am
by Mark Baker
Well the lines

Code: Select all

 
echo "Your session has timed out.";
//echo '<script type=\"text/javascript\">'; 
//echo 'window.location="http://islam4everyone1429.net/authanticate/sorry.php"'; 
//echo '</script>'; }
 
should be written to the output (assuming they don't remain commented out)

but then your code is continuing to output

Code: Select all

 
<html>
<body>
 
<p>This is the main application page. You are free to play around here since you 
are an autenthicated user  </p>
 
</body>
</html>
 
So the javascript is outside the html tags

And there's nothing anywhere to call the javascript either

Re: javascript within php not working!

Posted: Sun Feb 22, 2009 12:41 pm
by dheeraj
Try below code i think it will work..... best of luck

Code: Select all

<?php

session_start();


if ($_SESSION['timeout'] > (time() - 60))
{
echo "You are logged in.";
}
else
{
echo "Your session has timed out.";
?>

<script type="text/javascript">
window.location="http://islam4everyone1429.net/authanticate/sorry.php"
</script>


<?php }

if (!isset($_SESSION['db_is_logged_in'])
|| $_SESSION['db_is_logged_in'] !== true) {

// not logged in, move to login page
header('Location: http://islam4everyone1429.net/authanticate/sorry.php');
exit;
}
?>

<html>
<body>

<p>This is the main application page. You are free to play around here since you
are an autenthicated user  </p>

</body>
</html>