URL GET variable has a necessary hash symbol

2019-08-04 04:39发布

I am creating a url link and one of the GET variables has a hash symbol in it. The webpage will not read any data after the hash mark. I cannot take it out for two reasons.

  1. The website database (not designed by me in any way) has hash symbols for various items of data. I have no authorization to edit the database. And I'm sure if I did other things would break.
  2. I cannot edit the webpage of the url. It was designed by someone else and again I don't have any authorization to edit it.

The url looks something like this

         www.example.com?datapoint1=abc&datapoint2=#def

where the #def is necessary as the webpage will search the database for this exact string. If I could edit the webpage php I could put the hash in when necessary, but as I said, I don't.

To explain a little further. The user collects data (in a Java app) and the data is put into a long url (like the above example but more complicated)and is automatically emailed to a specific user with this link. The second user clicks on the link and does whatever he/she has to do.

I think the only way is to edit the php or javascript of the webpage. Any ideas would be appreciated.

标签: php url
2条回答
放我归山
2楼-- · 2019-08-04 05:14

You'll have to encode the # as %23, so your URL would look like this:

www.example.com?datapoint1=abc&datapoint2=%23def

To make it easier, you could use PHP's built-in urlencode function: http://php.net/urlencode

查看更多
The star\"
3楼-- · 2019-08-04 05:28

You need to escape the hash in the url if you don't want it to become the hash part. The urlencoded character for a # is %23.

You can use the urlencode() (php.net doc) in php to escape values in php.

You might also like to know about http_build_query_string() which can generate the url query and encode the values properly from a key value array. Check out the php.net examples for more information.

If you can't access the PHP but can use JS (which is sub-optimal) you could make a small script that rewrites the url when it sees a hash is present (will only work if a hash is never present otherwise)

if(window.location.hash) {
    // Hash detected, lets rebuild the url
    window.location.href = window.location.href + '%23' + window.location.hash.slice(1);
}
查看更多
登录 后发表回答