1

$_SESSION['isloggedin'] doesn't seem to be working on first load.
This only happens on server, not on localhost.
session_start() is at the top of each page.

initialized to: $_SESSION['isloggedin'] = false;

When user logs in $_SESSION['isloggedin'] = true;
When user logs out $_SESSION['isloggedin'] = false;

on home.php:

if (!$_SESSION['isloggedin']) {
  die(header("Location: login.php"));
}

on login.php:

if ($_SESSION['isloggedin']) {
  die(header("Location: home.php"));
}

When you login and sent to the home page $_SESSION['isloggedin'] doesn't seem to be true so it redirects to login.php. But since it is true it redirects to Home.php causing a redirect loop.

when a redirect loop error pops up, I refresh and am taken to the right page. Sometimes the page self refreshes and takes me to the correct page, still showing redirect error before.

Why isn't $_SESSION variable working properly on server? The correct value doesn't seem to register the first time on every page, every site link.

EDIT:

everything works as expected on localhost just not on the online server.
when login is clicked and everything passes the class login function is called:

class users {

    $_SESSION['isLoggedIn'] = false;

    function __construct() {
        if (session_id() == "") {
            session_start();
        }

        if (isset($_SESSION['isLoggedIn']) && $_SESSION['isLoggedIn'] == true) {
            if (session_id() == '') {
                session_start();
            }
        }
    }

    function login($user,$password) {
        if (session_id() == "") {
            session_start();
        }
        $_SESSION['isLoggedIn'] = false;

        $mysqli = new mysqli(DB_HOST, DB_USERNAME, DB_PASSWORD, DB_DATABASE);
        if ($mysqli->connect_errno) {
            return false;
        }

        $user = $mysqli->real_escape_string($user);
        $password = $mysqli->real_escape_string($password);
        $query = "SELECT * from users WHERE email=$user";

        if (!$result = $mysqli->query($query)) {
            return false;
        }

        $row = $result->fetch_assoc();
        $db_pass = $row['password'];

        if (crypt($password,$db_pass) != $db_pass) {
            return false;
        }

        $_SESSION['isLoggedIn'] = true;

        if (session_id() == '') {
            session_start();
        }
        return true;
    } 

}
6
  • if (!isset($_SESSION['isloggedin'])) and if (isset($_SESSION['isloggedin'])) try with this Commented Sep 13, 2016 at 1:28
  • header inside die() actually works ? Commented Sep 13, 2016 at 1:28
  • I tried with isset. Same issue. since $_SESSION['isloggedin'] is initialized i get redirect loop. Uninitialized I get redirected to home.php even though i am not logged in. Commented Sep 13, 2016 at 1:32
  • Show the code where you set that variable for first time Commented Sep 13, 2016 at 1:33
  • I added above some of the how the code looks Commented Sep 13, 2016 at 1:47

2 Answers 2

1

Try changing your code to something like this

if (!isset($_SESSION['isloggedin'])) {

  header("Location: login.php");

} else {

  header("Location: home.php");
}
Sign up to request clarification or add additional context in comments.

2 Comments

this directs me straight to home.php without logging in showing no user content.
Show your code where you are doing the loggin, because I don't know where you are returning true show your queries and the whole process, we won't steal your code
0

I was using AWS Elastic Beanstalk to run this web app. I didn't think this matter but apparently it did. It turns out that sessions don't work the same as they do on localhost since you are dividing your servers. I needed to enable sticky session within the load balancer.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.