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();
Related
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;
}
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;
}
}
So i'm writing a script in PHP which generates a image so other people can use it too.
But is it possible to get the url's on the pages the scripts are used ?
For example.
http://www.johnexample.com is using my image with this format
<img src="http://www.myurl.com/image.php">
Now i wan't to receive the url of http://www.johnexample.com without GET variables if possible.
It's basically a script that's suppose to track/note down all the websites that are using my image.
At first i though it was possible with this:
$url = (isset($_SERVER['HTTPS']) ? "https" : "http") . "://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
But that only get's the location of the script itself.
Thanks
Oh, that was simpler than i though.
Got it working like this now.
The page with the tag only has to load once and it will save.
Only using Session now because it's being tested local.
Gonna switch it over to a database.
Thanks guys
<?php
session_start();
$url = $_SERVER["HTTP_REFERER"];
if(!strpos($_SESSION["url"], $url)) {
if($url != '') {
$_SESSION["url"] = $_SESSION["url"] . "," . $url;
}
}
$tracker = explode(",", $_SESSION["url"]);
var_dump($tracker);
?>
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.
I am trying to create a login system with Zend that when a user tries to access a restricted page they will be taken to a login page if they aren't signed in. The issue I'm having is getting the URL they were trying to access before hand. Is there a Zend Function that will return everything after the base Url? For example, I need "moduleName/controllerName/ActionName/param1/value1/param2/value2" etc etc. to be sent as a querystring param to the login page (ex: login/?redirect=controllerName/actionName/param1/value1/param2/value)
I can get the controller and action name, and I get get the params, but it already includes the module, controller, and action as well. I'd like to just get what I need. I worked out the long way of doing it like this:
$controllerName = Zend_Controller_Front::getInstance()->getRequest()->getControllerName();
$actionName = Zend_Controller_Front::getInstance()->getRequest()->getActionName();
$paramArray = Zend_Controller_Front::getInstance()->getRequest()->getParams();
$params = '';
foreach($paramArray as $key => $value)
$params .= $key . "/" . $value;
$this->_redirect('/admin/login/?redirect=' . $controllerName . "/" . $actionName . "/" . $params);
but even then I end up with params like module/admin/controller/index/ etc which I don't want. So, how can I just get everything as a string like it is in the URL or at least just the params in a string without the controller and action as param values?
**EDIT: Here is my current solution, but there has got to be a more elegant way of doing this **
$moduleName = $this->getRequest()->getModuleName();
$controllerName = $this->getRequest()->getControllerName();
$actionName = $this->getRequest()->getActionName();
$paramArray = $this->getRequest()->getParams();
$params = '';
foreach($paramArray as $key => $value)
if($key <> "module" && $key <> "controller" && $key <> "action")
$params .= $key . "/" . $value . "/";
$this->_redirect('/admin/login/?redirect=' . $moduleName . "/" . $controllerName . "/" . $actionName . "/" . $params);
I think you can pass along the last request URI like this:
$this->_redirect('/admin/login/?redirect='.urlencode($this->getRequest()->REQUEST_URI));
i know its an old question but for future references:
$this->getRequest()->getRequestUri();
that should get you what you want..
No idea why Zend doesn't have this feature, but this is how I have accomplished it. My use case was that i wanted to redirect old URLs that went directly to controllers using the default routing, to a new API url structure that used routes.
$filtered_params = array_diff_key(
$this->_request->getParams(),
array_fill_keys( array('controller', 'module', 'action' ), 1)
);
$this->_redirect('/api/1.0/?' . http_build_query($filtered_params));
Old question but heres a solution thats worked for me. From here you may want to url encode it etc
$currentRoute = substr($this->getRequest()->getRequestUri(),strlen($this->getRequest()->getBaseUrl()));
You could try using urlencode() and urldecode() to encode/decode the individual param strings. An example would be:
// outputs %2Fpath%2Fto%2Ffile.php
$path = '/path/to/file.php';
echo urlencode($path);