Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Shivamyadav's avatar

session not set? why

setting the session when a user is logged in

<?php 
    require __DIR__ . "/../dbConnection.php";
    if($_SERVER['REQUEST_METHOD'] == 'POST')
    {
        $email = mysqli_real_escape_string($conn, $_POST['email']);
        $password = mysqli_real_escape_string($conn, $_POST['password']);   
             
        require __DIR__ . '/../validation/loginValidation.php';
        if(empty($errors))
        {
            $_SESSION['email'] = $emailRow['email'];
            $_SESSION['id'] = $emailRow['id'];
            $_SESSION['password'] = $emailRow['password'];
            $_SESSION['msg'] = "Welcome back!";
            header('location:/dashboard.php');
        } 
        
    }

and displaying it on the the footer page and footer is common to all pages here the code to display it

$s = isset($_SESSION['msg']) ? $_SESSION['msg'] : "";
var_dump($s); 
$user_id = isset($_SESSION['id']) ? $_SESSION['id'] : "";
echo $user_id;
die();

error is this string(0) "" 57 where 57 is the session['id'] and string is empty for the msg how can be this happened? anyone help me out :(

0 likes
14 replies
vincent15000's avatar

Are you sure that don't have the msg session value changed at another place in your code ?

Something else : it's not a good idea to store the password in the session ;).

vincent15000's avatar

@Shivamyadav Can you test this please ?

if(empty($errors))
{
	dd('test');
    $_SESSION['email'] = $emailRow['email'];
    $_SESSION['id'] = $emailRow['id'];
    $_SESSION['password'] = $emailRow['password'];
    $_SESSION['msg'] = "Welcome back!";
    header('location:/dashboard.php');
} 

Do you see the test on the screen ?

vincent15000's avatar

@Shivamyadav Ok I have seen, so you really unset the msg session value at another place.

If this problem is solved, please don't forget to close the post.

vincent15000's avatar

@Shivamyadav What error ? Do you get an error ? Oh you mean to close the post ? By assigning the best answer to the answer that best helped you to solve the problem.

Shivamyadav's avatar

@vincent15000 when i show the msg of the session it display when i comment this code <?php unset($_SESSION['message']) ?> and visible to all pages where i go, need to be unset when i leave the page ?that's the main error..

1 like
jlrdw's avatar

@Shivamyadav in plain php you need:

session_start();

on any page using session, that why you should use MVC and a config file:

I.E.

<?php

session_start();

$_SESSION['msg'] = "Welcome back!";
header('location:/codegen/dashboard.php');

dashboard:

<?php
session_start();

$s = isset($_SESSION['msg']) ? $_SESSION['msg'] : "";
echo $s; 
die();

Output:

Welcome back!

I suggest taking the w3schools php tutorial.

1 like
Shivamyadav's avatar

@jlrdw session is displaying in the page but when i change the link the session msg is not unset for the other page

1 like
jlrdw's avatar

@Shivamyadav I have no idea then what you are trying to do, if you don't need it in other page then don't display it in other page.

1 like
vincent15000's avatar

@Shivamyadav First you said that the session msg was not set and then you say that you don't want it. It's very confused in my mind.

1 like

Please or to participate in this conversation.