Session doesn't work

Hello

The problem is, Session lost when I refresh the page or when I click on any link on the sidebar

So when I log in, everything working fine. I get the details from username the session is working. But when I refresh or click on one of the links on the sidebar redirect me to the index page. If I switch off the SESSION then the link is working but no session data. on that page.

So the SESSION is not going to the next page on the admin area.

Can you help me with it?

So from the Login page to admin_dashboard.php working fine but from admin_dashboard.php to admin_user_setting.php, SESSION is lost.

Hopefully, you can help me.

Thank in advance

// login.php

<?php


if(isset($_POST['btn_login'])){


$user_email = $_POST['txt_email'];

$user_pass = $_POST['txt_password'];


$user_email = mysqli_real_escape_string($connection, $user_email);

$user_pass = mysqli_real_escape_string($connection, $user_pass);


$query = "SELECT * FROM users WHERE user_email = '{$user_email}' ";

$select_user_query = mysqli_query($connection, $query);

if(!$select_user_query) {


    die("QUERY FAILED". mysqli_error($connection));

}

while($row = mysqli_fetch_array($select_user_query)){


$db_user_id = $row['user_id'];

$db_user_name = $row['user_name'];

$db_user_pass = $row['user_pass'];

$db_user_email = $row['user_email'];

$db_role = $row['role'];


}


if ($user_email == $db_user_email && $user_pass == $db_user_pass && $db_role == "Admin" ) {


  $_SESSION['user_id'] = $db_user_id;

  $_SESSION['user_name'] = $db_user_name;

  $_SESSION['user_email'] = $db_user_email;

  $_SESSION['role'] = $db_role;

header("Location: includes/admin_dashboard.php");


} else if($user_email == $db_user_email && $user_pass == $db_user_pass && $db_role == "User" ) {


  $_SESSION['user_id'] = $db_user_id;

  $_SESSION['user_name'] = $db_user_name;

  $_SESSION['user_email'] = $db_user_email;

  $_SESSION['role'] = $db_role;

header("Location: includes/user_dashboard.php");


} else{

  header("Location: index.php");


}

}


// admin_header.php

<!DOCTYPE html>

<html lang="en">

<head>

  <meta name="viewport" content="width=device-width, initial-scale=1">

  <meta http-equiv="x-ua-compatible" content="ie=edge">

  <title>Bright</title>

  <!-- Font Awesome Icons -->

  <link rel="stylesheet" href="plugins/fontawesome-free/css/all.min.css">

  <!-- Theme style -->

  <link rel="stylesheet" href="dist/css/adminlte.min.css">

  <!-- Google Font: Source Sans Pro -->

  <link href="https://fonts.googleapis.com/css?family=Source+Sans+Pro:300,400,400i,700" rel="stylesheet">

</head>

