sharky
sharky

Reputation: 11

Redirect in php server side after http request

I am very new to php so this question might be trivial. I am trying to understand if it is possible to redirect the browser from php server side to the page returned by HTTP request.

I have a HTTP Post request looking like so:

$postdata = http_build_query(
    array(
       "someData" => "data" ,
    )
);

$opts = array('http' =>
    array(
        'method'  => 'POST',
        'content' => $postdata,
        'header'  =>
            "Cookie: someCookie" .
            'content-type: application/x-www-form-urlencoded; charset=utf-8'
    )

);

$context  = stream_context_create($opts);
$result = fopen('myWebsiteUrl', 'r', false, $context);

var_dump(stream_get_contents($result));

In the post I am being redirected to a different page with Get. I am trying to force the browser to move to the 'redirected' page. With the above code I am getting back the html of the redirected page but what I'm after is an actual redirect. The option of retrieving the redirect URL and doing the redirect myself in PHP doesn't work because the Get request has to happen within the same session as the Post.

Upvotes: 1

Views: 482

Answers (1)

jonas3344
jonas3344

Reputation: 201

header('Location: whereveryouwantogo.php?get=123456');

does a redirect for you with a GET-parameter attached. I'm not sure if I really got your question right, but I'm pretty sure you could do what you want/need with the header-command.

Upvotes: 1

Related Questions