Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Php to get value of hash from URL

How can I get a variable of a hash 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?

like image 401
Andelas Avatar asked Dec 13 '22 19:12

Andelas


2 Answers

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).

like image 168
Alec Avatar answered Jan 02 '23 15:01

Alec


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.

like image 36
Russell Dias Avatar answered Jan 02 '23 16:01

Russell Dias