Creating PHP session variable freezes my browser

I just got out of my ASP cave recently and am having trouble setting up PHP for sunlight.

My current problem is a simple logical sequence in which I create a session variable - this step causes my browser to freeze and then act randomly.

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

For brevity, I simply assume that 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 "$ _SESSION ['admin'] = $ a;" line, the redirection works fine, but as soon as I try to create this session variable, my browser freezes until it switches to C.php, where it cannot load files properly. The action of the back button seems to put the browser in an endless loop.

What is this caveman doing wrong?

Thanks,

Brian.

+4
source share
1 answer

I'm just guessing here.

After the header of your location, you must have exit(); . If after redirecting (for example, perhaps a session trying to set a cookie) more data is returned, the redirect will work.

Give him a test and see what happens.

You can also try session_write_close() after assigning a session to force all session-related data to be terminated before attempting to redirect. I still highly recommend exit(); .

+2
source

Source: https://habr.com/ru/post/1397018/


All Articles