Redirect post request to get using PHP - php

I need a PHP script that gets a POST request and redirects it to another page as a GET request with all parameters in the URL.
Is this possibile?

You can use the function http_build_query() to generate the GET query string from $_POST.
Afterwards attach it to the redirect URL and use header() with Location for the redirect, for example:
$newURL = 'http://example.com/script.php?' . http_build_query($_POST);
header("Location: {$newURL}");

$URL = "http://thatpage.com/thatpage.php?";
foreach($_POST as $key=>$value) {
$URL +="$key=$value&";
}
then open that $URL page

you need to parse the $_POST variable in order to create a complete GET URL
This question could help you to parse the $_POST array
PHP Parse $_POST Array?

Something like:
foreach($_POST as $k => $v)
{
$getString .= $k . '=' . $v . '&';
}
should format the POSTed variables in the proper format.

Related

Get URL headers in PHP

So, let's say I'm a client entering this URL on my blog website: https://blogbyme.com/post?id=1.
I want the server to see that URL and customize the page based off what post it is.
The main question is - when the request is made, how do I grab the id header's value to send the correct blog post?
Right now I've got this far.
if (!function_exists('getallheaders')) {
function getallheaders() {
$headers = [];
foreach ($_SERVER as $name => $value) {
if (substr($name, 0, 5) == 'HTTP_') {
$headers[str_replace(' ', '-', ucwords(strtolower(str_replace('_', ' ', substr($name, 5)))))] = $value;
}
}
return $headers;
}
}
and that's still not working.
Use the superglobal of PHP $_GET:
echo $_GET["id"];
I want the server to see that URL and customize the page based off what post it is. The main question is - when the request is made, how do I grab the id header's value to send the correct blog post?
You can get the full request URI from:
$_SERVER["REQUEST_URI"]

Redirect with PHP and skip specific parameter

Since I've started using friendly URLS in my website, I'm redirecting every page to the new version, only if the registered user has a "username" in his profile.
So, I'm redirecting from:
https://tribbr.me/post.php?id=850
with:
header("Location:/".$if_username."/post/".$post_id."?".$_SERVER['QUERY_STRING']); exit();
To keep all GET parameters.... but the problem is that this header request, obviously with $_SERVER['QUERY_STRING'] is adding the post id too to the URL, and when redirected, this is the final URL:
https://tribbr.me/TribeMasters/post/850?id=850
Is it possible to just skip the id=850 parameter to the URL redirection? Since it is a duplicated parameter: post/850 and id=850 are the same.
Thanks for helping me :)
#DE_'s answer is best. But If you are not familiar with Regex, This is an alternative way.
function removeGetParam($param){
$params = $_GET;
// removing the key
unset($params[$param]);
// joining and returning the rest
return implode(',', array_map(function ($value, $key) {
return $key.'='.$value;
},$params, array_keys($params))
);
}
$filtered_params = removeGetParam('id');
header("Location:/".$if_username."/post/".$post_id."?".$filtered_params);
David Walsh did a good article on this
https://davidwalsh.name/php-remove-variable
function remove_querystring_var($url, $key) {
$url = preg_replace('/(.*)(?|&)' . $key . '=[^&]+?(&)(.*)/i', '$1$2$4', $url . '&');
$url = substr($url, 0, -1);
return $url;
}

Retrieving whole URL, even with ampersands, using _GET

I made a quick script to retrieve and parse XML. It's simply for internal use and I though that appending the feed URL to the script address would be a convenient way to initialize the script...
www.example.com/feed_analyzer.php?url=www.example.com/an_xml_feed.xml
Then I simply grab the URL...
if (isset($_GET['url'])) {
$xml_url = $_GET['url']
}
...retrieve the file at $xml_url, parse etc.
All was fine until this URL came along with pesky parameters:
www.example.com/an_xml_feed.xml?foo=bar&rice=chips
That of course left me with the URL "www.example.com/an_xml_feed.xml"
I have managed to "patch back together" the whole URL using this clunky code:
if (isset($_GET['url'])) {
foreach($_GET as $key => $value){
$got .= "&".$key."=".$value;
}
$xml_url = ltrim($got,'&url=');
}
Can someone please suggest a more elegant approach.
You can directly use this to get the whole query url:
ltrim($_SERVER['QUERY_STRING'], 'url=');
urlencode() is the way to go :)
Try like this
if (isset($_GET['url'])) {
$query_string = $_SERVER['QUERY_STRING'];
if (!empty($query_string)) {
list($key, $xml_url) = explode('url=', $query_string);
echo $xml_url;
}
}

How to pass variables received in GET string through a php header redirect?