<body class="hold-transition sidebar-mini"> <div class="wrapper">

  <!-- Navbar -->

  <nav class="main-header navbar navbar-expand navbar-white navbar-light">

    <!-- Left navbar links -->

    <ul class="navbar-nav">

      <li class="nav-item">

        <a class="nav-link" data-widget="pushmenu" href="#" role="button">

          <i class="fas fa-bars">


          </i>

        </a>

      </li>

      <li class="nav-item d-none d-sm-inline-block">

        <a href="index3.html" class="nav-link">Home</a>

      </li> <li class="nav-item d-none d-sm-inline-block">

        <a href="#" class="nav-link">Contact</a>

      </li>

    </ul>

    <!-- SEARCH FORM -->

    <form class="form-inline ml-3">

      <div class="input-group input-group-sm">

        <input class="form-control form-control-navbar" type="search" placeholder="Search" aria-label="Search">

        <div class="input-group-append">

          <button class="btn btn-navbar" type="submit"> <i class="fas fa-search"></i>

          </button>

        </div>

      </div>

    </form>

    <!-- Right navbar links -->

    <ul class="navbar-nav ml-auto">

      <!-- Messages Dropdown Menu -->

      <li class="nav-item dropdown">

        <a class="nav-link" data-toggle="dropdown" href="v">

          <i class="far fa-comments">

          </i>

          <span class="badge badge-danger navbar-badge">3</span>

        </a>

        <div class="dropdown-menu dropdown-menu-lg dropdown-menu-right">

          <a href="#" class="dropdown-item">

            <!-- Message Start -->

            <div class="media">

              <img src="dist/img/user1-128x128.jpg" alt="User Avatar" class="img-size-50 mr-3 img-circle">

              <div class="media-body">

                <h3 class="dropdown-item-title"> Brad Diesel

                  <span class="float-right text-sm text-danger">

                  <i class="fas fa-star"></i>

                </span>

              </h3>

              <p class="text-sm">Call me whenever you can...</p>

                <p class="text-sm text-muted">

                  <i class="far fa-clock mr-1">

                  </i> 4 Hours Ago</p>

                </div>

              </div>

              <!-- Message End -->

            </a> <div class="dropdown-divider">

            </div>

            <a href="#" class="dropdown-item">

              <!-- Message Start -->

              <div class="media">

                <img src="dist/img/user8-128x128.jpg" alt="User Avatar" class="img-size-50 img-circle mr-3">

                <div class="media-body">

                  <h3 class="dropdown-item-title"> John Pierce

                    <span class="float-right text-sm text-muted">

                      <i class="fas fa-star">


                      </i>

                    </span>

                  </h3>

                  <p class="text-sm">I got your message bro</p>

                  <p class="text-sm text-muted">

                    <i class="far fa-clock mr-1">


                    </i> 4 Hours Ago</p>

                  </div>

                </div>

                <!-- Message End -->

              </a>

              <div class="dropdown-divider">


              </div>

              <a href="#" class="dropdown-item">

                <!-- Message Start -->

                <div class="media">

                  <img src="dist/img/user3-128x128.jpg" alt="User Avatar" class="img-size-50 img-circle mr-3">

                  <div class="media-body">

                    <h3 class="dropdown-item-title"> Nora Silvester

                      <span class="float-right text-sm text-warning">

                        <i class="fas fa-star"></i>

                      </span>

                    </h3>

                    <p class="text-sm">The subject goes here</p>

                    <p class="text-sm text-muted">

                      <i class="far fa-clock mr-1">


                      </i> 4 Hours Ago</p>

                    </div>

                  </div>

                  <!-- Message End -->

                </a>

                <div class="dropdown-divider">


                </div>

                <a href="#" class="dropdown-item dropdown-footer">See All Messages</a>

              </div>

            </li>

            <!-- Notifications Dropdown Menu -->

            <li class="nav-item dropdown">

              <a class="nav-link" data-toggle="dropdown" href="#">

                <i class="far fa-bell">


                </i>

                <span class="badge badge-warning navbar-badge">15</span>

              </a>

              <div class="dropdown-menu dropdown-menu-lg dropdown-menu-right">

                <span class="dropdown-header">15 Notifications</span>

                <div class="dropdown-divider">


                </div> <a href="#" class="dropdown-item">

                  <i class="fas fa-envelope mr-2">


                  </i> 4 new messages <span class="float-right text-muted text-sm">3 mins</span>

                </a>

                <div class="dropdown-divider">


                </div>

                <a href="#" class="dropdown-item">

                  <i class="fas fa-users mr-2">


                  </i> 8 friend requests

                  <span class="float-right text-muted text-sm">12 hours</span>

                </a>

                <div class="dropdown-divider">


                </div>

                <a href="#" class="dropdown-item">

                  <i class="fas fa-file mr-2">


                  </i> 3 new reports <span class="float-right text-muted text-sm">2 days</span>

                </a>

                <div class="dropdown-divider">


                </div> <a href="#" class="dropdown-item dropdown-footer">See All Notifications</a>

              </div>

            </li>

            <li class="dropdown user user-menu">

              <!-- Menu Toggle Button -->

              <a class="nav-link" data-toggle="dropdown" href="#">

                <i class="fas fa-user">


                </i>

                <span class="badge badge-warning navbar-badge"></span>

              </a>

              <ul class="dropdown-menu">

                <!-- The user image in the menu -->

                <li class="user-header">

                  <img src="dist/img/Male-Avatar-icon.png" class="img-circle" alt="User Image">

                  <p> Welcome - <?php echo $_SESSION['user_name'] ?> <small><?php echo $_SESSION['role'] ?></small>

                  </p>

                </li>

                <li class="user-footer">

                  <div class="float-left">

                    <a href="#" class="btn btn-default btn-flat">Profile</a>

                  </div>

                  <div class="float-right">

                    <a href="../includes/logout.php" class="btn btn-default btn-flat">Sign out</a>

                  </div>

                </li>

              </nav>

              <!-- /.navbar --> <!-- Main Sidebar Container -->

              <aside class="main-sidebar sidebar-dark-primary elevation-4">






                <!-- Brand Logo -->

                <a href="index3.html" class="brand-link">

                  <img src="dist/img/AdminLTELogo.png" alt="" class="brand-image img-circle elevation-3 far fa-sun" style="opacity: .8">

                  <span class="brand-text font-weight-light ">

                    <strong><b>Bright</b>Starter</strong>

                  </span> </a>

                  <!-- Sidebar -->

                  <div class="sidebar">

                    <!-- Sidebar user panel (optional) -->

                    <!-- <div class="user-panel mt-3 pb-3 mb-3 d-flex"> <div class=" image">

                    <img src="dist/img/Male-Avatar-icon.png" class="img-circle elevation-2" alt="User Image">

                  </div> <div class="info"> <a href="#" class="d-block">Alexander Pierce</a> </div> </div> -->

                  <!-- Sidebar Menu -->

                  <nav class="mt-2">

                    <ul class="nav nav-pills nav-sidebar flex-column" data-widget="treeview" role="menu" data-accordion="false">

                      <!-- Add icons to the links using the .nav-icon class with font-awesome or any other icon font library -->

                      <li class="nav-item">

                        <a href="#" class="nav-link">

                          <i class="nav-icon fas fa-tachometer-alt">


                          </i>

                          <p> Dashboard <span class="right badge badge-danger">New</span>

                          </p>

                        </a>

                      </li>

                      <li class="nav-item has-treeview">

                        <a href="#" class="nav-link ">

                          <i class="nav-icon fas fa-users">


                          </i>

                          <p> User Settings <i class="right fas fa-angle-left">


                          </i>

                        </p>

                      </a>

                      <ul class="nav nav-treeview">

                        <li class="nav-item">

                          <a href="#" class="nav-link">

                            <i class="far fa-circle nav-icon">


                            </i>

                            <p>Add User</p>

                          </a>

                        </li>

                        <li class="nav-item">

                          <a href="#" class="nav-link">

                            <i class="far fa-circle nav-icon">


                            </i>

                            <p>Edit User</p>

                          </a>

                        </li>

                      </li>

                      <li class="nav-item">

                        <a href="#" class="nav-link">

                          <i class="far fa-circle nav-icon">


                          </i>

                          <p>Settings</p>

                        </a>

                      </li>

                    </ul>

                  </li>

                  <li class="nav-item">

                    <a href="#" class="nav-link">

                      <i class="nav-icon fas fa-calendar-alt"></i>

                      <p> Holiday Planner <span class="right badge badge-danger"></span>

                      </p>

                    </a>

                  </li>

                  <li class="nav-item has-treeview">

                    <a href="#" class="nav-link ">

                      <i class="nav-icon fas fa-cogs"></i>

                      <p>Settings <i class="right fas fa-angle-left"></i></p>

                    </a>

                  <ul class="nav nav-treeview">

                    <li class="nav-item">

                      <a href="admin_user_settings.php" class="nav-link">

                        <i class="far fa-circle nav-icon">


                        </i>

                        <p>Users</p>

                      </a>

                    </li>

                    <li class="nav-item">

                      <a href="#" class="nav-link">

                        <i class="far fa-circle nav-icon">


                        </i>

                        <p>002</p>

                      </a>

                    </li>

                  </li>

                  <li class="nav-item">

                    <a href="#" class="nav-link">

                      <i class="far fa-circle nav-icon">


                      </i>

                      <p>Settings</p>

                    </a>

                  </li>

                </ul>

              </li>



              </nav>

              <!-- /.sidebar-menu -->

            </div>

            <!-- /.sidebar -->

          </aside>


