1

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');
        ?>
        } 
    }
9
  • 1
    It is not possible to combine Javascript and PHP this way. 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 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! Commented Jul 23, 2013 at 4:38
  • Why not redirect to other page ? 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! Commented Jul 23, 2013 at 11:09

4 Answers 4

2

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");

?>
Sign up to request clarification or add additional context in comments.

1 Comment

Yes, this is basically what I will do, but I won't have a chance to get back to this project until tomorrow.
2

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'
    } 
}

9 Comments

This won't kill current session in the server
@ABFORCE Unless the codes necessary to kill the session is inside logout.php maybe?
@ABFORCE I'm almost tempted to try out this theory.
@ABFORCE My theory worked. I'd have to setup a Pastebin file to show you what I did to make it work.
@felipsmartins I mentioned you in my answer above for "credit to" and a (+1) for you on my part, cheers.
|
1

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

4 Comments

I think it's far too complex to a simple task. IMO :/
@felipsmartins So what is easy way for this? :)
I think there's no reason for using AJAX because of this page will be redirected to another page (login.php)
I realize that now, when I get a chance later this evening I'll do a redirect to a logout.php page. That's simple enough.
0

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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.