I have the following which works fine:
header("Location: /index.php", true, 300);
but I want the filename to be dynamic, i.e.
header("Location: /" . $_SESSION['initiating_url']);
This works fine, however, I still need the , true, 300
at the end, which I can't seem to figure out:
header("Location: /" . $_SESSION['initiating_url'] . "\"", true, 300);
header("Location: /" . $_SESSION['initiating_url'] . """, true, 300);
but they just don't seem to work correctly. The bottom one gives me syntax error in my editor. and the second to last example doesn't redirect at all.
You don't need to add an explicit closing "
when concatenating strings:
header("Location: /" . $_SESSION['initiating_url'], true, 300);
For clarity:
$path = 'home';
echo "Location: /" . $path; // Location: /home
echo "Location: /home"; // Location: /home
echo "Location: /" . $path . "\""; // Location: /home"
echo "\"Location: /" . $path . "\""; // "Location: /home"
You could also use the advantage of having the string parsed to do this instead:
header("Location: /{$_SESSION['initiating_url']}", true, 300);
If you ever need to join strings, you should use single quotes because they are a bit faster (they dont get parsed or something) like this: $foo='World'; echo 'Hello '.$foo;
. Thats also good if you dont want the to be turned into an escape character where $foo='World'; echo "Hello $foo";
would parse it. (totally off-topic but helpful I guess)