最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Accessing # character in URL through $_GET - Stack Overflow

programmeradmin2浏览0评论

I am passing data through the URL and then accessing it using PHP's $_GET[] function. If the URL has a '#' somewhere in it, then the GET request seem to truncate the string. For example,

.php?version=0.88&value=ART:C_Sharp_%28programming_language%29@Multi-paradigm_programming_language@Influenced D, F#, Java 5, Nemerle, Vala Platform Common Language Infrastructure License CLR Proprietary Usual file extensions .cs Website C Sharp Programming at Wikibooks C# (pronounced /ˈsiː ˈʃɑrp/ see sharp) is a multi-paradigm programming language enpassing imperative, declarative, functional, generic, object-oriented (class-based), and ponent-oriented programming disciplines.@10902

$_GET['value'] returns:

ART:C_Sharp_(programming_language)@Multi-paradigm_programming_language@Influenced D, F

Is there a way to avoid this? Should I strip all #'s out of the values posted to the URL?

The URL location is accessed from the client side javascript code using XMLHttpRequest(). The encoded string from javascript functionality encodeURIComponent() is,

ART%3AC_Sharp_%2528programming_language%2529%40Multi-paradigm_programming_language%40Influenced%20D%2C%20F%23%2C%20Java%205%2C%20Nemerle%2C%20Vala%20Platform%20Common%20Language%20Infrastructure%20License%20CLR%20Proprietary%20Usual%20file%20extensions%20.cs%20Website%20C%20Sharp%20Programming%20at%20Wikibooks%20C%23%20(pronounced%20%2F%CB%88si%CB%90%20%CB%88%CA%83%C9%91rp%2F%20see%20sharp)%20is%20a%20multi-paradigm%20programming%20language%20enpassing%20imperative%2C%20declarative%2C%20functional%2C%20generic%2C%20object-oriented%20(class-based)%2C%20and%20ponent-oriented%20programming%20disciplines.%8681

decoded output logged by 'yyy.php':

ART:C_Sharp_%28programming_language%29@Multi-paradigm_programming_language@Influenced D, F#, Java 5, Nemerle, Vala Platform Common Language Infrastructure License CLR Proprietary Usual file extensions .cs Website C Sharp Programming at Wikibooks C# (pronounced /ˈsiË ËˆÊƒÉ‘rp/ see sharp) is a multi-paradigm programming language enpassing imperative, declarative, functional, generic, object-oriented (class-based), and ponent-oriented programming disciplines.@8681

Logged output without using encodeURIComponent() and what should be the expected result:

ART:C_Sharp_(programming_language)@Multi-paradigm_programming_language@Influenced D, F#, Java 5, Nemerle, Vala Platform Common Language Infrastructure License CLR Proprietary Usual file extensions .cs Website C Sharp Programming at Wikibooks C# (pronounced /ˈsiː ˈʃɑrp/ see sharp) is a multi-paradigm programming language enpassing imperative, declarative, functional, generic, object-oriented (class-based), and ponent-oriented programming disciplines.@8681

I am passing data through the URL and then accessing it using PHP's $_GET[] function. If the URL has a '#' somewhere in it, then the GET request seem to truncate the string. For example,

http://example./yyy.php?version=0.88&value=ART:C_Sharp_%28programming_language%29@Multi-paradigm_programming_language@Influenced D, F#, Java 5, Nemerle, Vala Platform Common Language Infrastructure License CLR Proprietary Usual file extensions .cs Website C Sharp Programming at Wikibooks C# (pronounced /ˈsiː ˈʃɑrp/ see sharp) is a multi-paradigm programming language enpassing imperative, declarative, functional, generic, object-oriented (class-based), and ponent-oriented programming disciplines.@10902

$_GET['value'] returns:

ART:C_Sharp_(programming_language)@Multi-paradigm_programming_language@Influenced D, F

Is there a way to avoid this? Should I strip all #'s out of the values posted to the URL?

The URL location is accessed from the client side javascript code using XMLHttpRequest(). The encoded string from javascript functionality encodeURIComponent() is,

ART%3AC_Sharp_%2528programming_language%2529%40Multi-paradigm_programming_language%40Influenced%20D%2C%20F%23%2C%20Java%205%2C%20Nemerle%2C%20Vala%20Platform%20Common%20Language%20Infrastructure%20License%20CLR%20Proprietary%20Usual%20file%20extensions%20.cs%20Website%20C%20Sharp%20Programming%20at%20Wikibooks%20C%23%20(pronounced%20%2F%CB%88si%CB%90%20%CB%88%CA%83%C9%91rp%2F%20see%20sharp)%20is%20a%20multi-paradigm%20programming%20language%20enpassing%20imperative%2C%20declarative%2C%20functional%2C%20generic%2C%20object-oriented%20(class-based)%2C%20and%20ponent-oriented%20programming%20disciplines.%8681

