Hey,

I was wondering how to obtain a hash value and pass it to php.

URL:

http://www.mydomain.com/#id=110

All I want to grab is just the "110".

This is my javascript code:

<script>
	function getUrlVars()
{
    var vars = [], hash;
    var hashes = window.location.href.slice(window.location.href.indexOf('#') + 1).split('&');
    for(var i = 0; i < hashes.length; i++)
    {
        hash = hashes[i].split('=');
        vars.push(hash[0]);
        vars[hash[0]] = hash[1];
    }
    return vars;
}
	</script>

This is how I am going to call the value of the hash:

var first = getUrlVars()["id"]; // <-- name of hash

So my question is, first is this the proper javascript to grab the hash value?

Second how would I pass the hash value to a php variable? (the variable is going to be used to set a cookie)

ie:

$refer_id = $_GET['refer']; //<-- Grab this from javascript (the value of the hash)
	$days = 5;
	
	$date_of_expiry = time() + 60 * 60 * 24 * $days ;
    setcookie( "referrer", $refer_id, $date_of_expiry );

Thanks

It would be better if you use a query string instead of a variable. So that PHP can directly access it using $_GET. Failing that, you could make an ajax call to send a request to PHP file with the hash id as a parameter. Only reason you should be using this method would be if the URL pattern was beyond your control.

There might be a way to do this through .htaccess as well by rewriting the URL. Someone knowledgeable in that area could help you out.

Hey jomanlk

thanks for the advice, after reading here for a bit I see that passing variables from javascript to php can mostly onyl be done with a form or cookie.

So I will just use javascript to get hash value and set cookie and than retrieve it later with php.

thanks

Cjay,

So my question is, first is this the proper javascript to grab the hash value?

This is actually very simple. Javascript makes the hash string availabnle as location.hash . The js standard specifies that the leading # should be included but not all browsers honour this. Therefore use location.hash.split('#')[0] for the hash string without the leading #.

Second how would I pass the hash value to a php variable? (the variable is going to be used to set a cookie)

It depends what you are trying to do, but the easiest thing would be to write the cookie directly from Javascript. There's no point me desribing how to do this here as Javascript cookies are well documented elsewhere - here for example.

I believe that a cookie set in Javascript is identical in function to a server-originated cookie. Though I've never tried it, you should (at least in theory) be able to set a cookie value from Javascript and read it in PHP (encryption permitting).

Airshow

Cjay,

I have just re-read your question and I don't think you mean hash. I think you are trying to parse the "query" (aka "search") part of the url to separate out the parameters that appear in php in the $_GET array.

For this, you need to read my Query Parser code snippet.

Airshow

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.