-1

So I created a login.php for our Web application project but it doesn't seem to let me login in a session

Here's my login.php code:

<?php
if(isset($_POST['username']) && isset($_POST['pass'])){
include("sql_connect.php");
$res = mysqli_query($mysqli, "SELECT * FROM customer
                              WHERE idnum='".$_POST['username']."'
                              AND password = '".$_POST['pass']."'");

if(mysqli_num_rows($res)==1){
    $_SESSION['logged_in'] = "$idnum";   //creates and initializes the session with the name 'variable_name'
    echo $_SESSION['logged_in'];   //will now print out value
    header("location:index.php");
    }
    else{
        echo "Incorrect Username/Password!";
    }
}
?>

and here's my index.php code:

<?php
session_start();
if(!isset($_SESSION['logged_in']) || $_SESSION['logged_in'] != "TRUE"){
    header("location:login.php");
}
include("sql_connect.php");
?>
Judd
  • 1

1 Answers1

0

You need to add session_start() on all files using sessions (the ones reading, updating, setting, etc. them).

brclz
  • 806
  • 9
  • 23
  • It's still not working for me, I don't know why – Judd Oct 10 '16 at 15:32
  • There are other mistakes in your code and logic like @nospor pointed in a comment above. `$_SESSION['logged_in']` seems to contain a username and not the string "TRUE" and the condition you use is always `true` (boolean). Work a bit on your code logic... – brclz Oct 10 '16 at 15:35
  • Not my downvote here, but OP is also outputting before header and see the comments under the question. You've put yourself in that rabbit hole here. – Funk Forty Niner Oct 10 '16 at 15:50