decoded output logged by 'yyy.php':

ART:C_Sharp_%28programming_language%29@Multi-paradigm_programming_language@Influenced D, F#, Java 5, Nemerle, Vala Platform Common Language Infrastructure License CLR Proprietary Usual file extensions .cs Website C Sharp Programming at Wikibooks C# (pronounced /ˈsiË ËˆÊƒÉ‘rp/ see sharp) is a multi-paradigm programming language enpassing imperative, declarative, functional, generic, object-oriented (class-based), and ponent-oriented programming disciplines.@8681

Logged output without using encodeURIComponent() and what should be the expected result:

ART:C_Sharp_(programming_language)@Multi-paradigm_programming_language@Influenced D, F#, Java 5, Nemerle, Vala Platform Common Language Infrastructure License CLR Proprietary Usual file extensions .cs Website C Sharp Programming at Wikibooks C# (pronounced /ˈsiː ˈʃɑrp/ see sharp) is a multi-paradigm programming language enpassing imperative, declarative, functional, generic, object-oriented (class-based), and ponent-oriented programming disciplines.@8681

Share Improve this question edited Nov 26, 2013 at 15:50 TRiG 10.7k9 gold badges61 silver badges111 bronze badges asked Apr 15, 2011 at 4:46 VJuneVJune 1,2155 gold badges17 silver badges27 bronze badges 3
  • can you put url up there, there _% in your url?? – S L Commented Apr 15, 2011 at 6:02
  • @experimentX: ok I edited.....now how do I fully recover the encoded/morphed characters? – VJune Commented Apr 15, 2011 at 6:34
  • all right, I think the best way seems to be to replace # with %23. I do not know what other characters like # might create troubles. Any ideas? – VJune Commented Apr 15, 2011 at 6:45
Add a ment  | 

5 Answers 5

Reset to default 5

A # in a URL string is a fragment identifier. You need to urlencode() the string before adding it to the URL.

You cannot do anything about it in your yyy.php script as PHP does not have access to URL fragments.

Ref: http://en.wikipedia/wiki/Fragment_identifier

Try passing parameters encoding urlencode().

header('Location: http://your_url?version=0.88&value='.urlencode('ART:C_Sharp_%28programming_language%29@Multi-paradigm_programming_language@Influenced D, F#, Java 5, Nemerle, Vala Platform Common Language Infrastructure License CLR Proprietary Usual file extensions .cs Website C Sharp Programming at Wikibooks C# (pronounced /ˈsiː ˈʃɑrp/ see sharp) is a multi-paradigm programming language enpassing imperative, declarative, functional, generic, object-oriented (class-based), and ponent-oriented programming disciplines.@10902'));

On js you have

window.location = 'index.php?value='+encodeURIComponent('ART:C_Sharp_%28programming_language%29@Multi-paradigm_programming_language@Influenced D, F#, Java 5, Nemerle, Vala Platform Common Language Infrastructure License CLR Proprietary Usual file extensions .cs Website C Sharp Programming at Wikibooks C# (pronounced /ˈsiː ˈʃɑrp/ see sharp) is a multi-paradigm programming language enpassing imperative, declarative, functional, generic, object-oriented (class-based), and ponent-oriented programming disciplines.@10902');

UPDATE

window.location = 'index.php?version=0.88&value='+encodeURIComponent('ART:C_Sharp_(programming_language)@Multi-paradigm_programming_language@Influenced D, F#, Java 5, Nemerle, Vala Platform Common Language Infrastructure License CLR Proprietary Usual file extensions .cs Website C Sharp Programming at Wikibooks C# (pronounced /ˈsiː ˈʃɑrp/ see sharp) is a multi-paradigm programming language enpassing imperative, declarative, functional, generic, object-oriented (class-based), and ponent-oriented programming disciplines.@8681');

You need to escape that character, as it's normally used by the browser as a hashtag to scroll to certain elements.

In PHP, you use the urlencode() function on the querystring.

The portion of the URL after the # is the fragment and is never sent to the server. It is handled by the user agent only. You'll need to use JavaScript to interact with it.

Though, in your example, you'll need to escape it so its meaning is taken literally. As others have pointed out, urlencode() is perfect for that.

did u try parse_url with PHP_URL_FRAGMENT ?

echo parse_url($url, PHP_URL_FRAGMENT);
发布评论

评论列表(0)

  1. 暂无评论