user2650277
user2650277

Reputation: 6739

Redirect a URL with Post data

I want to redirect a user from page1 to page2 with some POST data .Page1 and page2 are on two different domains and i have control over both of them

Page 1

<?php
$chars="stackoverflowrules"
?>

I want to submit the chars as a post data and the redirect to page 2.

Then one page 2 i want to use the POST data like

<?php
$token = $_POST['chars'];
echo $token;
?>

Upvotes: 4

Views: 19305

Answers (3)

user2650277
user2650277

Reputation: 6739

I did it using a form and JS

On page 1

<?php
$chars="stackoverflowrules";
?>
<html>
<form name='redirect' action='page2.php' method='POST'>
<input type='hidden' name='chars' value='<?php echo $chars; ?>'>
<input type='submit' value='Proceed'>
</form>
<script type='text/javascript'>
document.redirect.submit();
</script>
</html>

On page 2

<?php
$token = $_POST['chars'];
echo $token;
?>

Upvotes: 4

user3141031
user3141031

Reputation:

You want to use curl() for this.

On page1.php, do the following:

$data = $_POST; 

// Create a curl handle to domain 2
$ch = curl_init('http://www.domain2.com/page2.php'); 

//configure a POST request with some options
curl_setopt($ch, CURLOPT_POST, true);

//put data to send
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);  

//this option avoid retrieving HTTP response headers in answer
curl_setopt($ch, CURLOPT_HEADER, 0);

//we want to get result as a string
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

//execute request
$result = curl_exec($ch);

// now redirect to domain 2
header("Location: http://domain2.com/page2.php");

On page 2, you may retrieve the POST data:

<?php

$token = $_POST['secure_token'];
echo $token;

?>

Upvotes: 0

An Phan
An Phan

Reputation: 2568

  1. On page 1, use curl to post the data to page 2.
  2. There, store the POST'ed data somewhere (database?).
  3. Redirect from page 1 to page 2
  4. Retrieved the data back.

Upvotes: 2

Related Questions