创建JavaScript函数以销毁php会话 [英] Creating javascript function to destroy php session
问题描述
我在弄清楚如何创建将破坏php会话的javascript函数时遇到了麻烦.我有一个可点击的链接,它将调用一个称为destroyphpsess的函数.我认为到目前为止还可以.现在,我需要定义javascript函数.这是我到目前为止的代码...
I am having trouble figuring out how to create a javascript function that will destroy a php session. I have a clickable link that will call a function called destroyphpsess. I think this is all right so far. Now I need to define the javascript function. This is the code I have so far...
if ($_SESSION['color'] == "") {
$var = "<a href='JavaScript:newPopup(\"http://www.yourfantasyfootballreality.com/register.php\");' class='two'>Register</a> | <a href='JavaScript:newPopup(\"http://www.yourfantasyfootballreality.com/signin.php\");' class='two'>Sign In</a>";
} else {
$var = "Hello, ".$_SESSION['color'] ."! | " . "<a href=\"http://www.yourfantasyfootballreality.com/index.php\" onclick=\"destroyphpsess()\" class='two'>Log Out</a>";
}
echo $var;
现在,我需要定义javascript函数.这是我遇到麻烦的地方.这是我到目前为止的基本轮廓...
Now I need to define the javascript function. This is where I am having trouble. This is the basic outline I have so far...
function destroyphpsess()
{
<?php
session_destroy();
?>
}
如果有人可以帮助我使用该功能,我将不胜感激!谢谢.
If someone could help me with the function I would appreciate it! Thanks.
推荐答案
您的JavaScript运行在客户端,而您的PHP运行在服务器端.您不能以这种方式从JavaScript调用PHP函数.您有两种选择:
Your JavaScript runs client-side, and your PHP runs server-side. You can't call PHP functions from JavaScript this way. You have two options:
- 对服务器端脚本进行AJAX调用,以清除会话数据(为确保一致性并正确清除服务器端的内容,建议这样做)
- 使用JavaScript 清除PHPSESSID cookie(如果重新配置会话处理方式或URL参数处理会话,则无效)
这篇关于创建JavaScript函数以销毁php会话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!