Pass a full url with GET parameters via GET - php

I'm trying to create a intermediary page, i.e user clicks on a link that leaves the site a page tells you that you're now leaving the site.
An example link would look like this:
http://example.com/transitionpage.php?r=http://www.google.com
transitionpage.php then works with a simple
$redirectto = $_GET['r'];
header( "refresh:2;url=".$redirectto );
However I'm running into the problem that if the url you're redirecting to also has multiple GET parameters in it, the domain gets cut off at the first occurrence of &
So if the link was originally:
http://example.com/transitionpage.php?r=http://www.google.com?par=1&par=2
It would become:
http://example.com/transitionpage.php?r=http://www.google.com?par=1
Which is unfavorable.
How do I pass on the full URL via GET without it getting chopped off ? Do I have to escape it ?

You can do URL encoding: http://www.w3schools.com/tags/ref_urlencode.asp
Your get query would translate to: http://example.com/transitionpage.php?r=http%3A%2F%2Fwww.google.com%3Fpar%3D1%26par%3D2
These are the PHP methods you need: http://php.net/manual/en/function.urlencode.php http://php.net/manual/en/function.urldecode.php

Use:
$link = 'http://example.com/transitionpage.php?r='. urlencode('http://www.google.com?par=1&par=2');

Use the built in function for Encoding Urls
for example
urlencode("http://www.google.com");
for function refrence
Urlencode Php

Related

URL being re-encoded?

I have a few links that look like this:
https://www.example.com/find?category=food%20%26%20drink
Clicking on the link should take me to a page where I can GET the variable, and it SHOULD read "food & drink".
However, when I click the link, it takes me to this url instead:
https://www.example.com/find?category=food%2520%2526%2520drink
the variable reads: food%20%26%20drink.
If I paste the first url into the search-bar directly, it works fine. But if I click on it as a link, then it gets re-encoded somehow.
Any idea how to get it to read "food & drink" even though it comes from a different page?
many thanks in advance!
Realized the links were written as http instead of https.
Consequently, they were being re-written by the htaccess file to https when clicked, and also being re-encoded at the same time.
The link you have is double encoded. The possible solution to this would be
Find line of code where the link getting encoded again and make suer not encode if encoded already. Couple of examples are given here Click Here
If there is no way you can change the code form where the URL is getting generated, then you have to use urldecode twice to parse the url params
<?php
$query = "https://www.example.com/find?category=food%2520%2526%2520drink";
$param = explode("=", $query);
print_r(urldecode(urldecode($param[1])));
?>
Hope this helps!

How to use $_GET when there is &038; at place of & in url

I have wordpress template in which URL is generating in for of xyz.com/?pickup_location=662#038;pickup_date=2018%2F08%2F08&return_date=2018%2F08%2F10
you can see there & at a place of & so when I trying to get value by $_GET['pickup_date'] it showing blank.
I used $_SERVER['REQUEST_URI'] to get complete URL but it's also not giving me complete URL
it's look like there is use of esc_url() wordpress function.
if anyone can help me to get this complete url as a string it can also work for me I could split a string with a parameter name
Your problem is the #.
Everything that comes after the # is only accessible by the client and not the server.
If you need a # in your URL like that you need to URL encode it:
urlencode('#'); // %23
In your case that will be something like this:
$url = "xyz.com/?pickup_location=" . urlencode("662#038;") . "pickup_date=2018%2F08%2F08&return_date=2018%2F08%2F10";

PHP header_redirect with multiple variables

