0

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!

  • 1
    use **prepared stemenst** read this https://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php because of **sql injection**.second add and error management to your code to see if there is some error – nbk Feb 16 '20 at 13:13
  • 1
    Missing session_start(); – vadivel a Feb 16 '20 at 13:28
  • 1
    Where exactly have you started the session? It isn't in any of the code that you included in your question. It should be the very first thing on your entry page. – Dave Feb 16 '20 at 13:51

0 Answers0