I have an android app that uses a URL Connection. The latter part of the URL string is;
./upload_data.php?id=SC1495&image=%3FPNG%0D%0A%1A%0A%00%00%00%0DIHDR%00%00%02X%00%00%01%15%08%02%00%00%00%3F*%0C%3F%00%00%00%03sBIT%05%06%053%0B%3F%3F%00%00%01%3FIDATx%3F%3F%3F1%01%00%00%00%3F%3FOm%0D%0F%3F%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%3F7%3F%26%00%01%40%3F%27%13%00%00%00%00IEND%3FB%60%3F&imagename=bob&imagetime=20140806+121507
When I put this into the browser and use $_GET['image'], it returns the following;
?PNG IHDRX?*?sBIT3???IDATx???1??Om ??7?&#?'IEND?B?`
I am not decoding anything, I just want to get the string with all the %00 etc.
Can someone enlighten me as to why this would be happening?
$_GET['image'] is returning the correct string. The characters in the url are mainly %00 which is equal to NULL in URL encoding. If you want to keep the characters you can use rawurlencode()
$img = $_GET['image'];
echo rawurlencode($img);
Or just
echo urlencode($_GET['image'])
This will convert the characters you've got back to their url encoded form
remove special charactors
if ($_GET['image'] != ""){
$img = htmlspecialchars($_GET['image']);
echo "<h3>".$_GET['image']."</h3><br><img src='http://yoursite.com/images/".$_GET['image']."'>";
}
else {
exit ("No image here =(");
}
Related
I am building a hyperlink that includes a base64 encoded set of parameters as shown below:
$params = base64_encode("member_id={$recipient_id}&api_key=".SECRET_KEY);
$link = HOST_ADDRESS."test.php?k=" . $params;
When the link is executed, the following code runs:
// get the encoded string from the link parameter
$link_parm = $_GET['k'];
$link = substr($link_parm, 0);
// url encode the string to ensure all special characters convert properly - attempt to stop errors
urlencode($link);
// decode the rest of the link
$decoded_link = base64_decode($link);
// get the remaining data elements from the link parameter
$msg_data = preg_split( "/[&=]/", $decoded_link);
On occasion, the $msg data is corrupted and looks like this:
member_id=167œÈ&api_key=secretkey
As you can see the member id is corrupted.
Can someone please help me understand what may be causing this?
Thanks.
For starters, there are a few problems with this beside the issue you describe.
What are you trying to do using $link = substr($link_parm, 0);? This could just be written as $link = $link_parm;. Or, you could of course just do $link = $_GET['k']; or even just use $_GET['k'].
urlencode($link); does nothing as you're not catching its result. The argument is not passed by reference.
Your "attempt to stop errors" should probably be handled differently. By throwing an error when you're receiving unexpected input, for instance.
I have a database in UTF8 unicode ci collation that stores values with special chars like:
oukaïmeden
I want to be able to form urls like:
example.com/oukaïmeden
or
example.com/index.php?id=oukaïmeden
In index.php I set the HTML charset as UTF8 (not that it matters pre output), and save the PHP file as UTF8 unicode ci.
However, no matter what I do, I cannot see to get the string in the form oukaïmeden so I can use it to search the database.
$aparams = explode("/", $_SERVER["REQUEST_URI"]);
extract($_GET);
$id = utf8_decode($aparams[1]);
echo $id;
echo urldecode($id);
echo utf8_decode($id);
echo utf8_encode($id);
I get values like:
ouka%C3%AFmeden
oukaïmeden
I thought my question was "how can I get the string to show the umlaut so I can use it to search/compare etc?" But actually I wonder if I should be searching differently as well?
URLs cannot contain non-ASCII characters. The URL must look like this first and foremost to be correct:
example.com/index.php?id=ouka%C3%AFmeden
That's the correct percent-encoded representation of the UTF-8 encoded word "oukaïmeden". The browser may or may not show this as "oukaïmeden" in your address bar, but the actual URL must be as above.
In PHP, reading this from $_GET will give you the already decoded value. So, to get the UTF-8 encoded string in your PHP script:
$id = $_GET['id'];
Yup, that's it. Nothing more needed.
Here is the output using the so the correct to use is the "utf8_encode"
$id = $_GET['id'];
$id = utf8_decode($id);
echo $id."<br />";
echo urldecode($id)."<br />";
echo utf8_decode($id)."<br />";
echo utf8_encode($id)."<br />";
ouka�meden
ouka�meden
ouka?meden
oukaïmeden
I have been using URL decode on encoded URL variables from $_get.
The current problem I am facing is I have a URL encoded like this:
blah.php?url=http%3A%2F%2Fm.youtube.com%2F#/watch?feature=player_embedded&v=zd7c5tQCs1I&desktop_uri=%2Fwatch%3Fv%3Dzd7c5tQCs1I%26feature%3Dplayer_embedded
I'm not sure what kind of encoding this is, can someone help me? When I use just "urldecode" on this it just returns m.youtube.com
Edit: My problem is not that url decode isn't working, it works if I manually enter this encoded URL and use urldecode(), but when this encoded url is in the actual pages url and I use the _GET function then I try to decode it it stripes off everything after the "#" in the URL.
<?php print urldecode($_GET["url"]);?>
It returns
"http://m.youtube.com/"
instead of
"http://m.youtube.com/#/watch?feature=player_embedded&v=zd7c5tQCs1I&desktop_uri=/watch?v=zd7c5tQCs1I&feature=player_embedded"
I think the issue is that the pound sign is not encoded, if I refresh the page it strips away the pound sing and everything after it, so how do I get around this? Can I still retrieve the info from "GET" even though there is a pound sign? (#)
The problem is that the full link has multiple = signs, and browser cant determine, that the other = signs refer just to the url= parameter.
in your case, at first, you need to use function before link is given to url= parameter:
========================= 1) JAVASCRIPT ======================
<script type="text/javascript">
var mylink = encodeURIComponent('http://testest.com/link.php?name=sta&car=saab');
document.write("http://yoursite.com/url=" + mylink);
</script>
========================= 2)or PHP ===========================
<?php
$mylink = 'http://testest.com/link.php?name=sta&car=saab';
echo 'http://yoursite.com/url='.urlencode($mylink);
?>
so, your output (url parameter) will get like this
http://yoursite.com/url=http%3A%2F%2Ftest.com%2Flink.php%3Fname%3Dsta%
so, the url parameter will get the encoded url.
after that, your .php file needs to decode that "url" parameter-
<?php
$varr = $_GET['url'];
$varr = preg_replace("/%u([0-9a-f]{3,4})/i","&#x\\1;",urldecode($varr));
$varr = html_entity_decode($varr,null,'UTF-8');
echo $varr;
?>
that will give you the correct value
I read on php.net about urldecode function and they say that superglobal $_get is already decoded, ex: "The superglobals $_GET and $_REQUEST are already decoded. Using urldecode() on an element in $_GET or $_REQUEST could have unexpected and dangerous results."
It is encoded into ASCII format .
see http://www.w3schools.com/tags/ref_urlencode.asp
So here is the problem, the pound sign (#) (Hash) wasn't encoded... since I can't go back and re-encode it I have to use javascript (ex. alert(window.location.hash);) to send me the full URL after the hash then I append it to PHP's version of the URL, I THEN use a find and replace function in PHP to replace the "#" with "%23", then I use the urldecode method and it returns the full proper url decoded.
This encoding is called percent encoding or URL encoding. You can use urldecode for decoding it. (Example: http://phpfiddle.org/lite/code/0nj-198 )
I am trying to pass a url for creating an iframe as a parameter of a query string. Because the url that I am passing contains an ampersand, I encode the url with 'urlencode' then append it to the query string.
<?php
$url = "http://www.somesite.com/index.php?option=content&view=article&id=1234:some+article";
$url_encoded = urlencode($url);
?>
On the page where I want to create the iframe, I retrieve the url parameter using the $_GET variable.
<?php
$iframe_source = $_GET[$url];
?>
<iframe id="external-link-frame" src="<?php echo $iframe_source ?>"></iframe>
However $_GET only retrieves the part of the parameter value up to the encoded ampersand.
<?php echo $_GET[$url]; //outputs http://www.somesite.com/index.php?option=content ?>
What must I do in order to send the entire url including the parameters that are part of its own query string.
UPDATE: I am able to do it by encoding the url twice
urlencode(urlencode($url));
Take a look at: https://stackoverflow.com/a/2433211/1359529
I think rawurlencode() will encode the ampersands too.
http://us3.php.net/manual/en/function.rawurlencode.php
I believe, how you append it, the $_GET function thinks that the ampersand signs are signifying new values to get. I bet if you did after that $iframe_view = $_GET[$view] it will output article.
If you want it to get the full URL, I think it's best to encode by replacing & signs with something else and then once you get the url, then replace them back to & signs.
My Browser shows URL with file name as
http://www.example.com/pdf/204177_20090604_Chloorhexidine_DCB_oogdruppels_0%2C1%25.pdf
Actual File name is 204160_20090604_Atropine_DCB_oogdruppels_0,5%.pdf
After urldecode, it gives wrong file name as
http://www.example.com/pdf/204177_20090604_Chloorhexidine_DCB_oogdruppels_0,1%.pdf
Update:
Initially I thought that its problem of URL Decode, but files like name 204153_20090605_Aluminiumacetotartraat_DCB_oordruppels_1,2%.pdf while rendering in browser throws Bad request. I am using Kohana 3 framwork. Is it related with server?
$url = 'http://204160_20090604_Atropine_DCB_oogdruppels_0,5%.pdf';
$encode = urlencode($url);
$decode = urldecode($encode);
echo $url."<br />";
echo $encode."<br />";
echo $decode."<br />";
// outputs
http://204160_20090604_Atropine_DCB_oogdruppels_0,5%.pdf
http%3A%2F%2F204160_20090604_Atropine_DCB_oogdruppels_0%2C5%25.pdf
http://204160_20090604_Atropine_DCB_oogdruppels_0,5%.pdf
All ok. You're error is somewhere else.
You are looking at two different files.
It's not possible to urlencode 204160_20090604_Atropine_DCB_oogdruppels_ into 204177_20090604_Chloorhexidine_DCB_oogdruppels_, encoding does not change alphabetical characters.
The error is most likely in the code that creates the file list and outputs the links; the mapping between link titles and filenames appears to be messed up.
this will give you exact file name m using c#
Server.UrlDecode("http://www.example.com/pdf/204160_20090604_Atropine_DCB_oogdruppels_0,5%25.pdf")
, (comma) is encoded as %2c
% (percent) is encoded as %25 by browsers
if you use Request.Url it'll decode ,(comma) but not %(percent)
So Server.UrlDecode("xyz") decode all characters except %(percent), thats y there's "%25" in the above filename