Mustafa Temiz
Mustafa Temiz

Reputation: 326

# character in request string

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?

Upvotes: 1

Views: 769

Answers (7)

Noon Silk
Noon Silk

Reputation: 55082

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

Upvotes: 1

Steve Brewer
Steve Brewer

Reputation: 2100

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.

Upvotes: 1

x2.
x2.

Reputation: 9668

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

Upvotes: 1

belugabob
belugabob

Reputation: 4460

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,

Upvotes: 0

Mark L
Mark L

Reputation: 13435

somesite.php?a=<?=urlencode( "some text # some more" )?>

Turns it into:

somesite.php?a=some+text+%23+some+more

Upvotes: 4

RichieHindle
RichieHindle

Reputation: 281485

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

Upvotes: 8

Guffa
Guffa

Reputation: 700342

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.

Upvotes: 1

Related Questions