Bruce
Bruce

Reputation: 89

Creating a PHP session variable hangs my browser

I've only recently emerged from my ASP cave and am having trouble adjusting to the sunshine of PHP.

My current problem lies with a simple login sequence in which I create a session variable - that step causes my browser to hang and then act erratically.

From my login page (A.php) the login form is directed to B.php (below) which processes the password, creates the session variable and then redirects the user to another file (C.php).

For brevity, I'm just assuming the login is successful. B.php contains the following:

<?php
session_start();
require "../scripts/base/toolbox.php";

fnProcessLogin();

function fnProcessLogin(){
    $passwd = strtoupper($_POST["passwd"]);
    if (strlen($passwd)==0)
    {
    $passwd=strtoupper($_SESSION['plpassword']);
    unset($_SESSION['plpassword']);
  } 
  try{
    $db = Database::getDB();
    $sql="SELECT securitylevel, staffID, staffname, stafflname, staffemail, iRoleID FROM staff WHERE staffpasswd=?;";
    $data = array($passwd);
    $query = $db->prepare($sql);
    $query->execute($data);
    if($query->rowCount()>0){
      $row = $query->fetch();
      $a=$passwd."|".$row['staffID']."|".$row['staffname']."|".$row['stafflname']."|".$row['staffemail']."|".$row['iRoleID'];
      $_SESSION['admin'] = $a;
      header('Location: C.php');
    }

 } 
  catch(PDOException $pe){
    echo "We are sorry, but we cannot complete this database operation.";
    file_put_contents('PDOerrors.txt',$pe->getMessage(),FILE_APPEND);
  }
} 

?>

If I comment out the "$_SESSION['admin'] = $a;" line, the redirection works fine, but as soon as I try to create that session variable, my browser hangs, until eventually going to C.php where it fails to load any files properly. Back button action seems to place the browser in an endless loop.

What's this caveman doing wrong?

Thanks,

Brian.

Upvotes: 4

Views: 602

Answers (1)

lll
lll

Reputation: 12889

I'm just making a guess here.

You need to have an exit(); after your location header. If more data is being output after your redirect (like perhaps, a session trying to set a cookie) the redirect will fail.

Give it a test and see what happens.

You could also try a session_write_close() after your session assignment to force all session related data to be finished before attempting the redirect. I still highly recommend the exit(); though.

Upvotes: 2

Related Questions