如何使用 Jquery/Javascript 清除 PHP 会话?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24923413/
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
How to clear a PHP session using Jquery/Javascript?
提问by Jerielle
Just want to ask if is it possible to clear a PHP session using a jquery or javascript process? Because what I want to do is to clear the PHP session using a dialog box. If the user click the 'OK' button it will process the unset function. In my page there's a checkout process and after the user click the button 'OK' it will prompt a dialog box and if the user click the 'OK' button it will unset the PHP session and it will be redirected to another page.
只是想问一下是否可以使用 jquery 或 javascript 进程清除 PHP 会话?因为我想要做的是使用对话框清除PHP会话。如果用户单击“确定”按钮,它将处理未设置的功能。在我的页面中有一个结帐过程,在用户单击“确定”按钮后,它将提示一个对话框,如果用户单击“确定”按钮,它将取消设置 PHP 会话并将其重定向到另一个页面。
Here's my simple code:
这是我的简单代码:
<i class="fa fa-check"></i> <span id="save_registry" class="cursor: pointer">OK</span>
<div id="save_alert" style="display: none">
<?php unset($this->session->data['cart']);
</div>
....
$('#save_registry').click(function(){
$('#save_alert').dialog({
modal: true,
buttons: {
Ok: function() {
$( this ).dialog( "close" );
location = 'index.php?route=common/home';
}
}
});
});
回答by arielnmz
Simply put: You can't, directly.
简单地说:你不能,直接。
PHP is a server-sidelanguage while javascript is a client-sideone, which means that there's no other connection between them other than the document you receive and interacts with you. The sessions are stored and managed by the server.
PHP 是一种服务器端语言,而 javascript 是一种客户端语言,这意味着除了您收到并与您交互的文档之外,它们之间没有其他连接。会话由服务器存储和管理。
You can, however, as War10ck suggests, call to a server-side script that clears the session. Something like this:
但是,正如 War10ck 建议的那样,您可以调用清除会话的服务器端脚本。像这样的东西:
PHP:
PHP:
<?php
/*
* session_start();
* Starting a new session before clearing it
* assures you all $_SESSION vars are cleared
* correctly, but it's not strictly necessary.
*/
session_destroy();
session_unset();
header('Location: continue.php');
/* Or whatever document you want to show afterwards */
?>
HTML/javascript:
HTML/JavaScript:
<script type="text/javascript">
function logout() {
document.location = 'logout.php';
}
LogoutButton.addEventListener('click', logout, false);
</script>
<button id="LogoutButton">Log out</button>
Or even performing an asynchronous call:
甚至执行异步调用:
function logout() {
var xhr = new XMLHttpRequest();
xhr.onload = function() {
document.location = 'continue.php';
}
xhr.open('GET', 'logout.php', true);
xhr.send();
}
回答by Khalid
There is a way you can do it directly from javascript... first you have to declare a function that clears a cookie by its key
有一种方法可以直接从 javascript 中完成...首先,您必须声明一个通过其键清除 cookie 的函数
function removeCookie(cookieName)
{
cookieValue = "";
cookieLifetime = -1;
var date = new Date();
date.setTime(date.getTime()+(cookieLifetime*24*60*60*1000));
var expires = "; expires="+date.toGMTString();
document.cookie = cookieName+"="+JSON.stringify(cookieValue)+expires+"; path=/";
}
Now all you have to do is to remove a cookie with the key "PHPSESSID
" like this
现在你所要做的就是PHPSESSID
像这样删除一个带有“ ”键的cookie
removeCookie("PHPSESSID");
Now when you var_dump($_SESSION)
you find it empty array
现在当你var_dump($_SESSION)
发现它是空数组时
回答by Kyle Emmanuel
You can't clear a php session using Javascript directly.
您不能直接使用 Javascript 清除 php 会话。
PHP is a server-side language, whereas Javascript is client-side..
PHP 是服务器端语言,而 Javascript 是客户端语言。
However, you can throw an ajax request to the specified page that handles the destruction.
但是,您可以向处理销毁的指定页面抛出 ajax 请求。
Like this:
像这样:
// Client-side
$.post("logout.php", {"can_logout" : true}, function(data){
if(data.can_logout)
// redirect the user somewhere
}, "json");
<?php
// Server-side
$can_logout = $_POST["can_logout"];
if($can_logout)
session_destroy();
echo json_encode(array("can_logout" => true));
?>
Or just point the user to some page that handles the session destruction; i.e. logout.php
或者只是将用户指向某个处理会话销毁的页面;IElogout.php