I'm receiving values in a GET string from Aweber upon user's submission of a form. I take the variables they send and submit them to a SMS gateway to notify a 3rd party of the submission by text message.
Here's my problem. I need to redirect the page that performs the outgoing SMS commands in a php header to another page that finally displays the GET variables sent from Aweber.
I can retrieve the variables and their values in the first page. How do I pass them to the second page?
Here is the code I'm using on the first page (sms.php) to collect the variables sent by Aweber:
$fname = $_GET['name'];
$femail = $_GET['email'];
$fphone = $_GET['telephone'];
....etc
header('Location: confirmed.php');
exit;
First convert the $_GET HTTP variable into a query string using
$query = http_build_query($_GET);
Then append the query string variable to your redirect header
header('location: domain.com'."?".$query);
Done.
session_start();
$_SESSION['fname'] = $_GET['name'];
$_SESSION['femail'] = $_GET['email'];
$_SESSION['fphone'] = $_GET['telephone'];
....etc
header('Location: confirmed.php');
and get it on the next page like:
session_start();
$fname = $_SESSION['fname'];
$femail = $_SESSION['femail'];
$fphone = $_SESSION['fphone'];
....etc
You don't need to store them in a session, you can easily pass them with your location header:
$fname = $_GET['name'];
$femail = $_GET['email'];
$fphone = $_GET['telephone'];
//now a header with these var's:
header("Location: confirmed.php?name=".$fname."&email=".$femail."&telephone=".$fphone);
In confirmed.php you can get these variables with $_GET method.
Please for anyone reading this in future, use sessions for this kind of variable value transfer because if you rely mostly on adding variable to header then if the user in still on that form and carries out an action that changes the value of the header then your own variable value changes since it depends on the header......simply put, USE SESSIONS.
Store them in the session:
$_SESSION['fname'] = $_GET['name'];
Use session_start at the beginning of each file.
Try this. It worked perfectly for me.
if ($_GET)
{
$query = str_replace("%3D", "=", str_replace("%26", "&", strval(urlencode(http_build_query($_GET)))));
header('location: https://www.example.com'.'?'.$query);
}
else
{
header('location: https://www.example.com');
};
The best you can do is put all your POST variables to a session like this:
On page1.php put:
//Start the session
session_start();
//Dump your POST variables
$_SESSION['post-data'] = $_POST;
And on page2.php put: (If on page1.php we use a normal POST form submit with form action="page2.php")
//Start the session
session_start();
//Access your POST variables
foreach ($_POST as $key => $value) {
${$key} = $value;
$_SESSION[$key] = $value;
}
//Unset the useless session variable
unset($_SESSION['post-data']);
Or on page2.php put: (If on page1.php we use a self submit with form action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?> and then use a header("Location: page2.php"); to move to the page2.php and pass our POST variables via a session)
//Start the session
session_start();
//Access your POST variables
$_POST = $_SESSION['post-data'];
foreach ($_POST as $key => $value) {
${$key} = $value;
$_SESSION[$key] = $value;
}
unset($_SESSION['post-data']);
I literally spent hours figuring that out because all the forums put it wrong or incomplete.
Now it's as easy as just calling the variables you passed from the page1.php like this for example: <b>Points: </b><?php echo $points; ?> and that's it!!
When situating the header('Location: page2.php'); in a if condition, etc. make sure that it will be in the first PHP script of the page and above any HTML output.
This works use this sentex
header('location:member_dashboard.php?id='.$id);

How to pass GET variables from php to php on another server

In a php script I am receiving some data:
$data = $_POST['someData'];
How can I do something like this:
goToThisUrl( "http://someDomain.com/someScript.php?data = ".$data );
or if it is easier how can I do it by POST?
BTW.
This is not happening in a browser, the first php script is getting called by a cart when the order is paid for (if it makes any difference)
Replace goToThisUrl with the real function file_get_contents and remember to urlencode($data) and that would work just fine.
If you want to POST the data instead, look at cURL. Typing "[php] curl post" into the search box will get you the code.
If you want to send the user there, then:
header('Location: http://someDomain.com/someScript.php?data='.$data);
exit;
Or if you just want to call the other server, you can do:
$response = file_get_contents('http://someDomain.com/someScript.php?data='.$data);
Both assume data is already a urlencoded string, you might want to use 'data=' . urlencode($data) or just http_build_query($data) otherwise.
foreach ($_POST as $key => $val) {
$qs = urlencode($key) . "=" . urlencode($val) . "&";
}
$base_url = "<url here>";
$url = $base_url . "?" . $qs;
header('Location: $url'); exit();

Categories