PHP $_SESSION variable not showing value


Nikola Knežević:

I have written a text in the page title with the name of the logged in user. It worked for a while, but when I went to do something else, it just stopped. It 's like $_SESSION['ime'] holds a null value; I'm sure I'm not hitting anything related to this.

Here is some code: my main php file:

session_start();

// initializing variables
$username = "";
$email    = "";
$errors = array(); 

// connect to the database
$db = mysqli_connect('localhost', 'root', '', 'audiprojekat');

// REGISTER USER
if (isset($_POST['reg_user'])) {
  $vrsta = "KO";
  $ime = mysqli_real_escape_string($db, $_POST['ime']);
  $prezime = mysqli_real_escape_string($db, $_POST['prezime']);
  $username = mysqli_real_escape_string($db, $_POST['username']);
  $email = mysqli_real_escape_string($db, $_POST['email']);
  $password = mysqli_real_escape_string($db, $_POST['password']);



  // first check the database to make sure 
  // a user does not already exist with the same username and/or email
  $user_check_query = "SELECT * FROM registrovani WHERE username='$username' OR email='$email' LIMIT 1";
  $result = mysqli_query($db, $user_check_query);
  $user = mysqli_fetch_assoc($result);

  if ($user) { // if user exists
    if ($user['username'] === $username) {
      array_push($errors, "Username already exists");
    }

    if ($user['email'] === $email) {
      array_push($errors, "email already exists");
    }
  }

  // Finally, register user if there are no errors in the form
  if (count($errors) == 0) {
    $password = md5($password);//encrypt the password before saving in the database

    $query = "INSERT INTO registrovani (ime, prezime, username, email, password, vrsta) 
              VALUES('$ime','$prezime','$username', '$email', '$password', '$vrsta')";
    mysqli_query($db, $query);
    $_SESSION['username'] = $username;
    $_SESSION['ime'] = $ime;
    $_SESSION['success'] = "You are now logged in";
    header('location: user.php');
  }
}

if (isset($_POST['login_user'])) {
  $username = mysqli_real_escape_string($db, $_POST['username']);
  $password = mysqli_real_escape_string($db, $_POST['password']);

  if (empty($username)) {
    array_push($errors, "Username is required");
  }
  if (empty($password)) {
    array_push($errors, "Password is required");
  }

  if (count($errors) == 0) {
    $password = md5($password);
    $query = "SELECT * FROM registrovani WHERE username='$username' AND password='$password'";
    $results = mysqli_query($db, $query);
    if (mysqli_num_rows($results) == 1) {
      $_SESSION['username'] = $username;
      $_SESSION['ime'] = $ime;
      $_SESSION['email'] = $email;
      $_SESSION['success'] = "You are now logged in";
      header('location: user.php');
    }else {
        array_push($errors, "Wrong username/password combination");
    }
  }
}

?>

Here is the html page:

<?php include('server.php'); ?>
<p id="user_cont" style="color: white;  margin-left: 80%; margin-top: 0; margin-bottom: 0; width: 20%; background-color: black; padding: .5%; font-family: 'Rajdhani', sans-serif; font-size: 140%; border-left: thick red solid;">
                <?php
                    if(isset($_SESSION['username'])){
                        echo $_SESSION['ime'];
                        echo " <a href='logout.php' style='color: red; text-decoration: none;'>Odjavite se</a>";
                    }
                    else echo " <a href='login.php' style='color: white; text-decoration: none;'>Prijavite se</a>";

                ?>

            </p>

$_SESSION['username'] and $_SESSION['password'] are ok, but $_SESSION['ime'], $_SESSION['prezime'], $_SESSION['email'] are empty.

I checked the mySQL database and everything is written correctly.

Zhang Wensen:

Your program has 2 steps: 1. reg_user, 2. login_user

I think you are saying that some parameters are empty after logging into the page. However, you are not reading data from the Select query.

Example code to read and store SESSION data from a query:

$row = $results->fetch_assoc();
$_SESSION['ime'] = $row['ime'];
$_SESSION['email'] = $row['email'];
$_SESSION['prezime'] = $row['prezime'];

Reference : https://www.w3schools.com/php/func_mysqli_fetch_assoc.asp _

