Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can hyphens be used in query string values?

Tags:

http

php

hyphen

My question is related to this one. except that my question is more sepcific as it is about whether a hyphen can be used in a query string parameter value.

I am parsing $_SERVER['QUERY_STRING'] with PHP. I would like to know whether it is syntactically correct to use hyphens in query string values such as in the following case, or whether hyphens must be escaped in the browser URL. What about underscores?

http://example.com/?q1=query-string-value-one&q2=query-string-value-two

According to this document hyphens should be OK in all standards-compliant browsers, but I wanted to double check.

Thanks.

like image 409
John Sonderson Avatar asked Sep 02 '15 09:09

John Sonderson


People also ask

What characters are allowed in query string?

The query component is a string of information to be interpreted by the resource. Within a query component, the characters ";", "/", "?", ":", "@", "&", "=", "+", ",", and "$" are reserved.

How can I include special characters in query strings?

In JavaScript you can use the encodeURI() function. ASP has the Server. URLEncode() function.

Can a query string have multiple question marks?

Yes, it is valid.

Should there be a slash before query string?

As a matter of modern spec, yes, it is permissible to skip the slash, contrary to what the accepted answer here claims.


2 Answers

You are talking about query string parameters which must be encoded using urlencode function:

This function is convenient when encoding a string to be used in a query part of a URL, as a convenient way to pass variables to the next page.

According to the documentation - does not require encoding.

like image 167
Salman A Avatar answered Sep 21 '22 19:09

Salman A


Yes

Hyphens can be used for query string parameter names

like image 29
Tomi Avatar answered Sep 20 '22 19:09

Tomi