【发布时间】:2017-08-17 01:57:42
【问题描述】:
我是 PHP 编程新手,我有用于用户登录的代码,并且已经连接到 localhost 数据库。
这是Login.php代码
<html>
<head>
<title>Login Page</title>
</head>
<body>
<div>
<form action="doLogin.php" method="POST">
<p>
<label>Username : </label>
<input type="text" id="username" name="user">
</p>
<p>
<label>Password : </label>
<input type="text" id="password" name="pass">
</p>
<p>
<input type="submit" id="btnLogin" value="Login">
</p>
</form>
</div>
</body>
</html>
和doLogin.php
<?php
$username = $_POST["user"];
$password = $_POST["pass"];
$username = stripcslashes($username);
$password = stripcslashes($password);
$con = mysqli_connect('localhost', 'root', '', 'dbtest');
$query = mysqli_query($con, "select * from user where Username = '$username' and Password = '$password' ");
$row = mysqli_fetch_array($query);
if ($row['Username'] == $username && $row['Password'] == $password)
{
echo "You are Success Login!!! Welcome ".$row['Username'];
header('Location: Profile.php');
}
else
{
echo "Failed to login!";
}
?>
我想使用session,所以如果用户没有登录,它将重定向到登录页面。我在哪里以及如何将会话应用到我的代码中,非常感谢。
【问题讨论】:
-
在顶部。 session_start();
-
你可以用谷歌搜索一下。也不要直接将输入中的数据发布到您的查询中。阅读更多关于 PHP 中的 Santizing 输入。另外,阅读更多关于在 PHP 中使用准备好的语句