PHP session doesn't seem to work

Session variables do not seem to work for me. I do not know what I am doing wrong. This is the code I use to verify a valid password:

if ($input_password_hash == $password_hash)
 {
  session_start();
  $_SESSION['is_user'] = 1;
  header("Location: ../new_look"); //or Location: index.php
 }
else echo "Wrong password.";

in the index.php file (where it is redirected), I have this code:

if ($_SESSION['is_user'] == 1)
{
  //show index page with navigation bar for registered user
}

else
{
 //do something else
}

but it doesn’t work at all.

The session obviously begins, and this can be verified by checking the cookie.

What am I doing wrong?

+3
source share
3 answers

You know what you need to write session_start()before using a variable $_SESSIONin any query, right? It looks like you did not put it in index.php anywhere.

+5
source

session_start(); , , . HTML php.

+2

I just returned with the fix that ... added session_start()to the top of the file - even before the tag <html>. Running session_start()in the middle gives errors ... I don't know why. I am new to this. Thanks for your quick reply though.

+1
source

Source: https://habr.com/ru/post/1702401/


All Articles