Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/279.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/68.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何提取登录该会话的用户的当前用户id?_Php_Mysql_Session_Session Variables_Userid - Fatal编程技术网

Php 如何提取登录该会话的用户的当前用户id?

Php 如何提取登录该会话的用户的当前用户id?,php,mysql,session,session-variables,userid,Php,Mysql,Session,Session Variables,Userid,Login.php 我一直试图做的是提取登录用户的会话id,并在另一个页面上调用它,以便将用户id存储在comments表中。但是,当我尝试这样做时,它在下一页中只输出0,而不是用户ID <?php session_start(); require "../layouts/header.php";//including header require '../database/db.php';//db connection if (isset($_POST['login'])) {

Login.php
我一直试图做的是提取登录用户的会话id,并在另一个页面上调用它,以便将用户id存储在comments表中。但是,当我尝试这样做时,它在下一页中只输出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,而不是用户id

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

    }
?>

我已经在自己的机器上测试了您的代码,似乎您的问题在于您的foreach循环。我认为您不需要运行foreach,因为您已经使用PDO::FETCH_OBJ获取了单个对象

另外,您正试图从$view类以数组的形式访问user_id属性

我认为对代码进行简单的修改就足够了。请注意,您将获得用户名,因为您直接从foreach循环之前的$\u 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==$user->type && $type == 1)
                        {
                            header("location:../admin/index.php"); 
                        }
                        if($type==$user->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>';
            }
        }
    }

我已经在自己的机器上测试了您的代码,似乎您的问题在于您的foreach循环。我认为您不需要运行foreach,因为您已经使用PDO::FETCH_OBJ获取了单个对象

另外,您正试图从$view类以数组的形式访问user_id属性

我认为对代码进行简单的修改就足够了。请注意,您将获得用户名,因为您直接从foreach循环之前的$\u 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==$user->type && $type == 1)
                        {
                            header("location:../admin/index.php"); 
                        }
                        if($type==$user->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>';
            }
        }
    }

我假设您想显示
john.doe
而不是
0
(根据您的代码,它只是数据库中的行ID)?。如果是这样,为什么不使用
$\u SESSION['username']
变量呢?是否将
SESSION\u start()
放在index.php的顶部(或者放在脚本前面的
require
d或
include
d by index.php文件中)?在你可以阅读会话内容之前,这是必需的。谢谢@noah,但我想检查一下id是否通过了。是,会话启动可用。它确实打印用户名,但我的问题是登录id没有传递给用户\u idOk,所以让我们追溯它<代码>foreach($users as$view):$\u会话['user\u id']=$view['user\u id']看起来不对
$users
应该已经是包含表中一行的数组。您不需要循环它(这将循环对象的各个属性,这是不需要的)。请尝试只使用
$\u会话['user\u id']=$users[“user\u id”]取而代之。类似地,我认为以后对
$views
的其他引用也应该替换为
$users
。我很惊讶,
$\u SESSION['user\u id']=$view['user\u id']未生成有关未定义索引或类似索引的警告?您是否在PHP设置中抑制了警告或其他内容?或者您忘记检查日志文件了吗?我假设您想显示
john.doe
,而不是
0
(根据您的代码,它只是数据库中的行ID)?。如果是这样,为什么不使用
$\u SESSION['username']
变量呢?是否将
SESSION\u start()
放在index.php的顶部(或者放在脚本前面的
require
d或
include
d by index.php文件中)?在你可以阅读会话内容之前,这是必需的。谢谢@noah,但我想检查一下id是否通过了。是,会话启动可用。它确实打印用户名,但我的问题是登录id没有传递给用户\u idOk,所以让我们追溯它<代码>foreach($users as$view):$\u会话['user\u id']=$view['user\u id']看起来不对
$users
应该已经是包含表中一行的数组。您不需要循环它(这将循环对象的各个属性,这是不需要的)。请尝试只使用
$\u会话['user\u id']=$users[“user\u id”]取而代之。类似地,我认为以后对
$views
的其他引用也应该替换为
$users
。我很惊讶,
$\u SESSION['user\u id']=$view['user\u id']未生成有关未定义索引或类似索引的警告?您是否在PHP设置中抑制了警告或其他内容?或者您忘记检查日志文件了吗?$view在本例中未定义$view在本例中未定义