如何提取该会话中登录用户的当前user_id?

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

Login.php我一直试图做的是提取登录用户的会话ID,并在另一页上调用它,以便将user_id存储在注释表中。但是,当我尝试这样做时,它在下一页中仅输出0,而不是用户的ID。

<?php
session_start();
require "../layouts/header.php";//including header
require '../database/db.php';//db connection

    if (isset($_POST['login'])) {
        if(empty($_POST["username"]) || empty($_POST["password"]))
        {
            $msg = '<label style="color:red;"> Missing fields!</label>';
        }
        else
        {
            //checking if the provided credential match with the database
            $query = "SELECT * FROM users where username=:username";
            $statement = $db->prepare($query);
            //criteria for successful login
            $condition = [
                'username'=>$_POST['username']

            ];
            $statement->execute($condition);

            $count = $statement->rowCount();
            if($count > 0)
            {                   
                $users=$statement->fetch(PDO::FETCH_OBJ);
                $pwd = $_POST['password'];
                    //verification of password
                    if (password_verify($pwd, $users->password))
                    {
                        $type = $_POST['type'];
                        $_SESSION['username'] = $_POST['username'];
                        foreach( $users as $view):
                        $_SESSION['user_id'] = $view['user_id'];

                        if($type==$view['type'] && $type == 1)
                        {
                            header("location:../admin/index.php"); 
                        }
                        if($type==$view['type'] && $type == 0)
                        {
                            header("location:../client-side/index.php?user_id=<?= $view->user_id; ?>");
                        }
                        else
                        {
                            echo "Invalid!";
                        }    
                        endforeach; 
                    }
                    else{
                        echo '<label style="color:red;"> Invaid Entry!</label>';
                    }
            }
            else
            {
                $msg = '<label style="color:red;"> Invaid Entry!</label>';
            }
        }
    }

?>登录表单

<main class="home" style="margin-bottom: 30px; margin-top:30px;">
    <h2 style="text-align: center">Login</h2>
    <hr>

    <form action="login.php" method="POST">

        <label>Username:</label>
        <input type="text" name="username" placeholder="Enter Username"><br>
        <label>Password:</label>
        <input type="password" name="password" placeholder="Enter Password"><br>
        <label><strong>Please select one:</strong></label> 
        <label>Admin</label>
        <input type="radio" name="type" id="Admin" value="1" style="margin-top: 33px; height:20px;width:20px;"><br>
        <label>Client</label>
        <input type="radio" name="type" id="Client" value="0" style="margin-top: 33px; height:20px;width:20px;"><br>
        <input type="submit" name="login" value="Login">
    </form>

    <div style="height: 200px;"></div>

    <br>
</main>

    <?php            
    require "../layouts/footer.php"; 
    ?>

index.php索引页面仅输出0而不是user_id

<?php
    if(isset($_SESSION["user_id"]))
    {
        echo '<h1> Welcome - '.$_SESSION["user_id"].'</h1>';

    }
?>
php mysql session session-variables userid
1个回答
0
投票

我已经在自己的计算机上测试了您的代码,看来您的问题是您遇到的foreach循环。我认为您不需要运行foreach,因为您已经使用PDO :: FETCH_OBJ提取了单个对象。

此外,您正在尝试从$ view类以数组形式访问user_id属性。

我认为对您的代码进行简单的修改就足够了。请注意,您将获得用户名,因为您是在foreach循环之前直接从$ _POST变量将其分配给会话。

<?php
session_start();
require "../layouts/header.php";//including header
require '../database/db.php';//db connection

    if (isset($_POST['login'])) {
        if(empty($_POST["username"]) || empty($_POST["password"]))
        {
            $msg = '<label style="color:red;"> Missing fields!</label>';
        }
        else
        {
            //checking if the provided credential match with the database
            $query = "SELECT * FROM users where username=:username";
            $statement = $db->prepare($query);
            //criteria for successful login
            $condition = [
                'username'=>$_POST['username']

            ];
            $statement->execute($condition);

            $count = $statement->rowCount();
            if($count > 0)
            {                   
                $users=$statement->fetch(PDO::FETCH_OBJ);
                $pwd = $_POST['password'];
                    //verification of password
                    if (password_verify($pwd, $users->password))
                    {
                        $type = $_POST['type'];
                        $_SESSION['username'] = $_POST['username'];

                        $_SESSION['user_id'] = $user->user_id;

                        if($type==$view['type'] && $type == 1)
                        {
                            header("location:../admin/index.php"); 
                        }
                        if($type==$view['type'] && $type == 0)
                        {
                            header("location:../client-side/index.php?user_id=<?= $user->user_id; ?>");
                        }
                        else
                        {
                            echo "Invalid!";
                        }    
                    }
                    else{
                        echo '<label style="color:red;"> Invaid Entry!</label>';
                    }
            }
            else
            {
                $msg = '<label style="color:red;"> Invaid Entry!</label>';
            }
        }
    }
© www.soinside.com 2019 - 2024. All rights reserved.