Forum Moderators: coopster

Message Too Old, No Replies

Catching IDs in a URL

         

grallis

4:52 am on Feb 18, 2009 (gmt 0)

10+ Year Member



This may be a dumb question, and I'm positive I can solve it but in a very inelegant fashion so here goes:

I'm trying to capture a full URL including an ID, for eg: http://example.com/index.php#id

How do I get the pound info? I'm a fairly seasoned PHP coder but a simple solution to this escapes me. There must be an easy solution for this I'm missing or forgetting.

Thanks :)

[edited by: dreamcatcher at 7:48 am (utc) on Feb. 18, 2009]
[edit reason] use example.com. Thanks. [/edit]

phranque

7:00 am on Feb 18, 2009 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



the "pound info" refers to a fragment identifier and is only helpful for the user agent.
in other words, if you click on a link containing a fragment identifier and the url is for the current document, the server never sees a request for a new resource.

enigma1

6:10 pm on Feb 18, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You can use the parse_url php function. The fragment component of the result array will contain the link anchor.


if you click on a link containing a fragment identifier and the url is for the current document, the server never sees a request for a new resource.

That is not entirely accurate. For dynamic links you may have the same link but with different order of parameters.
eg:
http://www.example.com/index.php?param1=1&param2=2#top
vs
http://www.example.com/index.php?param2=2&param1=1#top
there will be a new request.

whoisgregg

6:20 pm on Feb 18, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The server never sees anything after the "#" fragment identifier (aka hash), and the PHP script won't be called if the only thing that changes is what's after the fragment.

If all you need is logging, you can have javascript ping a php script whenever the fragment changes. But if you're trying to have PHP produce different output based on the fragment, you're out of luck.

phranque

10:58 pm on Feb 18, 2009 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



and the url is for the current document
For dynamic links you may have the same link but with different order of parameters.

that is a different url, so if it's the "same document" you are serving you now have a canonicalization problem.

grallis

1:22 pm on Feb 21, 2009 (gmt 0)

10+ Year Member



Thanks all for your replies - very helpful

enigma1

10:08 am on Feb 22, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Phranque, not necessarily a canonicalization problem with the web app of the site, for instance, someone else can post the url externally with different ordering. So on the landing page 2 different urls may show (the landing and one in the document) in which case the server may send 2 different responses but it's effectively for the same page.

So a function like the parse_url function can retrieve the fragment part for the web app to get the visitor to the exact position within the page. This case will be handled by the server end instead of the browser. I believe it's one of the reasons for functions like the parse_url to return the fragment part in PHP.

whoisgregg

12:40 am on Feb 23, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If you do a quick test on your own server you'll see that the fragment is never sent to the server. Just make a page that dumps all the $_SERVER data:

<?php
echo '<pre>'; print_r($_SERVER); echo '</pre>';
?>

And then visit that page and test different fragments. You'll see that the fragment simply isn't part of the data that the server knows about.

The only way to get the current page's fragment to PHP is to have a separate Javascript request sent to the server. But, by that point, PHP has already outputted whatever it's going to output for the original page.

penders

12:27 pm on Feb 23, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



So a function like the parse_url function can retrieve the fragment part for the web app to get the visitor to the exact position within the page. This case will be handled by the server end instead of the browser. I believe it's one of the reasons for functions like the parse_url to return the fragment part in PHP.

The parse_url() [uk.php.net] is not just for parsing URLs that have been grabbed from the page request (which, as mentioned, does not contain a fragment identifier), but the (string)URL might have been pulled from a DB or created by some other means where there might actually be a fragment identifier, this can then be passed to the client/browser in the response.

enigma1

12:57 pm on Feb 23, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Yes you right, my mistake. It is not passed via the $_SERVER, but can only utilized by direct url input to the function.