// admin_dashboard.php

<?php include "db.php";?>

<?php     if($_SESSION['user_email']==""){

       header('location:../index.php');

    } ?>

<?php include "admin_header.php";?>

  <div class="content-wrapper">

    <!-- Content Header (Page header) -->

    <div class="content-header">

      <div class="container-fluid">

        <div class="row mb-2">

          <div class="col-sm-6">

            <h1 class="m-0 text-dark">Welcome <?php echo $_SESSION['user_name'] ?></h1>


          </div><!-- /.col -->

          <div class="col-sm-6">

            <ol class="breadcrumb float-sm-right">

              <li class="breadcrumb-item"><a href="#">Home</a></li>

              <li class="breadcrumb-item active">Admin Dashboard</li>

            </ol>

          </div><!-- /.col -->

        </div><!-- /.row -->

      </div><!-- /.container-fluid -->

    </div>

    <!-- /.content-header -->

      </div>

  <!-- Control Sidebar -->


<?php include "footer.php"; ?>

  </div>



<!-- ./wrapper -->


<!-- REQUIRED SCRIPTS -->

<script src="plugins/jquery/jquery.min.js"></script>

<!-- Bootstrap 4 -->

<script src="plugins/bootstrap/js/bootstrap.bundle.min.js"></script>

<!-- AdminLTE App -->

