Url decode php get

Url Decoding in PHP

Your string is also UTF-8 encoded. This will work:

echo utf8_decode(urldecode("Ant%C3%B4nio+Carlos+Jobim"));

Output: «Antônio Carlos Jobim».

PHP URL decode GET

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.

How to encode URL in JS and Decode in PHP?

Try this in your javascript code

window.location.href = 'products.php?price_range='+encodeURIComponent('-INFto2000,2001to5000');

You can access the decoded value in $_GET[‘price_range’]. $_GET variables are decoded by default in PHP.

Does PHP automatically do urldecode() on $_POST?

Yes, all the parameters you access via $_GET and $_POST are decoded.

The reason the urldecode() documentation doesn’t mention $_POST is because the POST data might not be URL-encoded in the first place. It depends on whether the POST data is submitted in application/x-www-form-urlencoded format or multipart/form-data format.

But all this is transparent to the application.

The documentation of $_GET does mention this explicitly, though.

The GET variables are passed through urldecode() .

How to properly URL encode a string in PHP?

For the URI query use urlencode / urldecode ; for anything else use rawurlencode / rawurldecode .

The difference between urlencode and rawurlencode is that

  • urlencode encodes according to application/x-www-form-urlencoded (space is encoded with + ) while
  • rawurlencode encodes according to the plain Percent-Encoding (space is encoded with %20 ).

Http cookies are headers that are transferred between the client (the browser), and the webserver.

When you use setcookie , what you are doing is instructing the PHP interpreter to inject a header in its response with this format:

That cookie will be stored by the browser, and returned by it in future requests (to the same host) in the Cookies request header like this:

Cookie:name=value;cookie2=value;cookie3=value

Normally, when transferring this you should urlencode any special characters. Let’s say that I wan to specify a cookie named «operator» with a value of «>», then I should emit this header:

Читайте также:  Compile and run java classes

When it says that the value is automatically urlencoded, is saying that you don’t have to worry about that. You can simply do:

And PHP will handle the urlencoding directly, producing the correct header.

On the server side, you’ll receive cookies in the $_COOKIES superglobal, and in the same way that happens with $_GET and $_POST , values will be automatically urldecoded for you. So if the client returns the previously set cookie %3E , you’ll see: > in your code.

If you use your browser inspector, you can see the relevant headers on any request-response. E.g.:

request (returning cookie)

request sending cookie

response (setting cookie)

response returning cookie

setrawcookie , does the same, but you have to urlencode on your own. From the docs:

setrawcookie() is exactly the same as setcookie() except that the cookie value will not be automatically urlencoded when sent to the browser.

More likely than not, you won’t have any reason to ever use setrawcookie directly.

php URL decode get ‘+’ from URL

Don’t run urldecode , the data in $_REQUEST is automatically decoded for you.

A plus sign, in a URL, is an encoded space. PHP decodes the hex value to a + automatically. Then, by running the result through urldecode , you are manually (and incorrectly) decoding the + to a .

PHP url decode do not work

if(!empty($_GET["pass"]))
$pass= $_GET["pass"];
>
http://www.test.com/index.htm?name1=value1&name2=value2
  • The GET method produces a long string that appears in your server logs, in the browser’s Location: box.
  • The GET method is restricted to send upto 1024 characters only.
  • Never use GET method if you have password or other sensitive information to be sent to the server.
  • GET can’t be used to send binary data, like images or word documents, to the server
  • The data sent by GET method can be accessed using QUERY_STRING environment variable.
  • The PHP provides $_GET associative array to access all the sent information using GET method.

Источник

How to do URL encoding and decoding in PHP

In this article, we will cover what URL encoding and decoding are, where they are applicable and why, and how to do it using PHP language.

ASCII (American Standard Code for Information Interchange) was the first character encoding standard used between computers and other electronic devices on the Internet.

It was designed in the 1960s, containing 128 characters. These characters include the numbers from 0 to 9, the upper and lower case alphabets from A to Z, and some special characters.

The character sets (encoding) used in modern computers, in HTML, and on the Internet, are all based on ASCII character set. For example, the default character set for HTML 4.01 is ISO-8859-1 while the default in HTML5 is UTF-8, which are both built on ASCII.

URLs can only be sent over the Internet using the ASCII character-set. Since oftentimes URLs contain non-ASCII characters (eg. semicolon, equal sign, space, etc), the URL has to be converted into a valid ASCII format.

URL encoding is a mechanism for translating/converting non-ASCII (unprintable or special) characters to a universally accepted format by web servers and browsers, and that can be transmitted over the Internet.

Читайте также:  Php http request parameters

URL encoding replaces the non-ASCII characters with a percent character «%» followed by two hexadecimal digits. These hexadecimal digits represent the numerical value of the characters being replaced.

URLs cannot contain spaces. Therefore, URL encoding replaces spaces with a plus «+» sign or with «%20» depending on the encoding method.

For instance, if you type any URL with some spaces in the browser address bar and hit enter, immediately that URL will be automatically converted to replace the space with «%20«.

Example

https://www.example.com/product?=black leather shoe

Will automatically be converted to:

URL encoding is mostly used in HTML form data submission via HTTP GET requests.

URL encoding is also known as percent-encoding.

As a developer, there will always be scenarios where you will be required to do URL encoding. There are two different ways to do encoding and decoding in PHP which includes:

  1. Using urlencode() and urldecode() functions
  2. Using rawurlencode() and rawurldecode() functions

1. Using urlencode() and urldecode() functions

