javascript 使用javascript注销php会话

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17801522/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 09:41:09  来源:igfitidea点击:

logout php session using javascript

phpjavascriptlogout

提问by user2455835

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.

我正在尝试使用 javascript 注销 php 会话。它不起作用 - 调用了 javascript 函数并且 if 语句起作用,但没有调用 php 脚本。有没有更好的方法来做到这一点?我正在使用 .php 页面。

function logoutck() 
    {
    var r = confirm("Do you really want to log out?");
    if (r==true)
        {
        <?php
        session_start();
        session_destroy();
        header('Location: login.php');
        ?>
        } 
    }

回答by Funk Forty Niner

As per my theory and Pastebin.com file at http://pastebin.com/439xPdJN

根据我的理论和 Pastebin.com 文件在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.

这是一个包含 2 个文件的工作演示,以及一个示例,以向您展示它可以完成。

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.

您需要重新加载页面 (session1.php) 几次才能使数字上升。

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.

幸得出:felipsmartins为他的JS例子。

The code:

代码:

Let's call this session1.phpfile

让我们调用这个session1.php文件

<?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.phpfile

让我们称这个logout.php文件

<?php

session_start();

if(isset($_SESSION['views']))
    unset($_SESSION['views']);

header("Location: session1.php");

?>

回答by felipsmartins

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

回答by Yogesh Suthar

It will not work because javascriptruns at client-side and PHPruns at server-side.

它不会工作,因为javascript在客户端PHP运行并在服务器端运行。

You can use AJAXcall for destroying session.

您可以使用AJAXcall 来销毁会话。

More info on how to kill session from javascript

有关如何从 javascript 终止会话的更多信息

回答by Joel Harkes

How javascript, html and php work:

javascript、html 和 php 的工作原理:

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.

服务器首先从上到下执行php脚本。它执行脚本中的所有语句并构建一个 html 页面,因此:<?php echo '<h1>hello world<h1/>'; ?>变成<h1>hello world<h1/>. 当服务器执行完所有内容后,它会返回创建的 html 页面。

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.

该页面然后由浏览器加载,然后当浏览器看到 javascript 命令时,它会尝试执行它。因此,当执行 javascript 命令时,不再有 php 命令,因为 php 服务器已经执行了它们并从中生成了 html 视图。

the following statements are the same for a php server

以下语句对于 php 服务器是相同的

<?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.phpbecause 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)

所以请注意,当你的 php 脚本被执行时,你会去,login.php因为这个命令header('Location: login.php');在 html 视图返回给客户端之前由服务器执行。这意味着当你加载这个 php 脚本时,客户端将收到由它创建的 html 文件login.php(除非这个脚本还包含一个转发到另一个脚本)