views:

177

answers:

3

I am creating a login script that stores the value of a variable called $userid to $_SESSION["userid"] then redirects the user back to the main page (a side question is how to send them back where they were?). However, when I get back to that page, I am echoing the session_id() and the value of $_SESSION["userid"] and only the session id shows up. It had occured to me that maybe my redirect page needs to have at the top, but if this were true, then the session_id I'm echoing would change each time I end up on the page that is echoing it. Here is the script:

    <?php
session_start();
include_once("db_include.php5");
doDB();
//check for required fields from the form
if ((empty($_POST['username']) && empty($_POST['email'])) || empty($_POST['password'])) {
header("Location: loginform.php5");
exit;
}   else if($_POST["username"] && $_POST["password"]){

    //create and issue the query
    $sql = "SELECT id FROM aromaMaster WHERE username='".$_POST["username"]."' AND password=PASSWORD('".$_POST["password"]."')";
    $sql_res =mysqli_query($mysqli, $sql) or die(mysqli_error($mysqli));

    //get the number of rows in the result set; should be 1 if a match
    if(mysqli_num_rows($sql_res) != 0) {
      //if authorized, get the userid
      while($info = mysqli_fetch_array($sql_res)) {
        $userid = $_info["id"];
      }
      //set session variables
      $_SESSION['userid'] = $userid;

      mysqli_free_result($sql_res);
      //redirect to main page
      header("Location: loginredirect.php5");
      exit; }
    } else if($_POST["email"] && $_POST["password"]) {

          //create and issue the query
    $sql = "SELECT id FROM aromaMaster WHERE email='".$_POST["email"]."' AND password=PASSWORD('".$_POST["password"]."')";
    $sql_res =mysqli_query($mysqli, $sql) or die(mysqli_error($mysqli));

    //get the number of rows in the result set; should be 1 if a match
    if(mysqli_num_rows($sql_res) != 0) {

      //if authorized, get the userid
      while($info = mysqli_fetch_array($sql_res)) {
        $userid = $_info["id"];
      }
      //set session variables
      $_SESSION['userid'] = $userid;

      mysqli_free_result($sql_res);

      //redirect to main page
      header("Location: loginredirect.php5");
      exit;}
      } else {
      //redirect back to login form
      header("Location: loginform.php5");
      exit;
    }
    mysqli_close($mysqli);
?>
+1  A: 

You need to call session_write_close() to store the session data changes.

Side answer: you can use the $SERVER["HTTP REFERER"] to redirect back, if it was filled by the browser

Zed
thanks, I'll look into the referer thing. I may as well ask, though, what you mean 'if it was filled by the browser'?
session_write_close() should be called for you
Tom Haigh
In some (most?) browsers you can tell the browser not to send a referer header. For example in firefox set network.http.sendRefererHeader value to 0 to disable sending referrers.
Zed
Ah, I see. So, I would just use header("Location: ".$_SERVER["HTTPREFERER"].""); ?
+3  A: 

You're doing this:

while($info = mysqli_fetch_array($sql_res)) {
    $userid = $_info["id"];
}

Where you should do this:

while($info = mysqli_fetch_array($sql_res)) {
    $userid = $info["id"];
}
Ropstah
Oh you made the same typo twice in your code by the way, so make sure you change it at both points!
Ropstah
aren't both those loops identical?
mrinject
No, please note the change from $_info['id'] to $info['id']
Ropstah
My answer as far as debugging would eventually find this - but yeah, The session value is being set wrong, so the session value is echo'ing wrong.
McAden
Ha, I know there's two the same :)They are identical, but one works if the user has entered their username and password, and the second if they've only entered their email address and password. I could have worked that into once condition and then decided inside which query to make, but that way was just simpler. Thanks, guys!
+1  A: 

Make sure:

   <?php
   session_start();

Is at the top of each page.

Additionally, you can test by commenting out your redirects and echo'ing the value you're setting with to make sure you're retrieving/storing the correct value to begin with.

McAden