Unable to get a value from url.
localhost/ddd.php?udh=%05%00%03%6d%03%01
When I try to copy paste this url to address bar "localhost/ddd.php?udh=%05%00%03%6d%03%01" it converts to "http://localhost/ddd.php?udh=%05%00%03m%03%01"
Is there any explanation?
Also I am not able to get $_GET['udh'], it prints:
array (size=1)
'udh' => string '�m' (length=6)
From W3C Schools:
URL Encoding (Percent Encoding)
URLs can only be sent over the Internet using the ASCII character-set.
Since URLs often contain characters outside the ASCII set, the URL has to be converted into a valid ASCII format.
URL encoding replaces unsafe ASCII characters with a "%" followed by two hexadecimal digits.
URLs cannot contain spaces. URL encoding normally replaces a space with a plus (+) sign or with %20.
So when you put the string %05%00%03%6d%03%01 in the browser it automatically tries do decode it searching for %character, in your case it happens that %6d = m.
Not sure if you are using % as a separator but you could change to : or | and use explode to extract it (http://php.net/manual/en/function.explode.php)
May be you should encode your message using urlencode() and when accessing the message using $_GET you have to decode it using urldecode().
HTML
<a href ="localhost/ddd.php?udh=".<?php urlencode('message');?>></a>
PHP
$value = urldecode(isset($_GET['udh']));
Related
I have a string of Characters that is passed in a URL.
The string happens to contain a group of characters that is equivalent to an ASCII code.
When I try to use the string on the page using the $_GET command, it converts the part of the string that is equivalent to the ASCII code to the ASCII code instead of passing the actual string.
For example the URL contains a string Name='%bert%'. But when I echo out $_GET['Name'] I get '3/4rt%' instead of '%bert%'. How can I get the actual text?
You're not escaping your data properly.
If you want to use %bert% in a URL, you need to encode your % as %25, making your query string value %25bert%25.
% in a URL means that the next two characters are going to be some encoded entity, so if you want to use it literally, it must be encoded this way.
You can read more information here: http://www.blooberry.com/indexdot/html/topics/urlencoding.htm
try passing Name='%25bert%25' instead of Name='%bert%'.
Note: %25 acts as escape character for % is url query string!
When text with HTML tags inside it are inputted into a web-form (e.g <em>john</em>), how does it appear to the PHP processor?
I did some experiments with the example above and found that the form was sending the query string %3Cem%3Ejohn%3C%2Fem%3E is that how it would appear?
How could i fix this so that it appears as typed: <em>john</em>?
"%3Cem%3Ejohn%3C%2Fem%3E"
In the above statement % specifies whitespace.its browser dependent ,some browser replaces whitespace with '%' and some with '+'
if you want to decode than you can do that by php function rawurldecode();
the syntax of which is string rawurldecode ( string $str ).
Now about querystring:
when you are sending your data via GET method,the data which you send is formed into "name=value" form and its goes to the server.
example:
consider you have a form like this
<form method="get" action="a.php">
name:<input type="text" name="name">
</form>
and you insert name as rahul
then the query string will be "name=rahul" ,the value of which you can retreive in the server side using $_GET or $_REQUEST
You can use the rawurldecode function to decode the string.
rawurldecode("%3Cem%3Ejohn%3C%2Fem%3E")
A string in which all non-alphanumeric characters except -_. have been replaced with a percent (%) sign followed by two hex digits and spaces encoded as plus (+) signs. It is encoded the same way that the posted data from a WWW form is encoded, that is the same way as in application/x-www-form-urlencoded media type. This differs from the » RFC 3986 encoding (see rawurlencode()) in that for historical reasons, spaces are encoded as plus (+) signs. It is an encoded, To get an typed content, you can use urldecode()
$str ='<em>john</em>';
echo $Encoded = urlencode ( $str ); //OP %3Cem%3Ejohn%3C%2Fem%3E
echo urldecode ( $Encoded );
I've done some tests, and it appears that when I test this:
http://127.0.0.1/test.php?x={some non-english string}
http://127.0.0.1/test.php?x=الapple
By examining the output of:
echo bin2hex($_GET["x"]);
In Firefox & Chrome, I get the UTF-8 representation of the string d8a7d9846170706c65.
$_GET['x'] variable. In IE, I get 3f3f6170706c65. which is wrong
And I know that PHP does not change encoding, and only sees the string as a byte array.
The question is:
Is this controlled by the browser used?
Is it reliable to always assume the input it in UTF-8 encoding?
Is there a way to manage what encoding the browser sends to the server? across all browsers?
There is a difference from where the request originated.
If it’s from a user’s input, e.g., entering the URL into the browser’s address field, most browsers follow the suggestion in RFC 3986 and use UTF-8 as encoding:
When a new URI scheme defines a component that represents textual
data consisting of characters from the Universal Character Set [UCS],
the data should first be encoded as octets according to the UTF-8
character encoding [STD63]; […]
Although this is intended for new URI schemes and HTTP is quite old.
However, if the URL was embedded in a document, e.g., as a link or form action, the document’s encoding is used unless the data was already encoded using the URL encoding. And in case the data has a wrong encoding, invalid sequences may be replaces with certain characters that should denote those invalid sequences like the � (U+FFFD) in Unicode does. Similarly, the invalid encoded characters ل and ا may have been replaces by ?, which has the code point 0x3F in ASCII.
I think it should come down to how urldecode (http://www.php.net/manual/en/function.urldecode.php) interprets it, since the $_GET variables are all passed through that function (see http://php.net/manual/en/reserved.variables.get.php)
EDIT
To encode the characters to UTF-8 for use in a URL from the client side, you can use the encodeURI in JavaScript.
For the example you gave, you can do encodeURI('الapple');, which should return "%D8%A7%D9%84apple"
Giving this to PHP's urldecode function (as it would be automatically) returns the original string, with the following hex output;
echo bin2hex(urldecode("%D8%A7%D9%84apple")); //outputs d8a7d9846170706c65
yes it's possible !
To encode the URL :
<?php
$url = "http://127.0.0.1/test.php?x=".urlencode("some non-english string");
?>
To decode the URL :
<?php
$url = urldecode($_GET["x"]);
?>
I need to get values in the url as it is
ex:
http://www.example.com/index?url=1+LY2ePh1pjX4tjZ4+GS393Y2pjd16Cbq63T3tbfzMzd16CarA==
but vriable url give me value of "1 LY2ePh1pjX4tjZ4 GS393Y2pjd16Cbq63T3tbfzMzd16CarA=="
Even though i have expected "1+LY2ePh1pjX4tjZ4+GS393Y2pjd16Cbq63T3tbfzMzd16CarA=="
any one can help me for this or know the reason
You see, you need to encode certain characters if you need to send them in a URL. For further references, I suggest you should read this Page. It seems that the URL you are getting isn't being encoded properly. If the URL is coming from your site, then I would suggest you to encode it properly.
In PHP, there is a function called urlencode, which may help you with this task.
A short explanation
URLs can only be sent over internet using ASCII character set.If you want to send characters which is outside this set, you need to encode it.URL encoding replaces unsafe ASCII characters with % followed by two hexadecimal digits corresponding to the character values in the ISO-8859-1 character-set.
The client sending the request apparently isn't URL encoding the value correctly. You can re-encode it after it's being decoded like this:
urlencode($_GET["url"])
IT convert %2B to space
The parameter you sent is wrong, it should have been encoded like so..
<?php
echo '<a href="http://www.example.com/index?url=', urlencode('1+LY2ePh1pjX4tjZ4+GS393Y2pjd16Cbq63T3tbfzMzd16CarA=='), '">';
?>
i have added encoding correctly now,It convert == correctly, but + sign encode to %2B correctly but in decode process it convert to space
As it seems that you’re having a Base-64 value there: You can use the URL safe alphabet for Base-64 that uses - and _ instead of + and / respectively:
$base64 = "1+LY2ePh1pjX4tjZ4+GS393Y2pjd16Cbq63T3tbfzMzd16CarA==";
// plain Base-64 to URL safe Base-64
$base64_safe = strtr($base64, '+/', '-_');
// URL safe Base-64 to plain Base-64
$base64 = strtr($base64_safe, '-_', '+/');
And if you know the length of the data, you can also omit the = padding:
rtrim($base64, '=')
I am attempting to open a page with window.open and it's not working. The path shown is like xyz/a%20b%20c%20.pdf, but it is supposed to be xyz/abc.pdf. If I remove the % and 20 manually, it works, how can I remove these characters using PHP?
Use urldecode:
(PHP 4, PHP 5)
urldecode — Decodes URL-encoded string
Description
string urldecode ( string $str )
Decodes any %## encoding in the given string. Plus symbols ('+') are decoded to a space character.
Example
echo urldecode('xyz/a%20b%20c%20.pdf');
This is known as URL Encoding. You need to decode the string. If you are using jQuery you should check out the URL Encode plug in.
You need to urldecode (as stated above).
However, you say that you can remove the %20 and it will work. I would say you need them, they decode to spaces. Check it out using this online url decoder:
http://www.convertstring.com/EncodeDecode/UrlDecode
it decodes to:
xyz/a b c .pdf
not
xyz/abc.pdf