URL being re-encoded? - php

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!

Related

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

Pass a full url with GET parameters via GET

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

Paste variables to $_GET

I have two pages. First one we open with $_POST variables in its url, the second one opens inside first via iframe. Both php files, second is for html manipulation.
Variables I got in $_POST are passed to iframe via $_GET:
echo '<iframe src="index.php&first=' . $first . '&second=' . $second . '&third=' . $third . '&iframe=true"></iframe>';
$first, $second, $third variables has text inside them with some html and new lines (\n).
The problem is, when data is passed to iframe by $_GET, all the new lines in variables disappear.
Tryed to pass variables like base64_encode($first), and then decode them by base64_decode(). It works buggy, some parts of text don't decode correctly, maybe because of bad symbols in iframe url.
Also tryed to throw all the variables into single array, serialize it and then encode by base64 - this way server gives error 500 (it also gives the same error for 404).
Please don't ask me why I did such structure of pages. It should not be changed.
What is the solution for this?
What about an urlencode after the base64_encode?
Depending on your situation you could also use Javascript to access the parent frame.
You could store the data in a javascript array of the first window, then the iframe sub window could call it via parent.*
Some more details from other questions.
You could write the contents of $first,$second,$third to first.txt,second.txt,third.txt and then open the text files inside your iframe script
Your initial approach is wrong.
POST variable shouldn't go anywhere.
After the POST request server have to order browser to reload the page.
Whole page, not only iframe in it.
After that reload you may show any iframes to user.
To pass the data there, a session would be ideal solution.
However, certain solution depends on the data nature and overall purpose of all the mess.

url or content as a variable in the header of the page

I am designing a site where external links form various are being shown on my page. I am using
$url=$_GET['url'];
$website_data = file_get_contents($url);
echo $website_data;
so essentially a user would click on a hyperlink which is something like www.test.com/display_page.php?url=http://www.xyz.com/article/2.jpg
My page, list_of_images.php, typically has a list of images with href for each image as above on the page and when any image is clicked it would go to display_page.php, which would show our banner on the top of this page, some text and then this image beneath that. This image could be from any website.
I am currently sending the url directly and grabbing it using GET. I understand that users/hackers can actually do some coding and send commands for the url variable and could break the server or do something harmful and so i would like to avoid this method or sending the url directly in the header. what is the alternate approach for this problem?
The safe approach is to use a fixed set of resources stored in either an array or a database, and the appropriate key as a parameter.
$ress = Array('1' => 'http://www.google.com/', ...);
$res = $ress[$_GET['res']];
I would make sure the url starts with http:// or https://:
if(preg_match("`^https?://`i", $_GET['url']))
// do stuff
You may also want to make sure it isn't pointing anywhere internal:
if(preg_match('`^https?://(?!localhost|127\.|192\.|10\.0\.)`i', $_GET['url']))
// do stuff
Rather than a big dirty regex, you could go for a more elegant host black-list approach, but you get my drift...
Try POST....
Try doing this using POST method

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