Pass url with character %20 as ajax variable - php

I need to pass a url to a php page. the url may or may not contain special characters and encoded variables.
for example consider the ajax request.
var url = 'http://siteaddr.com/abc%20cdf%202012%20movies%20software/';
$.get('check_link.php',{url:url},function(){
//some function
});
the echo result of variable $_GET['url'] in the php page is http://siteaddr.com/abc cdf 2012 movies software/
the %20's are converted into spaces. I need to receive the url as it is.
tried encoding and decoding the url. but I didn't got the result as i need.

You're not supposed to define a variable with the name that will be used as a key.
Encoding the URL again, before sending it will make PHP decode the double encoded URL.
var link = 'http://siteaddr.com/abc%20cdf%202012%20movies%20software/';
$.get('check_link.php',{url: encodeURIComponent(link)}, function(){
//some function
});
// Double encoded URL: Note that % has become %25
// http%3A%2F%2Fsiteaddr.com%2Fabc%2520cdf%25202012%2520movies%2520software%2F
http://jsfiddle.net/zNqac/

Related

PHP - GET url encode #, \ and & sign

This PHP code:
$test = '#&\\';
echo rawurlencode($test);
Results in this:
http://example.com/test=%40%26%5C
Which in a $_GET request in a url becomes something like this:
test=#&\
The above is not an allowed $_GET variable.
How can I still use a $_GET variable with these values, encode them and decode them back to get these values?
The URL will most certainly be http://example.com/test=%40%26%5C if you create the URL as http://example.com/test=%40%26%5C. When PHP receives a request for this URL, PHP automatically decodes the query string and puts it into $_GET. The URL didn't change, you simply have the decoded value of the query string available to your PHP script in the $_GET variable.
Additionally the browser may decide to display the URL in a decoded form in the address bar for usability/readability. It does not mean that the actual URL contains #&\ characters.

PHP Header: Param value got truncated after #

I'm using following code for redirection in PHP
header("Location:restaurantsList.php?msg=Restaurant 100#we updated successfully");
In restaurantsList.php when i'm trying to get request parameters then getting following array.
Array ( [msg] => Restaurant 100 )
Param value is getting truncated after '#'.
If a pass any normal text in param (without #) then i'm getting whole string as it is.
Is there any solution to get string as it is, even if string will be containing #?
You should URL encode special characters in your query string so your URL would look like:
header("Location:restaurantsList.php?msg=Restaurant%20100%23we%20updated%20successfully");
The # symbol is used for anchor links and therefore must be encoded if used as "data" in the URL. You can use the urlencode PHP function for this. The # symbol would translate to %23
in url # behave as hash tag that called Fragment identifier
Javascript: window.location.hash; // this is return sting that start with # tag
You will need to encode url component in server side code to keep message
header("Location:restaurantsList.php?msg=".
urlencode("Restaurant 100#we updated successfully"));
OR
header("Location:restaurantsList.php?msg=Restaurant 100%23we updated successfully");
You need to urlencode the string after msg=:
header("Location:restaurantsList.php?msg=" . urlencode("Restaurant 100#we updated successfully");

How do I pass one PHP url as a php variable in another URL without that syntax messing up the variables?

I have a download page that take arguments like the download URL, the download-counter data file url, and the page to return to after downloading.
It is arranged like so:
start.php?url=...&page=...&file=...
(Download url, redirect page, counter file)
The problem is, when the redirect page contains PHP arguments with ? and & symbols, the URL becomes a confusing mess for PHP to work with.
Example:
start.php?url=URLTEXT&page=page?test1=x&test2=xx&file=FILETEXT
What should happen:
url=URLTEXT
page=page?test1=x&test2=xx
file=FILETEXT
what happens:
url=URLTEXT
page=page?test1=x
test2=xx
file=FILETEXT
How could I substitute characters or somehow make these arguments pass correctly in php?
Thanks for any help you can give.
Well, I'm not sure how your "messed up" URL looks like. However the string after the "?" is called Query String, and you can decode/encode it with
urlencode($normalString); //will be encoded for use in URL
urldeocde($queryString); //will be decoded for "normal" use
EDIT:
Here is some short example:
echo "Encode for use in URL: ";
echo urlencode("this is a string & üäöllasdlk<bbb2");
echo "<br />";
echo "Decode to use it in your script: ";
echo urldecode($_SERVER['QUERY_STRING']);
Output:
Encode for use in URL:
this+is+a+string+%26+%C3%BC%C3%A4%C3%B6llasdlk%3Cbbb2
Decode to use it in your script: test=12
(Assuming you have a Querystring containing the variable test=12)
Just use htmlspecialchars function on your URL string:
http://php.net/manual/en/function.htmlspecialchars.php

PHP URL decode GET

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 )

What will decode "email%2540yahoo.com" value in PHP?

My variable with javascript looks like this:
var email = encodeURIComponent($('input[name=\'email\']').val())
email is clearly being encoded and is producing this when sent to server: email%2540yahoo.com
What function in PHP will decode this value properly?
I've tried using html_entity_decode
The correct url encoding for # is %40.
When a url, for example from an e-mail, with the encoded # character in it, is redirected using a rewrite rule, it will be rewritten as %2540 (the % is encoded as %25). If you keep rewriting / redirecting, you will replace % with %25 each time, ending up with %25252540 (or more 25, you get the picture).
For example clicking this:
http://example.org?email=info%40example.org
Will produce after a rewrite and redirect using a rewrite rule:
https://example.org?info%2540example.org
in the browser address bar, which does not correctly translate to info#example.org in php.
What function in PHP will decode this value properly?
You don't need to decode anything. $_GET["email"] and $_POST["email"] will work just fine. The encodeURIComponent function is used to properly url encode a url to avoid having invalid urls. If you have a valid url, PHP will successfully be able to read the parameters.
echo urldecode(urldecode('email%2540yahoo.com')); // email#yahoo.com
Try urldecode(<value_to_decode_here>);

Categories