PHP header ("Location:") inside a function redirects without a call function

I am using a function with a name headerLocation()to redirect correctly. This is the relevant part of the function, I use it to redirect and then display the corresponding message (personal preference).

function headerLocation($location,$message)
{
    $_SESSION['output'] = $message;
    header("Location: ". $location);
}

The problem is that when the script starts, the part is header("Location: ". $location);activated without even calling the function (when I initially require it to parse php). This makes the function useless.

Is there any way around this?

+3
source share
5 answers

. -, . ?

+3

. exit .

function headerLocation($location,$message)
{
    $_SESSION['output'] = $message;
    header("Location: ". $location);
    exit;
}
+10

$_Session.

session_write_close();

.

Edit: $ .

+1

, , (ob_start()) , , .

+1

header() , headerLocation().

I assume that you do not see $_SESSION['output']hold $message, and this makes you think that the function is not executing. Try writing a new file instead? I bet it will be.

The reason $_SESSIONmay not contain your values, probably due to P3P and your browser and / or PHP configuration.

Are you also sure you don't want to call die()/ exit()after the redirect header()?

+1
source

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


All Articles