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');
?>
}
}
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');
?>
}
}
Share
Improve this question
edited Oct 19, 2018 at 8:17
Alessandro
90812 silver badges23 bronze badges
asked Jul 23, 2013 at 4:30
user2455835user2455835
131 gold badge1 silver badge3 bronze badges
9
- 1 It is not possible to bine Javascript and PHP this way. – invisal Commented Jul 23, 2013 at 4:32
- Learn what is server side / client side code.... Use ajax for that but learn before how to use PHP – Dorian_gd Commented Jul 23, 2013 at 4:34
-
I see this error way too many times on SO. I don't get it! If you have any minimal
PHP(HTTP)/HTML/JS/CSS
knowledge, which you should as a webdev, you cannot make this mistake. This is the result of learning the basics from online tutorials vs. reading books. So go read a book! – CodeAngry Commented Jul 23, 2013 at 4:38 - Why not redirect to other page ? – NullPoiиteя Commented Jul 23, 2013 at 4:49
- Thanks for all the helpful answers, I see my mistake. @CodeAngry - I'm not a webdev, does that allow me to make a mistake? Just curious, did you get where you are without ever having made a mistake? I'm envious! – user2455835 Commented Jul 23, 2013 at 11:09
4 Answers
Reset to default 2As per my theory and Pastebin. file at http://pastebin./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");
?>
You can to do it:
function logoutck() {
var r = confirm("Do you really want to log out?");
if (r) {
window.location.href = 'http://site./logout.php'
}
}
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
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/>'; ?>
bees <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 mands it tries to execute it. So when a javascript mand gets executed there is no more php mands 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 mand 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)