Reputation: 13
I'm trying to logout of a php session using javascript. It doesn't work - the javascript function is called and the if statement works, but the php script isn't called. Is there a better way to do it? I am using a .php page.
function logoutck()
{
var r = confirm("Do you really want to log out?");
if (r==true)
{
<?php
session_start();
session_destroy();
header('Location: login.php');
?>
}
}
Upvotes: 1
Views: 16432
Reputation: 11661
How javascript, html and php work:
The server first executes the php script from top to bottom. It executes all the statements in the script and builds an html page so: <?php echo '<h1>hello world<h1/>'; ?>
becomes <h1>hello world<h1/>
. When the server is done executing everything it returns the created html page.
This page is then loaded in by the browser and then when the browser sees javascript commands it tries to execute it. So when a javascript command gets executed there is no more php commands because the php server already executed them and made an html view out of it.
the following statements are the same for a php server
<?php echo '<h1>hello world<h1/>'; ?>
<?= '<h1>hello world</h1>';
<h1>hello world</h1>
so note that when your php script is executed you will go to login.php
because this command gets executed by the server header('Location: login.php');
before the html view is returned to the client. This means when you load this php script the client will recieve the html file created by login.php
(unless this script also contains a forward to another script)
Upvotes: 0
Reputation: 74220
As per my theory and Pastebin.com file at http://pastebin.com/439xPdJN
Here is a working demo with 2 files, and an example in order to show you that it can be done.
Modify to suit.
First, some instructions on how to use it:
You will need to to reload the page (session1.php) a few times in order to get the number up.
Then, you will notice the page view count will go back to ZERO once you confirm the logout button.
Credit goes out to: (felipsmartins) for his JS example.
The code:
Let's call this session1.php file
<?php
session_start();
if(isset($_SESSION['views']))
$_SESSION['views'] = $_SESSION['views']+ 1;
else
$_SESSION['views'] = 1;
echo "views = ". $_SESSION['views'];
?>
<!doctype html>
<head>
</head>
<body>
<script type="text/javascript">
function logoutck() {
var r = confirm("Do you really want to log out?");
if (r) {
window.location.href = 'logout.php'
}
}
</script>
<input id="button1" type='button' onclick='logoutck();' value='LOGOUT'/>
</body>
</html>
Let's call this logout.php file
<?php
session_start();
if(isset($_SESSION['views']))
unset($_SESSION['views']);
header("Location: session1.php");
?>
Upvotes: 2
Reputation: 13559
You can to do it:
function logoutck() {
var r = confirm("Do you really want to log out?");
if (r) {
window.location.href = 'http://site.com/logout.php'
}
}
Upvotes: 2
Reputation: 30488
It will not work because javascript
runs at client-side and PHP
runs at server-side.
You can use AJAX
call for destroying session.
More info on how to kill session from javascript
Upvotes: 1