The login scripts I have written work fine on my Unix box at home. (PHP 4.3.2), but when I upload it to the server (PHP 4.2.3), there are issues.

The login process appears to go fine. But the $_SESSION variables are not registering. That is, after I log in, it should say "Welcome James Smith" at the top of the page, because I have this

The session variables array after login:

$_SESSION['user'] = mysql_fetch_array($query);

echo "Welcome ";
echo $_SESSION['user']['firstname'] . " " . $_SESSION['user']['lastname'];

But it just prints "Welcome"

Also, any time I go to a page that requires some of these variables, the variables are not present and it kicks me back to login again.

Originally, I had session_register("_SESSION") at the top of the page, but this was causing an ISE 500. Now, I'm not getting the error... but the variables aren't there! Anyone have any ideas?

    trying using a different variable name than $_SESSION when you register the variable name.

    Im not sure on this, but $_session may be what we call in Visual Basic a reserved word. A reserved word being a variable or built in function that is 'reserved' for other use. Comparible to "explode" and "date" in PHP.

      Just for kicks, I changed $_SESSION to $mySESSION, but I can't log in at all... I log in, it takes me to the page I requested, and then back to the log in screen again, with the message that I am not logged in.

      So, _SESSION does seem to be working... but not.

      I think you're right that _SESSION is a reserved word, but it is reserved specifically for this!

      So, I still cannot get it to work.

        Ok, I have a couple of things now:
        1) I notced on the host's phpinfo(); that there's a variable called _COOKIE['PHPSESSID']. Indeed, the host is putting this cookie on my browser when I go to the site. I don't know if that piece of information changes things or not.

        2) After doing some more digging: The problem seems to be that the session data is not passing from page to page, but it does keep on the login page. Normally, after the user logs in, I would redirect them to the "admin" home page... But if I change the redirect to just refresh the login page, the session data keeps and will print on the page.

        Any help or advice or direction is greatly appreciated. I was hoping to hand this off to my client today! 🙁 But this one issue is the only thing keeping me from being finished! (I think 😉)

          I've narrowed the problem to register_globals being ON on the host. Whereas, my box has it set to OFF. When I turn ON register_globals on my box, I get the exact same result.

          So, now I can test it on my box... which is a plus. I've tried uploading a .htaccess file with:
          php_flag register_globals off
          But that just results in an ISE 500, which makes me think the host does not support this line in .htaccess.

          Anyone know how I can fix this script to work with register_globals ON or OFF? Or know of another work around?

            Could be wrong on this one, so don't quote me, but don't you have to start the session on each page? IOW, if you register the $_SESSION['user'] variable on page one, then go to page 2, you have to call the session_start(); function to actually retrieve the session variables.

            pageIndex2.php ->

            session_start();
            echo $_SESSION['user'];
            

            should work...

            Again, I think this could be the problem.

            And doesn't register_globals have something to do with the difference between typing $_SESSION[] and $HTTP_SESSION_VARS[]?

            I know, it's not a terribly helpful post, but it's early and I've only had 2 cups of coffee so far...

              Maxxd,
              You're right session_start(); does have to be called on every page that needs to access those variables, and I do have this at the top of EVERY page. Sorry I didn't clarify.

              And I believe using $HTTP_SESSION_VARS is for pre PHP 4.1.0 builds... which is not the case here. But I tried it anyway and it did not change anything. It behaves the same using $HTTP_SESSION_VARS or $_SESSION...

              I'm going crazy!

                tomandhannah - Figured you did have the session_start() call, it's just one of those fun stupid things that's really easy to forget. Otherwise, have you tried just changing the variables to simply $SESSION['user_firstname'] and $SESSION['user_lastname']? It's a wild stab in the dark, but the only other thing I can think of off the top of my head...

                  No that's not it either... Again, the variables print on the login page itself just fine. as $_SESSION['user']['firstname'] but if I redirect the user to another page, the data is lost.

                    And the drama concluded today as I narrowed and narrowed and narrowed the problem down to one line of code in the /admin/index.php file. It was a MySQL query for user submitted articles and the result set was named: $user

                    Since register_globals is ON both $user and $SESSION['user'] are valid variables. In this case, the latter $user was unsetting the previous $user variable from the login. This explains why the session would keep, but the data didn't across pages.

                    And all my other pages required some information from the $_SESSION['user'] variable. So, once this variable was replaced by the second $user... it assumed I wasn't logged in and kicked me back to the login screen, while still saying "You are logged in as: (logout)" It should have said something like: "logged in as: James Smith" but couldn't because the $user variable had changed.

                    So, I changed the second $user to $usersub and all is fine. Clearly, a mistake on my part... but since I usually run with register_globals OFF I hardly think much about naming consistency across multiple pages. Whew!

                      You should be able to simple access the value by naming the variable the same name as the parameter. i.e. ?userId=

                      you define the variable as $userId. That's all.

                      Lynn

                        Uhh... ok. I'm not sure what you're suggesting, but the problem has been solved already. Thanks.

                          Write a Reply...