通过$ _GET访问URL中的#字符

时间:2011-04-15 04:46:35

标签: javascript php url

我通过URL传递数据,然后使用PHP的$ _GET []函数访问它。如果URL中有某个“#”,那么GET请求似乎会截断字符串。例如,

  

http://example.com/yyy.php?version=0.88&value=ART:C_Sharp_%28programming_language%29@Multi-paradigm_programming_language@Influenced   D,F#,Java 5,Nemerle,Vala平台   通用语言基础结构许可   CLR专有的常用文件扩展名   .cs网站C Sharp编程   Wikibooks C#(发音为/siːːrp/   看清楚)是一种多范式   编程语言包含   命令式,陈述性,功能性,   通用的,面向对象的   (基于类),以组件为导向   编程学科。@ 10902

$_GET['value']返回:

  

ART:C_Sharp_(programming_language)@ Multi-paradigm_programming_language @受影响的D,F

有没有办法避免这种情况?我应该从发布到URL的值中删除所有#?

使用XMLHttpRequest()从客户端javascript代码访问URL位置。来自javascript功能encodeURIComponent()的编码字符串是,

  

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(发音%20%2F%CB%88si%CB%90%20%CB%88 %CA%83%C9%91rp%2F%20see%20sharp)%图20是%20A%20multi范%20programming%20language%20encompassing%20imperative%2C%20declarative%2C%20functional%2C%20generic%2C%20object为本% 20(基于类的)%2C%20于是%20component为本%20programming%20disciplines。%8681

'yyy.php'记录的解码输出:

  

ART:C_Sharp_%28programming_language%29 @多paradigm_programming_language @影响   D,F#,Java 5,Nemerle,Vala平台   通用语言基础结构许可   CLR专有的常用文件扩展名   .cs网站C Sharp编程   Wikibooks C#(发音为/ËsiË   ËʃÉ'rp/ see sharp)是一个   多范式编程语言   包含命令式,陈述性,   功能性,通用性,面向对象   (基于类),以组件为导向   编程学科。@ 8681

不使用encodeURIComponent()记录输出,应该是预期结果:

  

ART:C_Sharp_(programming_language)@多paradigm_programming_language @影响   D,F#,Java 5,Nemerle,Vala平台   通用语言基础结构许可   CLR专有的常用文件扩展名   .cs网站C Sharp编程   Wikibooks C#(发音为/siːːrp/   看清楚)是一种多范式   编程语言包含   命令式,陈述性,功能性,   通用的,面向对象的   (基于类),以组件为导向   编程学科。@ 8681

5 个答案:

答案 0 :(得分:5)

URL字符串中的#是片段标识符。在将字符串添加到URL之前,您需要urlencode()字符串。

您无法在yyy.php脚本中执行任何操作,因为PHP无法访问URL片段。

参考:http://en.wikipedia.org/wiki/Fragment_identifier

答案 1 :(得分:2)

尝试传递编码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 encompassing imperative, declarative, functional, generic, object-oriented (class-based), and component-oriented programming disciplines.@10902'));

你有js

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 encompassing imperative, declarative, functional, generic, object-oriented (class-based), and component-oriented programming disciplines.@10902');

<强>更新

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 encompassing imperative, declarative, functional, generic, object-oriented (class-based), and component-oriented programming disciplines.@8681');

enter image description here

答案 2 :(得分:1)

您需要转义该字符,因为它通常被浏览器用作hashtag以滚动到某些元素。

在PHP中,您使用查询字符串上的urlencode()函数。

答案 3 :(得分:1)

#之后的URL部分是片段,永远不会发送到服务器。它仅由用户代理处理。您需要使用JavaScript与其进行交互。

虽然,在你的例子中,你需要逃避它,所以它的意思是字面意思。正如其他人所指出的那样,urlencode()是完美的。

答案 4 :(得分:0)

您是否使用 PHP_URL_FRAGMENT 尝试 parse_url

echo parse_url($url, PHP_URL_FRAGMENT);