Javascript 需要Ajax调用来销毁会话吗
我有一个脚本,当用户得到验证时,他/她会被带到Javascript 需要Ajax调用来销毁会话吗,javascript,php,ajax,session,event-listener,Javascript,Php,Ajax,Session,Event Listener,我有一个脚本,当用户得到验证时,他/她会被带到Home.php。目前,Home.php做的不多。但在左下角我有一个注销按钮。正如你所知,当用户点击这个按钮时,他希望他的会话被破坏,并被重定向到登录页面。不幸的是,您无法在php中创建单击侦听器。我已经浏览了一个小时,寻找解决方案,但没有找到正确的关键字或其他东西。 这是我的密码 编辑:你只需要从Home.php中阅读一些代码,只有当一些人不确定自己的答案而想要运行代码时,才需要est Index.php(登录页面) 根据我在搜索中发现的内容,我需
Home.php
。目前,Home.php
做的不多。但在左下角我有一个注销按钮。正如你所知,当用户点击这个按钮时,他希望他的会话被破坏,并被重定向到登录页面。不幸的是,您无法在php
中创建单击侦听器。我已经浏览了一个小时,寻找解决方案,但没有找到正确的关键字或其他东西。
这是我的密码
编辑:你只需要从Home.php
中阅读一些代码,只有当一些人不确定自己的答案而想要运行代码时,才需要est
Index.php
(登录页面)
根据我在搜索中发现的内容,我需要一个带有ajax调用的Home.js
文件。我不知道Ajax的第一件事,所以我可能需要粘贴代码或一个非常简单的教程。
谢谢您可以将logout href更改为/logout.php,并且在logout.php中
这将简单地销毁用户当前会话,然后将用户重定向回Index.php页面
AJAX的方式是(使用jQuery,我记不起AJAX调用的普通JS语法)
$.ajax({
键入:“GET”,
url:“/Logout.php”,
成功:功能(msg){
如果(msg='loggedOut'){
window.location.href='Index.php';
}
}
});
然后,您需要更改Logout.php,而不是标题行,使其成为echo/die/print
loggedOut
(或者一个json字符串,可能会更好,但这只是一个示例)。css代码如何与ajax和会话相关?您是否可以不只是这样做,然后在index.php的顶部使用:if(isset($\u GET['Logout')){session_destroy();}-不需要ajax。然后会向用户显示登录表单。注销
然后生成jsfunction()将用户注销并通过Ajax重定向用户。危险:您正在使用并且应该使用。您还容易受到现代API的攻击,因为它会使您从中更轻松。tks@Quentin我只需要它在场,这样我就不会陷入SeurityTanks的泥潭我整个上午都在寻找类似的东西没有问题,别忘了答案他如果它解决了你的问题,请你投票表决并接受它。
<?php
session_start();
mysql_connect("localhost","root","") or die ("cannot");
mysql_select_db("virtualdiary") or die ("db");
if (isset ($_POST["Username"])&& isset($_POST["Password"]))
{
$Username = $_POST["Username"];
$Password = $_POST["Password"];
$_SESSION["username"] = $Username;
$DB_Check = " SELECT * from users Where username = '".$Username."' and password = '".$Password."' " ;
$result = mysql_query($DB_Check);
if(mysql_fetch_assoc($result) === false){
$error = "invalid username or password";
}else{
header( 'Location: Home.php' ) ;
}
}
?>
<html>
<head>
<link type="text/css" rel="stylesheet" href="Index.css"/>
<title>Login</title>
</head>
<body>
<div id="main">
<div class="messages">
<?php
if(isset($error))
echo $error; ?>
</div>
<form action="Index.php" method="post">
<h5>Diary Name:</h5>
<input name="Username" type="text"/>
<h5>Password:</h5>
<input name="Password" type="password"/>
</br>
</br>
</br>
<input name="login" type="submit"/>
</form>
<p>Click <a href="Register.php">HERE </a>to register.</p>
</div>
</body>
</html>
<?php
session_start();
echo "Username = " . $_SESSION["username"] . " !";
mysql_connect("localhost","root","") or die ("cannot");
mysql_select_db("virtualdiary") or die ("db");
if (isset($_POST["entry"])){
$entry = $_POST["entry"];
$submission = "INSERT INTO `virtualdiary`.`entries` (`entry`) VALUES ('". $entry . "')";
mysql_query($submission);
}
?>
<html>
<head>
<link type="text/css" rel="stylesheet" href="Home.css"/>
<title>Home</title>
</head>
<body>
<h1>Entry: </h1>
<form method="post" action="Home.php">
<textarea name="entry" rows="24" cols="87">
<?php
if (isset($_POST["entry"])){
echo $entry;
}
?>
</textarea>
</br>
</br>
<input name="submit" type="submit"/>
</form>
<button id="LogOut"><a href="Index.php">Log Out</a></button>
</body>
</html>