PHP Sessions issues while on server but no on loca

2019-07-10 19:37发布

问题:

I'm passing the user variable through sessions. It works fine on the localhost but once on the web server it does weird things.

After logging in, the session variable works as should.....until you click on about three pages and it suddenly goes POOF!

Notice the "Welcome, jordan." as opposed to the "Welcome, ." Also the top left corner.

Session functioning: http://imageshack.us/photo/my-images/32/loggedins.png/

Session POOF! http://imageshack.us/photo/my-images/515/loggedinno.png/

Login/Create session variable code:

<?php
        if (!isset($_SESSION['user']))
        {
        if (isset($_POST['user']))
        {
        $user = sanitizeString($_POST['user']);
        $pass = sanitizeString($_POST['pass']);
        if (preg_match($txtMatch,$user))
        {
        if ($user == "" || $pass == "")
        {
        $error = "Please enter all required fields";
        }
        else
        {
        $query = "SELECT * FROM gtmembers WHERE user='$user'";
        $result = queryMysql($query);
        $rank = mysql_result($result, 0, 'rank');
        if (!mysql_num_rows($result))
        {
        $error = "Username does not exist.";
        }
        else
        {
        $getPass = mysql_result($result, 0, 'pass');
        $salt = substr($getPass, 0, 64);
        $hash = $salt . $pass;
        for ($i = 0; $i < 100000; $i++) 
        {
        $hash = hash('sha256', $hash);
        }
        $hash = $salt . $hash;
        if ($hash == $getPass)
        {
        if ($rank != "Banned")
        {
        $userLow = strtolower($user);
        $_SESSION['user'] = $userLow;
        $_SESSION['rank'] = $rank;
        echo <<<_END
        <script type="text/javascript">
        window.location.href='index.php';
        </script>
        _END;
        echo "Successfully logged in. Click <a href='index.php'>here</a> to continue.";
        }

Header Code:

        <?php //gtheader.php
        session_start();
        include_once 'gtfunctions.php';
        $loggedIn = FALSE;

        if (isset($_SESSION['user']))
        {
        $user = $_SESSION['user'];
        if ($user) echo "Current User: $user<br />";
        else echo "Current User: None<br />";
        $rank = $_SESSION['rank'];
        $loggedIn = TRUE;
        echo "is set SESSION['user']? Yes";
        }
        else echo "is set SESSION['user']? No";

        echo "<div id='header'><a class='header' href='index.php'> <h1 id='headerTitle'>$appname</h1></a>";
        if ($loggedIn == TRUE)
        {
        $query = "SELECT * FROM gtmessages WHERE recip='$user' AND status='0'";
        $result = queryMysql($query);
        if (mysql_num_rows($result) == 0) $num = "";
        else $num = "[".mysql_num_rows($result)."]";
        if ($rank == 'Owner' || $rank == 'Admin')
        {
        echo "Welcome, <a class='header' href='gtmembers.php?view=$user'>$user</a><a     class='header' href='gtmessage.php'>$num</a>. [<a class='header'     href='gtlogout.php'>Logout</a>] | <a class='header' href='gtadmin.php'>Admin</a><br />";
        }
        else
        {
        echo "Welcome, <a class='header' href='gtmembers.php?view=$user'>$user</a><a     class='header' href='gtmessage.php'>$num</a>. [<a class='header'     href='gtlogout.php'>Logout</a>]<br />";
        }
        }
?>

回答1:

If it works in one environment and not the other, I'm guessing your PHP.ini has session.auto_start = 1 in the environment that works. Best practice is to always call session_start() at the top of your page and not rely on php.ini to be set correctly. This should make it work in any environment.



回答2:

Solved.

The issue here is that Host Gator registers global variables. So since I use $user, and $_SESSION['user'] gets registered, it gets overwritten.

I fixed the issue by changing $_SESSION['user'] in all files to $_SESSION['myUser']

Thanks for the help.



回答3:

Must call session_start() at the top of the page

gtheader.php

<?php
    session_start();
    include_once 'gtfunctions.php';
    $loggedIn = FALSE;
    .....