0

I am learning php, I want to create a page users can send a post response, redirect to another page and pass the token i get from a service in the body to a header to the redirect site.

I understand that i can redirect via header("Location: http://www.example.com/"); , how do i capture a body parameter of the form Token blahblah and then pass it as a header Bearer blahblah

So far i have

<?php 
if(isset($_POST['submit']))
{
    $headers = $_POST['token'];

    if ($headers) {
        header('Location: http://www.example.com/');
    } 
}
?>

I am ok with adding javascript because it seems like its not possible to send custom headers with redirect in php - Redirect to page and send custom HTTP headers

Illusionist
  • 5,204
  • 11
  • 46
  • 76
  • 1
    You cannot redirect someone to another page and then take data from that page if its not yours. When you send a header (here Location) you dont redirect the user to a page. You send an instruction to the browser to do so, and every standard compilant browser will follow it (but is not forced to). If you want to receive a token to authorize/authenticate for an api you normally use something like [`curl`](https://www.php.net/manual/de/book.curl.php) to communicate with that api and pass the credentials. – Code Spirit Nov 06 '19 at 19:59
  • That page is indeed mine. – Illusionist Nov 06 '19 at 20:07
  • Can you please explain in detail what you want to do? Redirect to a page and take a token back and somehow use javascript is not much information. – Code Spirit Nov 06 '19 at 20:08
  • so i am using a service that sends a token in body and i need to capture that token and send it to another service that expects it in the headers, so I thought I would write a small proxy page that would capture this token from body and send to a header – Illusionist Nov 06 '19 at 20:10
  • Then just put it in your form and submit it. – Code Spirit Nov 06 '19 at 20:14
  • Just proxy the request in your post request using curl. The Guzzle client library can make this very easy. In your post endpoint, you will create a new request with the data from the body in a header location. You are just applying the [adapter pattern](https://en.wikipedia.org/wiki/Adapter_pattern) to a http request. – Alex Barker Nov 06 '19 at 20:31
  • Thanks, i don't have any control over the two services, but i am just trying to intercept the response and copy the token from body to headers to make them happy – Illusionist Nov 06 '19 at 20:36

1 Answers1

-1

If you want send additional header, you can use header() function for token too. Like so:

<?php 
if(isset($_POST['submit']))
{
    $headers = $_POST['token'];

    if ($headers) {
        // your additional header
        header('Bearer: blahblah');

        header('Location: http://www.example.com/');
    } 
}
?>
Naomi
  • 46
  • 5