views:

77

answers:

3

How can I get a variable of a hashtag in php.

I have a variable on page like this

catalog.php#album=2song=1

How can i get the album and song values and put them into PHP variables?

+5  A: 

You can't get this value with PHP because PHP processes things server-side, and the hash in the URL is client-side only and never gets sent to the server. JavaScript can get the hash though, using window.location.hash (and optionally call on a PHP script including this information, or add the data to the DOM).

Alec
+1  A: 

You can get the hash for example by using parse_url() , this returns an array with an item "fragment".

To extract the variables you could use some RegExp.

Dr.Molle
+1  A: 

Just adding onto @Alec's answer.

There is a parse_url() function:

Which can return fragment - after the hashmark #. However, in your case it will return all values after the hashmark:

Array
(
    [path] => catalog.php
    [fragment] => album=2song=1
)

As @NullUserException pointed out, unless you have the url beforehand this really is pointless. But, I feel its good to know nonetheless.

Russell Dias
Unless you have the URL beforehand, this is useless.
NullUserException
Indeed. Let me edit that into my answer too.
Russell Dias
Thank you. Sounds like I could work this idea into what I need.
scatteredbomb