tags:

views:

107

answers:

7

When a PHP site is requested like e.g. somesite.php?a=some text # some more

Currently it seems like $_REQUEST["a"] returns the string "some text ", how can I get "some text # some more". Are there other characters which get similar treatment by PHP?

A: 

The "#" character is special in the URL specification; it refers to a location on the page (named by an 'a' tag like: <a name='top'>this is the top of the page</a>). It, and everything after it, is not passed to the server (php).

Noon Silk
+1  A: 

You can't. The browser doesn't send the part of the url after the # to the server.

So, it's not PHP that removes that part of the URL, it never sees it.

Guffa
Damn, that I didn't think of that :-)Blaming our lovely PHP
Mustafa
+8  A: 

The hash (#) is a "Fragment identifier" (also informally known as an "anchor") and refers to a location within the page - and you're right, it doesn't get sent to the server.

It's the only URL character that behaves like this.

If you need to include a hash character in a URL, you need to encode it as %23

RichieHindle
+4  A: 
somesite.php?a=<?=urlencode( "some text # some more" )?>

Turns it into:

somesite.php?a=some+text+%23+some+more
Mark L
A: 

The # character, and any characters that follow it, specify and anchor. This is a point within the page that the browser will scroll to.

As far as I know, this is for the browser's use only, and is never transmitted to the server side - presumably because it means nothing to the server,

belugabob
+1  A: 

You can use urlencode() & urldecode() functions it will be %23 instead of # symbol

x2
+1  A: 

If you have to have it, you can write javascript to re-post it for you:

<script>

function checkHash(){
  var hash = window.location.hash;
  if(hash){
    // see if we've already included this
    var loc = window.location.pathname;
    if(!loc.indexOf('hashData=') >= 0){
      // hashData param not included
      window.location.href = loc + '&hashData=' + encodeURIComponent(hash) + hash;
    }
  }
};
checkHash();
</script>

There are some obvious issues with this (like it double-submits items). Note - if someone clicks on a hash link in the page, the code won't re-run, so you would need to monitor the hash for changes. You may or may not care about that case.

Steve Brewer
This solution comes in handy when you are using an ajax request to populate some sort of information anyway.
gnarf
In general, I would say "don't put stuff in the hash that the server needs".
Steve Brewer