I am writing a PHP script that will redirect based on a link like this:
header_redirect($_GET['redirect']);
And the URL being:
https://www.url.com/index.php?change_language=english&redirect=other_page.php?category=1234&limit=24
As you can see, the actual page is
https://www.url.com/index.php?change_language=english
and the redirect is then to another page with multiple variables like:
&redirect=other_page.php?category=1234&limit=24
However, when I run the link above, I only get redirected to "other_page.php" and the additional variables are lost.
How would I achieve this?
You can solve this problem using some encryption and decryption trick, I have used base64_encode and base64_decode() function to solve your problem.
Step1: in your html page
<?php $redirectUrl = base64_encode('other_page.php?category=1234&limit=24'); ?>
Link1
Step 2: in your header_redirect() function, you can use base64_decode() function decode the redirect stings and get the expected one.
function header_redirect($redirect){
$redirect = base64_decode($redirect); // you can get the expected redirected url with query string
//your redirect script
}
Depends on what you're trying to do.
Option 1: Use http_build_query.
Try:
$target=$_GET['redirect'];
unset($_GET['redirect']);
$query_str=http_build_query($_GET);
header_redirect($target.'?'.$query_str);
If your starting URL is this:
https://www.url.com/index.php?change_language=english&redirect=other_page.php&category=1234&limit=24
You would then be redirected to:
https://www.url.com/other_page.php?change_language=english&category=1234&limit=24
Option 2: Use rawurlencode and rawurldecode.
However, if your goal is to be redirected to whatever you have stored in $_GET['redirect'] (and ignore any other variables in the URL), then you would need to encode the other_page.php&category=1234&limit=24 bit before you put it into your starting URL. This will effectively escape the special characters and allow you to simply call header_redirect(rawurldecode($_GET['redirect']));.
Say your starting URL is then:
https://www.url.com/index.php?change_language=english&redirect=other_page.php%3Fcategory%3D1234%26limit%3D24
You would then be redirected to:
https://www.url.com/other_page.php?category=1234&limit=24

PHP - Get path minus root

So I have this link:
http://kenthomes.net/Amelia-Cove (We use an alias system.)
Then I open a pop-up iframe (http://kenthomes.net/shareplan.php?mod=39)
How I can I pass the string "Amelia-Cove" to that page?
My best guess was to make the link = to http://kenthomes.net/shareplan.php?mod=39&plan=Amelia-Cove
But how to I retrieve only "Amelia-Cove" from the initial page?
Try this:
$_SERVER['REQUEST_URI']
In your above example, this would have a value of: /Amelia-Cove. If you want to get rid of the /, try this:
trim($_SERVER['REQUEST_URI'],'/');
You can use the GET protocol to receive parameters in the URL:
http://kenthomes.net/shareplan.php?mod=39&plan=Amelia-Cove
then fetch these parameters through the PHP GET global variable:
$_GET['plan']
I'm assuming OP will not always fetch Amelia-Cove but rather want that part of the url and used Amelia-Cove as an example and knows how to use $_GET
To fetch that part of the url and pass it through $_GET I would suggest using this:
parse_url($url, PHP_URL_PATH)
Full parse_url documentation

URL Redirect, HTML, PHP

I want to link links on my website via go.php?urlhere, I have been told and I have tried using go.php?url=urlhere however the URL's to which I redirect, redirect to another URL with-in it for example go.php?http://.com/click?p=0&a=0&url=http://.com, many of the redirect I have tried to use simply copy the URL in the go.php file and use a meta refresh or a window.location reload; however they redirect to the second URL and not the first one. Sometimes when I do actually get it to redirect the first part of the redirected URL gets all the dots changed to "_" which stops it redirecting.
I want to have something like this website using on its "Buy It Now" buttons
http://www.searchchief.co.uk/Search/Sony-PSP-Go
So I think this is what you are asking: How do I redirect to a page using a url as a query string parameter?
eg.
http://www.myurl.com/go.php?url=http%3A%2F%2Fwww.myotherurl.com
You must do two things to achieve this on the url:
have a query string parameter. eg.
go.php?url=mysite.com or
go.php?redirect=mysite.com not
just go.php?mysite.com.
you must URL ENCODE this query
string parameter value. eg. go.php?url=http%3A%2F%2Fwww.myotherurl.com
NOT go.php?url=http://www.myotherurl.com. In php you can use the urlencode() function or http://meyerweb.com/eric/tools/dencoder/ can do it for you if you have a small number of urls to convert.
On the PHP side of things you will use the following
<?php
header('Location: '.urldecode($_GET['url']));
exit();
?>
You can do other basic checks on the php side of things eg. check for a valid url format etc
I think you want to use the header command.
So, in your case you would do this:
For a url in this syntax: go.php?url=thisurl
This would be the code
<?php
header('Location: ' . $_GET['url']);
?>

Categories