PHP header() redirect to the same page before the right one

2616 views php
9

I have an index.php file here:

<?php

session_start();
var_dump($_SESSION['modals']);
$_SESSION['modals'] = [];

?>

and a redirect.php file here:

<?php

session_start();
$_SESSION['modals'][] = ['title' => "test", 'msg' => "message to show"];
header('Location: index.php', true, 301);
exit();

?>

As you see, these are realy basic.

The problem I encounter is that if I load the page redirect.php 2 or 3 times in row, it simply show one entry in the $_SESSION['modals'] variable, as it should. But if I load the redirect.php page several times in row, usualy 4 or more, it start buging. The $_SESSION['modals'] sometimes contain 2 entries, and sometimes 1.

I don't understand why??

Thanks in advance for your responses!

answered question

1 Answer

10

Depending on your session handler, there may or may not be session locking. Additionally, browsers don't exactly wait around when they receive a 301 redirect.

I suspect your session data isn't getting saved before the second script is opening it.

posted this

Have an answer?

JD

Please login first before posting an answer.