PHP:如何只允许注册用户到另一个页面

问题描述 投票:0回答:2

我试图建立一个小项目,只允许注册用户登录到网站中的另一页,我有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>

我在MySQL数据库中的“用户”表按名称“newschool”存储数据

enter image description here

我可以在数据库中注册的用户,我可以检索由代码数据:

<?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,所以如果可能的一点解释。

php html css
2个回答
0
投票
if(empty($_SESSION['userInfo']))
{
    header("Location: login.php");
}

你可以把检查这样的事情每一页上


0
投票

你必须设置cookie变量或会话变量时的细节与数据库进行匹配和你有检查,有在变量值的页面的顶部,每次如果出现在变量的值,则用户可以去主页否则用户定向上的登录页面。每一页上粘贴代码(在页面的顶部)

     if(empty($_SESSION['userinfo'])
         {
          header("Location:Login.php");
         }
else
{

header("Location:home.php");
}

在登录页之后的语句返回true或让行数

 .......
    $stmt->execute()
    if($query->rowCount() > 0) {
      $_SESSION['username'] = $user;
      header('location:home.php');
    } else {
      header('location:login.php');
    }

.......

使用这个在你的代码我相信,这将帮助你。

© www.soinside.com 2019 - 2024. All rights reserved.