tags:

views:

40

answers:

3

Hi guys I have a url like http://localhost.com/api/authenticateuser?user=demo&pass=demo#123

But when i use $_GET['pass'] i am only able to get value of $_GET['pass'] is "demo" not "demo#123"

is there any way to get parameter value as "demo#123"

Thanks

+2  A: 

You have to urlencode your url, in order to escape special / forbidden chars.

http://php.net/manual/en/function.urlencode.php

Guillaume Lebourgeois
To elaborate, # is a special character in urls (pointing to an anchor within the page). To use it as a part of a query string, it needs to be encoded, which will look like %23 in the URL, but # in $_GET.
grossvogel
Firstly, I believe one should use rawurlencode instread of urlencode, as urlencode's way of encoding spaces (as '+') is almost deprecated and is kept only for legacy purposes. Secondly, he won't recieve the url server-side, will he? A '#' symbol indicates a fragment and nothing after it (and including it) is returned to the server. This answer will not work as far as I know.
Stephen
@Stephen: encoding spaces as + is part of the `application/x-www-form-urlencoded` content type, which is what HTML uses to transmit form data through query strings and the `POST` message body when no file inputs are present. This media type is still the default for forms in HTML5; as such, it is **not** deprecated.
R. Bemrose
@Stephen urlencoding a # means it is no longer a #, but %23. And it does get sent to the server, where it automatically gets decoded. This answer will definitely work, if the OP is constructing the url on the server.
Maerlyn
`rawurlencode` is for use on things that will be used in the HTTP path. For example: `http://example.com/file+name.php` is invalid, as HTTP doesn't do form url encoding; `http://example.com/file%20name.php` is correct. But in query strings, either works: `http://example.com/?this=two+words` is correct, as is `http://example.com/?this=two%20words`
R. Bemrose
@Maerlyn I'm not very experienced with PHP, but where exactly? If I navigate to `http://localhost/index.html?Parameter=Bob#The%20Dinosaur`, I cannot find a single PHP-side server variable that has the full URI in it. `$_GET[Parameter]` has the value `Bob` of course, and `$_SERVER[REQUEST_URI]` is just `/index.html?Parameter=Bob`. I do admit being wrong about urlencode - I am now feeling rather silly about that one. Thanks for the correction @Bemrose.
Stephen
@Stephen if you urlencode that hashmark, it becomes %23, and then it will get sent to the server. You used a literal hashmark instead of it's urlencoded form.
Maerlyn
@Maerlyn But how are you meant to urlencode the URL that is being passed? It's most likely being passed from a form being submitted, so php doesn't get a look in - hence why the Javascript answers below. Unless I'm missing something - all our php processing here at work only begins once we've recieved a request from the web-page.
Stephen
@Stephen If he is generating such an url while he's generating the page, our work starts even before the request you meant is being started. If it's passed from a form, nothing to be done, browsers urlencode automatically. If it's via ajax, depending on his code he may need to use the javascript encoding function. The question is a bit underspecified.
Maerlyn
@Maerlyn Well I'll be damned. Today I learn how little I know about website coding. Never even occured to me to think that the browser would urlencode form data automatically XD. So, I suppose the only unavoidable solution is directly typing in that URI, but at that point the solution is assumedly to slap the person who did it...
Stephen
@Stephen we're all here to learn, after all
Maerlyn
+1  A: 

The part after the # is called the fragment identifier, and it is not sent to the server. If you want to send it that way, you need to use encodeURIComponent.

Maerlyn
I believe it's officially called the "Fragment Identifier" <http://www.w3.org/TR/REC-html40/intro/intro.html#h-2.1.2>. The "#" character itself is called a hash (in the UK at least).
w3d
Thank you, I've edited my answer. I called it hash since you can access it via `window.location.hash`.
Maerlyn
A: 

You need to ensure the URL is properly escaped - see URL Escape Codes for a reference. For example, # is supposed to be escaped to %23.

There are many ways to do this, including using JavaScript function encodeURIComponent (for dynamically generated links).

Justin Ethier