m0g
m0g

Reputation: 969

Special Characters in URL query string

I have a situation where the user is able to enter any characters they want in a URL query string.

Example:

http://localhost/default.aspx?ID=XXXX

http://localhost/default.aspx?ID=&XXXX

http://localhost/default.aspx?ID=#XXXX

The web page must accept the ID parameter as it is no matter what the characters are. However certain special characters such as ampersand(&) and pound(#) creates problems. How can I accept them as is?

Upvotes: 7

Views: 15749

Answers (4)

Muhammad Akhtar
Muhammad Akhtar

Reputation: 52241

Encode your URL HttpServerUtility.UrlEncode Method (String)

Edit: following your comment, you want to get query String value of ID

 String id = Request.QueryString["ID"];

Upvotes: 1

Rocky Pulley
Rocky Pulley

Reputation: 23301

If the user is entering the query string, they must properly encode the query string first. If you are creating the query string yourself, such as from a form submission, you will need to use a URL encode method.

Upvotes: 4

bopjesvla
bopjesvla

Reputation: 763

Use

userinput = escape(userinput)

then, in PHP:

$userinput = urldecode($_GET['id'])

or in JS:

userinput = unescape(userinput)

Upvotes: 0

wanovak
wanovak

Reputation: 6127

This:

encodeURIComponent(uri)

Where uri is the component after the ?ID=

Upvotes: 8

Related Questions