How do I print user information once logged in?

63
July 28, 2021, at 3:40 PM

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;
}
?>
Answer 1

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.

READ ALSO
(discord.js + document) Replace new lines with &lt;br&gt;

(discord.js + document) Replace new lines with <br>

I am creating a ticket bot with discordjs

51
Using Cropper.js with only javascript, my below script codes are conflicting with each other

Using Cropper.js with only javascript, my below script codes are conflicting with each other

I'am trying to make a custom mockup generatorWith below this script code i can mockup in the browser without using ajax or something else

68
React + Electron + Typescript - Pass defaultValue prop to JSX Input Element using data fetched asynchronously (Promise)

React + Electron + Typescript - Pass defaultValue prop to JSX Input Element using data fetched asynchronously (Promise)

I have what is meant to be a settings menu component, and I am attempting to read from a JSON file located in the app data directoryFor this persistent data storage, I'm using electron-store@^8

68
Can i deduct that either thumb pressed or finger pressed in android screen programmatically?

Can i deduct that either thumb pressed or finger pressed in android screen programmatically?

i started working as android developerI want to gesture that when someone press on screen, its thumb or finger

89