-1

Well this is really confusing. I have two pages, A & B. Both use the following code:

 $row = mysqli_fetch_array($data);
     $_SESSION['user_id'] = $row['user_id'];
     $_SESSION['username'] = $row['username'];
     setcookie('user_id', $row['user_id'], time() + (60 * 60 * 24 * 30));    // expires in 30 days
      setcookie('username', $row['username'], time() + (60 * 60 * 24 * 30));  // expires in 30 days
      $home_url = 'http://' . $_SERVER['HTTP_HOST'] . dirname($_SERVER['PHP_SELF']) . '/main.php';
      header('Location: ' . $home_url);

A will execute the code after the setting of the headers (removed for your ease) and then set the headers; B will execute the code after the setting of the headers and not set the headers.

I have checked out NUMEROUS answers, and there is no whitespace, the main.php file is set as normal, the A & B files look identical but with different content before the IF statement which irons out.

No error appears; the redirect (which should have headers set and react accordingly acts as if you were logged out (hence no headers active) on B, whereas on A it appears as intended.

The only difference between the two code blocks setting the cookies (both in a IF statement) is that the IF statement is different, according to code before it's appearance.

What (if any) kind of things should I look out for in terms of differences between the codes of A & B?

(PS: I also know that the headers aren't being set because the session is not started, by putting a IF $_SESSION ... towards the end of the code.

Sam Bowyer
  • 60
  • 7

1 Answers1

1
session_start();
$row = mysqli_fetch_array($data);
$_SESSION['user_id'] = $row['user_id'];
$_SESSION['username'] = $row['username'];
setcookie('user_id', $row['user_id'], time() + (60 * 60 * 24 * 30));    // expires in 30 days
setcookie('username', $row['username'], time() + (60 * 60 * 24 * 30));  // expires in 30 days
$home_url = 'http://' . $_SERVER['HTTP_HOST'] . dirname($_SERVER['PHP_SELF']) . '/main.php';
header('Location: ' . $home_url);
exit;

See

Community
  • 1
  • 1
MagePal Extensions
  • 17,646
  • 2
  • 47
  • 62
  • Thanks for the answer; moving session_start() aloud me to notice the code above was defining a if statement which ended immediately thus shutting down all the code inside of it..... although the fact that some got executed still eludes me.... – Sam Bowyer Nov 01 '12 at 23:31