运行PHP onClick

编程入门 行业动态 更新时间:2024-10-25 22:32:20
运行PHP onClick - session_destroy(Run PHP onClick - session_destroy)

我想在用户单击“注销”时销毁会话,我试图通过使用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 ?>

更多推荐

本文发布于:2023-07-24 20:12:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1250593.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:PHP   onClick

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!