Vishnu Sharma
Vishnu Sharma

Reputation: 642

Get Url Parameter

I am having a simple problem. I need to get the parameter value from url.

URL is like this:

http://www.example.com/index.php?someUrl=http://www.example.com/folder/index.htm#<id=18025>>newwnd=false

I need to get the value of someUrl in php.

What i tried:

$_GET['someUrl'];

Output I am getting is http://www.example.com/folder/index.htm

Output need to get :

http://www.example.com/folder/index.htm#<id=18025>>newwnd=false

Note: I can't separate the URL after # into new parameter.

I hope i am able to clear the question.

EDIT: As per the valuable feedback, i have encoded the string before sending into as a URL parameter.

I did encoding by javascript function encodeURIComponent Now URL become like below:

http://www.example.com/index.php?someUrl=http%3A%2F%2Fexample.com%folder%2Ftallyhelp.htm%23%253Cid%3D18025%253E%253Enewwnd%3Dfalse

After decoding in php by urldecode function, i am getting the result http://www.example.com/folder/index.htm#>newwnd=false 'id' remove from the result.

Upvotes: 3

Views: 146

Answers (1)

Sharky
Sharky

Reputation: 6274

Your problem is that if your request to your page is this:

http://www.example.com/index.php?someUrl=http://www.example.com/folder/index.htm#<id=18025>>newwnd=false

then whatever is after the # is an anchor tag, that means whatever follows is not a parameter that you can $_GET, its just an anchor tag. so you need to encode that # (and some other strange characters that can cause more problems)

so if you manage to make your request to your page like this:

http://www.example.com/index.php?someUrl=http%3A%2F%2Fwww.example.com%2Ffolder%2Findex.htm%23%3Cid%3D18025%3E%3Enewwnd%3Dfalse

then you will be able to use $_GET['someUrl']; to get the value you want because the # will be encoded (among other characters too)

so how to make your request like this? simply use urlencode to encode your someUrl parameter.

see how it works

echo urlencode('http://www.example.com/folder/index.htm#<id=18025>>newwnd=false');

so after encoding your links that pointing to that page will be like this

<a href="http://www.example.com/index.php?someUrl=http%3A%2F%2Fwww.example.com%2Ffolder%2Findex.htm%23%3Cid%3D18025%3E%3Enewwnd%3Dfalse">my link</a>

as you can see the # will be encoded and that will stop it from messing with the request's url and you will be able to get this string via $_GET['someUrl'];

UPDATE for OP's new edit:

if you encoded with js's encodeURIComponent then use rawurldecode() in PHP. see How to decode the url in php where url is encoded with encodeURIComponent() which has another answer also that may help.

Upvotes: 3

Related Questions