By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
431,985 Members | 1,694 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 431,985 IT Pros & Developers. It's quick & easy.

how to get values of a redirected a POST request ?

P: n/a
these are three files below :
submit.html
<html>
<head>
</head>
<body>
<form action="redirect.php" method="POST" >
<input type="text" name="value" value="test" >
<input type="submit" >
</form>
</body>
</html>

redirect.php
<?php
header("HTTP/1.1 303 See Other");
header("Location: post.php");
?>

post.php
<html>
<head>
</head>
<body>
<?php
if(isset($_REQUEST['value'])) {
echo "the value is : <font color=RED >{$_REQUEST['value']}</font>";
}
?>
</body>
</html>
When I submit, it does redirect to 'post.php', but it doesn't show
"test". If I change the submit.html to use the "GET" method ,and append
"?value={$_GET['value']}" to the url int the redirect.php, it works
well. Why the "POST" method doesn't work? I use "apache http server
2.0.58" with "php 5" , "ms IE 6.0" and "firefox 1.5.0".

Jun 15 '06 #1
Share this Question
Share on Google+
7 Replies


P: n/a
Carl wrote:
these are three files below :
submit.html
<html>
<head>
</head>
<body>
<form action="redirect.php" method="POST" >
<input type="text" name="value" value="test" >
<input type="submit" >
</form>
</body>
</html>

redirect.php
<?php
header("HTTP/1.1 303 See Other");
header("Location: post.php");
?>

post.php
<html>
<head>
</head>
<body>
<?php
if(isset($_REQUEST['value'])) {
echo "the value is : <font color=RED >{$_REQUEST['value']}</font>";
}
?>
</body>
</html>
When I submit, it does redirect to 'post.php', but it doesn't show
"test". If I change the submit.html to use the "GET" method ,and append
"?value={$_GET['value']}" to the url int the redirect.php, it works
well. Why the "POST" method doesn't work? I use "apache http server
2.0.58" with "php 5" , "ms IE 6.0" and "firefox 1.5.0".


You lose posted data when you redirect. You would need to re-post the
data along with sending the redirect, which i'm not sure is possible,
never tried it myself.

Why can you simply include the file?

Since you're using apache, you could use mod_rewrite to transparently
rewrite the request, not sure if that would accomplish what you want to
do.

Jun 15 '06 #2

P: n/a
NC
Carl wrote:

these are three files below :
[Snippets deleted]
When I submit, it does redirect to 'post.php', but it doesn't show
"test". If I change the submit.html to use the "GET" method ,and append
"?value={$_GET['value']}" to the url int the redirect.php, it works
well. Why the "POST" method doesn't work?


Because redirection uses the GET method.

You should consider either including post.php into redirect.php or
saving the POSTed values into session variables:

redirect.php
<?php
session_start();
$_SESSION = $_POST;
header("HTTP/1.1 303 See Other");
header("Location: post.php");
?>

post.php
<?php
session_start();
if(isset($_SESSION['value'])) {
echo "the value is : <font color=RED >{$_SESSION['value']}</font>";
}
?>

Cheers,
NC

Jun 15 '06 #3

P: n/a
Richard Levasseur <ri********@gmail.com> wrote:
You lose posted data when you redirect. You would need to re-post the
data along with sending the redirect, which i'm not sure is possible,
never tried it myself.


Yes, it's possible :)

Using Curl, you might try something like this (adjust to fit your
needs):

if( condition )
{
$rePosts = '';

foreach( $_POST as $pk => $pv )
{
$rePosts .= "&$pk=" . urlencode($pv);
}

$rsrc = curl_init( 'http://site_to_redirect_to/remote_script.php' );

if( is_resource($rsrc) )
{
curl_setopt( $rsrc, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
curl_setopt( $rsrc, CURLOPT_POST, 1 );
curl_setopt( $rsrc, CURLOPT_POSTFIELDS, ltrim($rePosts, '&') );
curl_setopt( $rsrc, CURLOPT_HEADER, 0 );
curl_setopt( $rsrc, CURLOPT_FOLLOWLOCATION, 1 );
curl_setopt( $rsrc, CURLOPT_RETURNTRANSFER, 1 );

curl_exec( $rsrc );
curl_close( $rsrc );

exit;
}
else
{
// some error handling...
}
}

hih,

JJS.
Jun 15 '06 #4

P: n/a

NC wrote:
Carl wrote:

these are three files below :


[Snippets deleted]
When I submit, it does redirect to 'post.php', but it doesn't show
"test". If I change the submit.html to use the "GET" method ,and append
"?value={$_GET['value']}" to the url int the redirect.php, it works
well. Why the "POST" method doesn't work?


Because redirection uses the GET method.

You should consider either including post.php into redirect.php or
saving the POSTed values into session variables:

redirect.php
<?php
session_start();
$_SESSION = $_POST;
header("HTTP/1.1 303 See Other");
header("Location: post.php");
?>

post.php
<?php
session_start();
if(isset($_SESSION['value'])) {
echo "the value is : <font color=RED >{$_SESSION['value']}</font>";
}
?>

Cheers,
NC


In fact ,the destination that I want to redirect to is not developed by
me , and they won't change their code for me. They use "POST" to get
values ,I have to redirect the POST request.

Jun 16 '06 #5

P: n/a
Carl wrote:
NC wrote:
Carl wrote:
these are three files below :


[Snippets deleted]

When I submit, it does redirect to 'post.php', but it doesn't show
"test". If I change the submit.html to use the "GET" method ,and append
"?value={$_GET['value']}" to the url int the redirect.php, it works
well. Why the "POST" method doesn't work?


Because redirection uses the GET method.

You should consider either including post.php into redirect.php or
saving the POSTed values into session variables:

redirect.php
<?php
session_start();
$_SESSION = $_POST;
header("HTTP/1.1 303 See Other");
header("Location: post.php");
?>

post.php
<?php
session_start();
if(isset($_SESSION['value'])) {
echo "the value is : <font color=RED >{$_SESSION['value']}</font>";
}
?>

Cheers,
NC

In fact ,the destination that I want to redirect to is not developed by
me , and they won't change their code for me. They use "POST" to get
values ,I have to redirect the POST request.


As others have said - you can't redirect post values. They're sent as part of
the request for the page, and a redirect doesn't resend them.

Your only real choices are to use CURL or send the header information yourself.

Then pass the results back to the client.
--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Jun 16 '06 #6

P: n/a
NC
Carl wrote:
NC wrote:
Carl wrote:

Why the "POST" method doesn't work?


Because redirection uses the GET method.


In fact ,the destination that I want to redirect to is not developed
by me , and they won't change their code for me. They use
"POST" to get values ,I have to redirect the POST request.


You can't. HTTP does not work this way.

Cheers,
NC

Jun 16 '06 #7

P: n/a
NC
Jerry Stuckle wrote:

As others have said - you can't redirect post values. They're
sent as part of the request for the page, and a redirect doesn't
resend them.

Your only real choices are to use CURL or send the header
information yourself.

Then pass the results back to the client.


Alternatively, he could put all necessary information into the form and
POST directly to the page that requires POST inputs...

Cheers,
NC

Jun 16 '06 #8

This discussion thread is closed

Replies have been disabled for this discussion.