【发布时间】:2014-07-24 20:36:32
【问题描述】:
我有一个脚本,当用户通过验证时,他/她会被带到Home.php。目前Home.php 做的不多。但是在左下角我有一个注销按钮。如您所知,当用户单击此按钮时,他希望他的会话被破坏并被重定向到登录页面。不幸的是,您无法在php 中创建点击监听器。我已经浏览了一个小时寻找解决方案,但我无法找到正确的关键词或其他东西。
这是我的代码
编辑:您只需要从Home.php 读取一些代码,只有当某人不确定自己的答案时才想运行代码
Index.php(登录页面)
<?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>
Home.php
<?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>
根据我在四处搜索中发现的信息,我需要一个带有 ajax 调用的 Home.js 文件。我不知道关于 Ajax 的第一件事,所以我可能需要粘贴代码或非常生硬的教程。
谢谢
【问题讨论】:
-
css 代码如何与 ajax 和 session 相关联?
-
<button id="LogOut" onclick="jsfunction()">Log Out</button>然后让 jsfunction() 注销用户并通过 Ajax 重定向用户。 -
危险:您使用的是an obsolete database API,应该使用modern replacement。您也容易受到SQL injection attacks的影响,现代 API 可以让您更轻松地从 defend 中获得。
-
tks @Quentin 我只需要它在场,这样我就不会因为安全而陷入困境
标签: javascript php ajax session event-listener