我想在用户单击“注销”时销毁会话,我试图通过使用jQuery调用函数onClick来执行此操作,然后使用jQuery调用包含session_destroy();的.PHP文件session_destroy(); 。
的index.php
<?php if(isset($_SESSION['username'])) { echo "<li><a href='#' id='logOut' onclick='logOut();'>Log Out</a></li>"; } ?>这使得注销按钮仅在用户登录时以及onClick应该调用logOut()中包含的logOut()时可见。
Nav.js
function logOut() { $.get("../Controller/logOut.php"); return false; }logOut.php
<?php session_destroy(); ?>当我点击“注销”时,似乎没有任何事情发生,我在Chrome开发者控制台中没有收到任何错误。 问题是什么?
I am looking to destroy the session when a user clicks "Log Out", I am trying to do this by calling a function onClick using jQuery which will then call a .PHP file which contains, session_destroy();.
index.php
<?php if(isset($_SESSION['username'])) { echo "<li><a href='#' id='logOut' onclick='logOut();'>Log Out</a></li>"; } ?>This makes the log out button visible only when a user is logged in and when onClick should call logOut() which is contained in Nav.js.
Nav.js
function logOut() { $.get("../Controller/logOut.php"); return false; }logOut.php
<?php session_destroy(); ?>When I click "Log Out" nothing seems to happen and I am not getting any errors in Chrome Dev console. What is the problem?
最满意答案
你必须在调用session_destroy()之前启动会话
<?php session_start(); session_destroy(); header("Location: login.php"); //redirect your page to login page ?>You must have to start session before calling session_destroy()
<?php session_start(); session_destroy(); header("Location: login.php"); //redirect your page to login page ?>更多推荐
发布评论