Finally, read more about SQL injection with PDO or MYSQLI.

Related


PHP $_SESSION variable not showing value

Nikola Knežević: I have written a text in the page title with the name of the logged in user. It worked for a while, but when I went to do something else, it just stopped. It 's like $_SESSION['ime'] holds a null value; I'm sure I'm not hitting anything relate

PHP $_SESSION[variable] not getting value on next page

Aashish tandon This is a login form from which we will redirect to the login_success.php page when the username/password matches the database values. Checklogin.php <?php include 'config.php'; // username and password sent from form $myusername=$_POST['email'

PHP $_SESSION[variable] not getting value on next page

Aashish tandon This is a login form from which we will redirect to the login_success.php page when the username/password matches the database values. Checklogin.php <?php include 'config.php'; // username and password sent from form $myusername=$_POST['email'

PHP SESSION variable using $_SESSION

Talfred I'm trying to pass a session variable using $_SESSION, so I can check if the currently logged in user has "admin" privileges, which is stored in the database table "login" as "login_privileges". If they have admin rights, they can access some pages, ot

PHP SESSION variable using $_SESSION

Talfred I'm trying to pass a session variable using $_SESSION, so I can check if the currently logged in user has "admin" privileges, which is stored in the database table "login" as "login_privileges". If they have admin rights, they can access some pages, ot

PHP/Ajax: How to show/hide DIV on $_SESSION variable value?

Will Markuller I've searched the web for a lot of topics that talk about session variables and how to get them from Javascript via Ajax. However, although I've been able to do this, it doesn't completely solve my problem. objective Online inventory management

PHP/Ajax: How to show/hide DIV on $_SESSION variable value?

Will Markuller I've searched the web for a lot of topics that talk about session variables and how to get them from Javascript via Ajax. However, although I've been able to do this, it doesn't completely solve my problem. objective Online inventory management

How to update keys of $_SESSION array with php variable value

Rabia Rana Khan I have a question and can't seem to find the answer anywhere. So I put it here. I have the following variables stored in the $_SESSION variable: $_SESSION["pr_first_name"] ="Thomas"; $_SESSION["pr_middle_name"] ="Richard"; $_SESSION["pr_last_n

PHP session not showing correctly in terminal

Zoric I'm currently building a small script for a cron job, I'm testing everything in the terminal (Linux), but I can't figure out why the session isn't working. session_start() $_SESSION['cron'] = 'test'; echo $_SESSION['cron']; Zoric Here is my solution pri

session variable is empty in php

Aravindan vaithialingam login.php <?php ob_start(); session_start(); include '../config.php'; if( (isset($_SESSION['can-id']) )) { header('location: ../home/profile.php'); } if(isset($_POST['can-login'])) { $email=$_

PHP SESSION undefined variable

Kouci : Image description entered here good evening, I'm working on a small order" project. I'm creating a website where people can use PHP, which is new to me, to post used items to sell them or with other object (a kind of ebay, but 100000x simpler) to excha

PHP not setting session variable

bbowesbo: I'm a rookie programmer, so I apologize in advance for any obvious mistakes. Over the past week, I've spent a lot of time creating a product database. I can also use the form to add products, view all products added, etc. I am using a session created

PHP SESSION undefined variable

Kouci : Image description entered here good evening, I'm working on a small order" project. I'm creating a website where people can use PHP, which is new to me, to post used items to sell them or with other object (a kind of ebay, but 100000x simpler) to excha

update session variable in php

Unknown potato Okey, so there is a problem when the user changes the profile picture. If I understand correctly in my stupid mind (what I think), it's due to my session not being updated... so here is my uploadfile where user can upload image file to database,

custom php session variable

Jasper Chloe I've been checking all the posts here and I'm not getting what I need. I have multiple session variables, Some for the login information created after login: $_SESSION['user']=$username; // Initializing Session user $_SESSION['dept']='Admin'; // I

session not storing variable PHP

IRF I've started sessiongoing in header.phpand including in all files, but when I declare variables like this $_SESSION['var_name']="Hello";and save them in other files echo $_SESSION['var_name'];, I'm php.iniin a folder that contains the following session.sav