我试图创建一个小项目,仅允许注册用户登录站点中的另一个页面,我有3个PHP页面:
注册页面 登录页面 主页
注册PHP页面
<?php
if(isset($_POST['reg'])){
$name = $_POST['name'];
$age = $_POST['age'];
$address = $_POST['address'];
$password = md5($_POST['password']);
$con = new PDO("mysql:host=localhost;dbname=newschool","admin","admin");
$affected = $con->exec("insert into users (name,age,address,password) values('$name',$age,'$address','$password') ");
if($affected > 0){
echo "Your data has been added successfully";
}
}
?>
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<link rel="stylesheet" href="css/bootstrap.min.css">
</head>
<body>
<div class="container">
<form action="#" method="post">
<div class="form-group">
<label for="name">Username:</label>
<input type="text" id="name" name="name" class="form-control">
</div>
<div class="form-group">
<label for="age">Age:</label>
<input type="number" id="age" name="age" class="form-control">
</div>
<div class="form-group">
<label for="address">Address:</label>
<textarea id="address" name="address" class="form-control"></textarea>
</div>
<div class="form-group">
<label for="password">Password:</label>
<input type="password" id="password" name="password" class="form-control">
</div>
<button name="reg" class="btn btn-primary btn-block">Register</button>
</form>
</div>
<script src="js/jquery.js"></script>
<script src="js/bootstrap.min.js"></script>
</body>
</html>
和登录页面:
<?php
session_start();
if(isset($_POST['b_login'])){
$username =$_POST['username'];
$password =$_POST['password'];
try{
$db = new PDO('mysql:host=localhost;dbname=newschool','admin','admin');
$stm = $db->prepare(" select * from users where name= ? and password = ? ");
$stm->execute([$username,$password]);
if(empty($_SESSION['userinfo']))
{
header("Location:Login.php");
}
else
{
header("Location:home.php");
}
}catch (PDOException $ex){
}
}
?>
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<link rel="stylesheet" href="css/bootstrap.min.css">
</head>
<body>
<div class="container">
<form action="#" method="post">
<div class="form-group">
<label for="username">Username</label>
<input type="text" id="username" name="username" class="form-control">
</div>
<div class="form-group">
<label for="password">Password</label>
<input type="password" id="password" name="password" class="form-control">
</div>
<button name="b_login" class="btn btn-primary">Login</button>
</form>
</div>
<script src="js/jquery.js"></script>
<script src="js/bootstrap.min.js"></script>
</body>
</html>
我通过名称“ newschool”将数据存储在MySQL数据库的“用户”表中
我可以在数据库中注册用户,并且可以通过代码检索数据:
<?php
try{
$con = new PDO("mysql:host=localhost;dbname=newschool","admin","admin");
$users = $con->query("select * from users");
}catch(PDOException $e){
echo "try again";
}
?>
问题是,当我在登录页面中输入用户名和密码时,它将用户转发到主页!
请注意,我是PHP的新手,所以如有可能,请作一些说明。
答案 0 :(得分:1)
这是一个较晚的响应,但是对于将来的开发人员来说,如果您碰巧遇到了这个我认为不会的问题,答案很简单,可以在第5步中找到,但请看您的代码和屏幕快照您似乎还不了解Login Systems在PHP中的工作方式,因此我编排了本指南,强烈建议您在继续操作之前先阅读所有内容。
登录系统通过首先检查用户输入是否与数据库中的用户名,名称或ID相匹配来进行工作。
$stmt = $con->prepare('SELECT * FROM users WHERE name = ?');
然后从准备好的语句中进行绑定,并确保存储结果。
$stmt->bind_param('s', $_POST['name']);
$stmt->execute();
// Store the result so we can check it later
$row = $stmt->fetch();
// Checks if result exists
if($row){
// use the 'password verify' only if you encrypt your password in your database if not just do $_POST['password'] == $password (not recommended)
if(password_verify($_POST['password'], $password)){
// no need to include the $_POST['password'] in your prepared statement since you will not be executing a SQL command to check for a match of passwords
} else {
// redirect if password is not a match to the ones in the database
header("Location: login.php?error=IncorrectPassword");
}
} else {
// redirects to login page
header("Location: login.php?error=UsernameNotFound");
}
$_SESSION['loggedin'] = TRUE;
$_SESSION['name'] = $row['name'];
$_SESSION['username'] = $row['username'];
// Then redirect to your homepage
header("Location: index.php");
// checks for session when you enter index.php
if(!isset($_SESSION['username']) || $_SESSION['loggedin'] !== TRUE){
// redirects you if sessions are not present
header("Location: login.php");
}
最后,永远不要忘记将session_start();
添加到所有页面的开头,否则会话将无法进行。
答案 1 :(得分:0)
if(empty($_SESSION['userInfo']))
{
header("Location: login.php");
}
您可以在每个页面上进行类似的检查
答案 2 :(得分:0)
当详细信息与数据库匹配时,您必须设置cookie变量或会话变量,并且您每次都要在页面顶部检查变量中是否有值,如果变量中有值,那么用户可以转到主页,否则将用户定向到登录页面。 在每个页面上(页面顶部)粘贴代码
if(empty($_SESSION['userinfo'])
{
header("Location:Login.php");
}
else
{
header("Location:home.php");
}
在登录页面中的after语句返回true或使行计数
.......
$stmt->execute()
if($query->rowCount() > 0) {
$_SESSION['username'] = $user;
header('location:home.php');
} else {
header('location:login.php');
}
.......
在您的代码中使用 我相信这会对您有所帮助。