Home » Php » php – Session variables doesn't shows up on different page

php – Session variables doesn't shows up on different page

Posted by: admin February 25, 2020 Leave a comment

Questions:

I’ve started to learn about $_SESSIONs and I’ve headed with this problem. The session of the username is work it is callable but the others can’t. I’ve tried to solved this problem about 2 hours by stackoverflow but I didn’t found the perfect solution. Here are the codes:

login.php

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

$username = stripslashes($_REQUEST['username']);

$username = mysqli_real_escape_string($con,$username);

$kname = stripslashes($_REQUEST['kname']);
$kname = mysqli_real_escape_string($con,$kname);

$trn_date = stripslashes($_REQUEST['trn_date']);
$trn_date = mysqli_real_escape_string($con,$trn_date);
$password = stripslashes($_REQUEST['password']);
$password = mysqli_real_escape_string($con,$password);

$query = "SELECT * FROM `accounts` WHERE username='$username'
and password='".md5($password)."'";
$result = mysqli_query($con,$query) or die(mysql_error());
$rows = mysqli_num_rows($result);

    if($rows==1){

  $_SESSION['trn_date'] = $trn_date;  
  $_SESSION['kname'] = $kname;
  $_SESSION['username'] = $username;
  $_SESSION['loggedin'] = true;

        // Redirect user to index.php
 header("Location: index.php");
     }  

profile.php

<?php 


    var_dump($_SESSION);
    ini_set('display_errors',1);
    error_reporting(E_ALL);
    echo "session started" ."<br />";

     if (isset($_SESSION['id'])){
    echo "user is not logged in";}
     echo $_SESSION["username"];
     echo $_SESSION["kname"];  
     echo $_SESSION["trn_date"];  ?>

Output:

 C:\wamp64\www\profile.php:20:
 array (size=4)
 'trn_date' => string '' (length=0)
 'kname' => string '' (length=0)
 'username' => string 'admin' (length=5)
 'loggedin' => boolean true
  session started
  admin 
  1. Yes I’ve started the session
  2. No the MySQL database aren’t empty

Thank you for the answers!

How to&Answers: