Jump to content

Archived

This topic is now archived and is closed to further replies.

oskom

Need PHP to get hash from url

Recommended Posts

Hey all,

Is there some $_SERVER method, or anything else, that will allow PHP to get the hash from a url? In other words, I want to take a url like "www.mydomain.com/#1234" and have PHP get the hash and use it as a variable for other purposes.

 

Does this sound do-able?

Share this post


Link to post
Share on other sites

www.yourdomain.com/?hash=yourhasgoesherewoooooo

 

<?php
$Hash=$_GET['hash'];

if ($Var==md5($Hash) {
     // it matches
}
?>

 

Simple enough? :P

Share this post


Link to post
Share on other sites

Jabop, technically, the hash is the #foo after the URL, not a GET parameter.

Share this post


Link to post
Share on other sites

That's exactly what I'm trying to avoid. I don't want to pass it as a variable. This is for an all AJAX site where the unique url's(in theory) will be created in the hash. I can create a particular page from javascript with the hash, but I need to do it with PHP instead...Google searchable!!!! The only way I can make a unique url for each page(i.e. effect the address without actually flipping the page) is to hash it with javascript. I need to then be able to cut-n-paste that url with the hash into another browser window and have PHP build the page based on the hash being turned into a variable PHP can use.

 

I hope that wasn't too confusing. Can I achieve that with PHP?

 

Here's an idea: can I cookie the hash and pass that?

Share this post


Link to post
Share on other sites

Well it sounds like you're doing something more complex than I was thinking. I'm not too sure how you'd do that, to be honest. But stepping back a bit and thinking about it, I think a cookie may be the best way without passing it visibly?

Share this post


Link to post
Share on other sites

Try:

 

$hash = substr(strrchr('#', $_SERVER['REQUEST_URI']), 1);

 

Share this post


Link to post
Share on other sites

@Jabop: The point is to pass it visually so that they can jump to a certain part of an Ajax app.

Share this post


Link to post
Share on other sites

$hash = substr(strrchr('#', $_SERVER['REQUEST_URI']), 1);

 

DarkWater,

In testing, it doesn't appear that $_SERVER['REQUEST_URI'] passes the hash. Otherwise, yes, that would be my method. Unfortunately, PHP isn't getting a hash to sub-string.

 

hmmmmmm...

Share this post


Link to post
Share on other sites

Gimme a minute.  There's gotta be some way to get that hash. >_>  *Goes to the manual*

 

Edit: Nope, there's no way to directly get the hash, as it's only used client-side.  You're going to have to set it in a cookie or something.

Share this post


Link to post
Share on other sites

That was my thinking, which is fine. I would be nice to avoid that step, but if it works, hoooooray!!!

 

Share this post


Link to post
Share on other sites

I think window.location.hash might have the hash of the current page (in Javascript), so try working with that to set a cookie for it.

Share this post


Link to post
Share on other sites

as long as you are using ajax, grab the hash w/ ecma and load the rest of the page from a php script

Share this post


Link to post
Share on other sites

Javascript.  Okay, ECMAScript.  It's like the new version that has standards or something.

Share this post


Link to post
Share on other sites

I just checked it out online. Yes, it looks like javascript. Not sure what the difference is yet, though. However, in thinking about it some more, I don't know that the cookie is the solution either. You would have to establish the browser-side cookie with javascript first, then redirect the page(cookie in place) to a PHP page. With Google-searchability at stake, is the googlebot going to be able to take that step? Or does that small amount of javascript intervention break the deal?

Share this post


Link to post
Share on other sites

All modern browsers have to have their Javascript comply with ECMAScript standards, so they're essentially the same.  Don't worry about that.  And when you set the cookie, try doing it before the Ajax request and let the PHP handle it. >_>

Share this post


Link to post
Share on other sites

Actually, I was hoping to pass the cookie directly to php and have it build the initial page. After that point the ajax would do the rest.

 

Thanks for the info!

Share this post


Link to post
Share on other sites

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.