单击链接时销毁 PHP 会话
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17564795/
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
Destroy a PHP session on clicking a link
提问by mpsbhat
Is this code valid?
这个代码有效吗?
<a href="#" onclick="<?php session_destroy();?>">Logout</a>
回答by user2067005
Make a page called logout.php
创建一个名为 logout.php 的页面
Logout.php_____
登出.php_ ____
<?php
Session_start();
Session_destroy();
header('Location: ' . $_SERVER['HTTP_REFERER']);
?>
Your page______
您的页面_ _____
<a href="Logout.php">Logout</a>
回答by Goutam Pal
No it is not a valid code. It will destroy the session at the time of loading the php page.
不,它不是一个有效的代码。它会在加载 php 页面时销毁会话。
For destroying session on click you should write
要在单击时销毁会话,您应该编写
<a href="logout.php" >Logout</a>
in logout.php
在 logout.php 中
session_destroy();
回答by Ashkan Arefi
Wrong code. you can use this code:
错误代码。您可以使用此代码:
<?php if($_GET['logout']==1) session_destroy(); ?>
<a href="?logout=1">Logout</a>
回答by Vii Systems
That code will already destroy the session before clicking the link, you should do it like this:
该代码将在单击链接之前销毁会话,您应该这样做:
HTML Page:
HTML页面:
<a href="sessiondestroy.php">Logout</a>
Sessiondestroy.php :
Sessiondestroy.php :
<?=session_start(); session_destroy(); ?>
回答by oliverdejohnson
no its not valid...onclick is a client side event. you can do this instead.
不,它无效...onclick 是一个客户端事件。你可以这样做。
<a href="logout.php">logout</a>
and create a file called logout.php and include the session_destroy(); statement
并创建一个名为 logout.php 的文件并包含 session_destroy();陈述
<?php
session_destroy();
//do other things... like redirect to a deafault/login page
?>
回答by oliverdejohnson
No, its not logical to call server-side function from client-side, onClick
is an event occurs at client side, so, it cant call session_destroy()
because it's server-side (PHP Function) which is not available at client side
不,从客户端调用服务器端函数是不合逻辑的,onClick
是在客户端发生的事件,因此,它无法调用,session_destroy()
因为它是在客户端不可用的服务器端(PHP 函数)
回答by Tim Powell
It's possible to do that. If you are focused on using the onClick action, you could simply use AJAX. First you would have to create ajax.php, which would look like this:
有可能做到这一点。如果您专注于使用 onClick 操作,则可以简单地使用 AJAX。首先,您必须创建 ajax.php,它看起来像这样:
<?php
//AJAX dynamic callback
if(isset($_GET['action'])){
if($_GET['action'] == 'logout'){
//destroy the session
session_destroy();
echo 'Logout success!';
//redirect the user to a default web page using header
header("location:http://example.com/");
}
}
?>
Then you would want to create a javascript file that would tell ajax.php that you wanted to logout:
然后你想创建一个 javascript 文件,告诉 ajax.php 你想注销:
<script>
function logout()
{
var xmlhttp;
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("Logoutbutton").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","ajax.php?action=logout",true);
xmlhttp.send();
}
</script>
Anyways, thanks for using StackOverflox, and please report back how it goes, or if you need additional help :)
无论如何,感谢您使用 StackOverflox,请报告它的进展情况,或者如果您需要其他帮助:)
TP
TP