<script src="dist/js/adminlte.min.js"></script>


//admin_user_setting.php

<?php include "db.php";?>

<?php     if($_SESSION['user_email']==""){

       header('location:../index.php');

    } ?>

<?php include "admin_header.php";?>

  <div class="content-wrapper">

    <!-- Content Header (Page header) -->

    <div class="content-header">

      <div class="container-fluid">

        <div class="row mb-2">

          <div class="col-sm-6">

            <h1 class="m-0 text-dark">User Settings</h1>


          </div><!-- /.col -->

          <div class="col-sm-6">

            <ol class="breadcrumb float-sm-right">

              <li class="breadcrumb-item"><a href="#">Home</a></li>

              <li class="breadcrumb-item active">User Settings</li>

            </ol>

          </div><!-- /.col -->

        </div><!-- /.row -->

      </div><!-- /.container-fluid -->

    </div>

    <!-- /.content-header -->

      </div>

  <!-- Control Sidebar -->


<?php include "footer.php"; ?>

  </div>



<!-- ./wrapper -->


<!-- REQUIRED SCRIPTS -->

<script src="plugins/jquery/jquery.min.js"></script>

<!-- Bootstrap 4 -->

<script src="plugins/bootstrap/js/bootstrap.bundle.min.js"></script>

<!-- AdminLTE App -->

<script src="dist/js/adminlte.min.js"></script>

Do you have a session_start() statement on each page that sets or references a $_SESSION variable?

Also, every header() redirect needs an exit/die statement after it to STOP program execution.

The ONLY thing you should store in a session variable when the user logs in is the user’s id. You should query on each page request to get any other user information, so that any changes mode to the user information will take affect immediately (on the next page request after the change), without needing the user to re-login.

Hi
Thanks your answer
I use session_start() on each page but can you help me with the rest?

Yes, but is it running without error on each page? Do you have php’s error_reporting set to E_ALL and display_errors set to ON, preferably in the php.ini on your system, so that php would help you by reporting and displaying all the errors that it detects?

This is the code on each admin page:

<?php session_start();

if($_SESSION['user_email']=="" OR $_SESSION['role']=="User"){

    header('location:../index.php');
}?>

If the header locations (header(‘location:…/index.php’):wink: active, automatically send me to the index.php

If I delete, redirect me to the correct page but I get this message : Notice** : Undefined index: user_email in C:\xampp\htdocs\testCopy\includes\admin_user_settings.php on line **5

The SESSION doesn’t send the details from the admin_dashboard to another page!!

Hi
can you help with it?

You need to check if the key isset

if ( isset( $_SESSION['user_email']))

or

if ( empty( $_SESSION['user_email']))

From login page to admin page , workng fine but from admin to another page the session is lost.
redirect me to index.php
This is the code on the 3rd page:

<?php session_start(); if($_SESSION['user_email']=="" OR $_SESSION['role']=="User"){ header('location:../index.php'); } ?>

And session_start is on every page?

Yes

<?php include "db.php";?> <?php session_start(); if($_SESSION['user_email']=="" OR $_SESSION['role']=="User"){ header('location:../index.php'); } ?>
Sponsor our Newsletter | Privacy Policy | Terms of Service