Also referred to as «application/x-www-form-urlencoded» type, this method is preferable when sending the data submitted from the form to the URL query string.

It uses the PHP built-in functions urlencode() and urldecode() to encode and decode respectively.

This method replaces space with the plus «+» character.

It replaces the special characters with the «%hexcode» except for hyphen (), underscore (_), and dot (.).

Example: URL encoding

Example: URL decoding

URL decoding simply means reverting an encoded URL back to its original form. To do this, we used the urldecode() function.

https://www.example.com/product?=HP Elitebook Folio 9470m

2. Using rawurlencode() and rawurldecode() functions

This encoding method replaces the spaces within the URL with «%20» as opposed to the above method which uses the plus «+» character.

This encoding method is most preferable when creating URLs dynamically.

This method uses the RFC 3986 standard. Prior to PHP version 5.3.0, it used the RFC 1738 standard.

It uses the PHP built-in functions rawurlencode() and rawurldecode() to encode and decode respectively.

Example: URL encoding

Example: URL decoding

https://www.example.com/product?=HP Elitebook Folio 9470m

Источник

urldecode

Decodes any % ## encoding in the given string. Plus symbols (‘ + ‘) are decoded to a space character.

Parameters

Return Values

Returns the decoded string.

Examples

Example #1 urldecode() example

if ( $param ) printf ( «Value for parameter \»%s\» is \»%s\»
\n» , urldecode ( $param [ 0 ]), urldecode ( $param [ 1 ]));
>
>
?>

Notes

The superglobals $_GET and $_REQUEST are already decoded. Using urldecode() on an element in $_GET or $_REQUEST could have unexpected and dangerous results.

See Also

  • urlencode() — URL-encodes string
  • rawurlencode() — URL-encode according to RFC 3986
  • rawurldecode() — Decode URL-encoded strings
  • » RFC 3986

User Contributed Notes 20 notes

When the client send Get data, utf-8 character encoding have a tiny problem with the urlencode.
Consider the «º» character.
Some clients can send (as example)
foo.php?myvar=%BA
and another clients send
foo.php?myvar=%C2%BA (The «right» url encoding)

Читайте также:  Npx create react app my app template redux typescript

in this scenary, you assign the value into variable $x

$x = $_GET [ ‘myvar’ ];
?>

$x store: in the first case «�» (bad) and in the second case «º» (good)

To fix that, you can use this function:

function to_utf8 ( $string ) <
// From http://w3.org/International/questions/qa-forms-utf-8.html
if ( preg_match ( ‘%^(?:
[\x09\x0A\x0D\x20-\x7E] # ASCII
| [\xC2-\xDF][\x80-\xBF] # non-overlong 2-byte
| \xE0[\xA0-\xBF][\x80-\xBF] # excluding overlongs
| [\xE1-\xEC\xEE\xEF][\x80-\xBF] # straight 3-byte
| \xED[\x80-\x9F][\x80-\xBF] # excluding surrogates
| \xF0[\x90-\xBF][\x80-\xBF] # planes 1-3
| [\xF1-\xF3][\x80-\xBF] # planes 4-15
| \xF4[\x80-\x8F][\x80-\xBF] # plane 16
)*$%xs’ , $string ) ) <
return $string ;
> else <
return iconv ( ‘CP1252’ , ‘UTF-8’ , $string );
>
>
?>

and assign in this way:

$x = to_utf8 ( $_GET [ ‘myvar’ ] );
?>

$x store: in the first case «º» (good) and in the second case «º» (good)

Solve a lot of i18n problems.

Please fix the auto-urldecode of $_GET var in the next PHP version.

Источник

Using PHP urlencode and urldecode

Monty Shokeen

Monty Shokeen Last updated Aug 21, 2021

Every now and then you will have to pass data to a webpage or service using a URL, for example in a GET request. This is pretty easy, since URLs are basically just text strings, but things can sometimes get complicated. For example, some characters have a special meaning in URLs (like : ), and some characters aren’t allowed at all (like space). Sometimes you even need to encode a URL in another URL!

In this tutorial, you’ll learn why you need to encode or decode strings to be passed in URLs and how to use the built-in PHP functions to do so.

Why Encode and Decode Strings to URLs

Perhaps you want to pass some info as query parameters to a web service or another web page. Say, for example, you want to send the following data to a website as a query string:

key data
redirect https://code.tutsplus.com
author monty shokeen
page 2

That information would be encoded in the following query string:

https://www.example.com?redirect=https%3A%2F%2Fcode.tutsplus.com&author=monty%20shokeen&page=2

Notice that the special characters like : and / in the «redirect» URL have been encoded as %3A and %2F to avoid interfering with the structure of the overall URL. This is called escaping, and that’s where the encoding functions come in.

The server at example.com will receive that encoded information in the query string and will probably need to decode it later. That’s where URL decoding is important.

Encoding Strings With urlencode() and rawurlencode()

There are two different functions in PHP for encoding strings in URLs. These are called urlencode() and rawurlencode() . The major difference between these two is the set of characters they encode and how they handle spaces.

In the case of urlencode() , the function replaces all other non-alphanumeric characters except — , _ and . with a percent sign followed by two hex digits. Any spaces are replaced with the + character. On the other hand, the rawurlencode() function replaces all other non-alphanumeric characters except — , _ , . , and ~ with a percent sign followed by two hex digits. This function also replaces spaces with a percent followed by two hex digits: %20 .

The following example should clear things up a bit for you.

Источник

Оцените статью