简体   繁体   中英

How do I print user information once logged in?

I've added a login system to my website but it seems the information isn't properly being stored in the session. When I try to open up the destination page, I am trying to echo the user_id, but I get the error: "Trying to access array offset on value of type null". From what I can see, this shouldn't be null since it is set in my login.php script. For further info, I've included the signup, login and function scripts. Any help would be massively appreciated.

login.php:

    <?php

session_start();

require ("connection.php");
require ("functions.php");

if ($_SERVER['REQUEST_METHOD'] == "POST")
{

    if (isset($_POST['email']) || isset($_POST['password']))
    {
        $email = $_POST['email'];
        $password = $_POST['password'];

        if (!empty($email) && !empty($password))
        {

            $query = ("select * from users where email = '$email' and password = '$password' and organiser_yn = 'N' limit 1");
            $result = mysqli_query($con, $query);

            $query2 = ("select * from users where email = '$email' and password = '$password' and organiser_yn = 'Y' limit 1");
            $result2 = mysqli_query($con, $query2);

            if ($result)
            {
                if ($result && mysqli_num_rows($result) > 0)
                {
                    $user_data = mysqli_fetch_assoc($result);

                        $_SESSION['user_id'] = $user_data['user_id'];
                        header("Location: EventPlannerSignedIn.php");
                        die;
                    
                    
                } elseif ($result2 && mysqli_fetch_assoc($result2) > 0)
                {
                    $user_data2 = mysqli_fetch_assoc($result2);
                    $_SESSION['user_id'] = $user_data2['user_id'];
                    header("Location: EventPlannerOrganiser.php");
                    die;
                }
                
                
                else
                {
                    echo "Email or password is incorrect.";
                }
            }
        }
    }
}

?>

signup.php:

  <?php 
session_start();

    include("connection.php");
    include("functions.php");


    if($_SERVER['REQUEST_METHOD'] == "POST")
    {
        if(isset($_POST['name']) || isset($_POST['email']) || isset($_POST['password']) ) 
        {
        $name = $_POST['name'];
        $email = $_POST['email'];
        $password = $_POST['password'];

        if(!empty($email) && !empty($password))
        {

            $user_id = random_num(20);
            $query = "insert into users (user_id,name,email,password) values ('$user_id','$name','$email','$password')";

            mysqli_query($con, $query);

            header("Location: login.php");
            die;
        }
        else
        {
            echo "The information you have entered is invalid.";
        }
    }
}
?>

destination page

<?php

session_start();

include("connection.php");
include("functions.php");

$user_data2 = check_login($con);
?>

<!DOCTYPE html>
<html lang="en">
   <head>
      
   </head>
   <body>
      <?php echo $user_data2['user_id'];?>
   </body>
   </html>

function:

<?php

function check_login($con)
{

    if(isset($_SESSION['user_id']))
    {

        $id = $_SESSION['user_id'];
        $query = "select * from users where user_id = '$id' limit 1";

        $result = mysqli_query($con,$query);
        if($result && mysqli_num_rows($result) > 0)
        {

            $user_data = mysqli_fetch_assoc($result);
            return $user_data;
        }
    }
    die;

}
?>

try

<body>
<?php echo $_SESSION['user_id']; ?>
</body>

because you are not storing anything in the $userdata2 and not even initializing any data on that variable, nor from sessions, nor from your actual database.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM