Extract variables from a URL with PHP

Hi,
I’m just wondering if it is possible to pull the following values (id and num) from this URL?

website_url/website_name/generatebuynowbtn.php#id=65&num=1

This url is beng dynamically generated and I cannot convert the # into a ? in order to use the $GET_ method

Any help would be greatly appreciated

Hmm…


 
//First we set the url variable here
$url = 'website_url/website_name/generatebuynowbtn.php#id=65&num=1';
 
//We search for a match to grab the id value and the num
//value in the url variable above
preg_match('#\\#id=([0-9]+)\\&num=([0-9]+)#',$url,$variables);
 
//We assign the values to their own variables and
//make sure they are a number with int()
$url_id = intval($variables[1]);
$url_num = intval($variables[2]);

Wow that was quick, can the url be dynamically grabbed?

http://www.php.net/parse_str

$vars = array();

parse_str(substr($str, strpos($str, '#') + 1), $vars);

Script works great, any ideas as to how to pull in the full url dynamically to populate $url?

Do you need to do anything with these variables, or are you just transfering the querystring to another URL ?

Yes I need these values to query my database

Is the url locally on your server or on another server?

Currently it is locally on my server i.e. localhost though when/if i get it working it will be hosted somewhere.

I’ve tried REQUEST_URI but it only gets as far as /websitename/generatebuynowbtn.php without the variables I need. Javascript will pull it with location.href but I dont know if I can add this to a php variable …

Bit lost really

Ok, I’m quite dissapointed that no one has given the actual answer here.

The # is a command to the browser, it’s not sent to the server.

For example:
This URL: http://www.sitepoint.com/forums/showthread.php?t=556061
The Top: http://www.sitepoint.com/forums/showthread.php?t=556061#top
The Bottom: http://www.sitepoint.com/forums/showthread.php?t=556061#bottom
Some post: http://www.sitepoint.com/forums/showthread.php?t=556061#post_message_3867033

So there is no way to do it? Can Javascript be used to pull the url into php?

Yeah, replace the # with ?

Alternatively, dont use the # in the first place.

I am using slideshowpro which is generating the #, this cannot be changed, if it was ? I would not have a problem getting the values in the first instance …

Any other suggestions?

If I used js to create a link with ? instead of # and pass this to the next page, i assume this would work but how could i make this seemless, is this the only way I could do it?

You could have it copy the hash as querystring variables, so both things work.

it’s not sent to the server

You know, I never realized that.

Thats because the # seemed to be required, otherwise ? would be a very simple solution that he would of tried already and from the ops recent post that is the case.

Dont know if it would work but have you looked into mod rewrite?

Yes, one use for # is bookmarks. When a bookmark is clicked it doesnt refresh the page.

Dont know if it would work but have you looked into mod rewrite?

It won’t.

As you said, it’s not refreshed. A request isn’t sent to the server.

Use JS to redirect after replaceing the # with ?

Yes, was messing around with some js code I put together:


<script type="text/javascript">
 curr_hash = location.hash;
 new_hash = curr_hash.replace("#", "?");
      window.location = 'http://yoursite/somescript.php' + new_hash;
</script>

Obviously you need to grab the rest of the url to pass along with it. But as is if you had this url:

http://www.somesite.com/test.php#test=1

Then ran the code above to forward to somescript it would then go to:

http://www.somesite.com/somescript.php?test=1

Edit:
Just tested it with putting #id=65&num=1 at the end of the url and it redirected as ?id=65&num=1

So I think that is the output you are looking for?

Thats great thanks for the help lads, I’ll have a play around with this tomorrow and see if I can get it all working …

Thanks again
Adam