简体   繁体   English

$ _POST,$ _GET unescape转义查询字符

[英]$_POST, $_GET unescape escaped query characters

I have a search field where users paste URLs (url is the search query). 我有一个搜索字段,用户粘贴网址(网址是搜索查询)。 Most urls work fine, but urls with escaped characters are getting 'unescaped'. 大多数网址都运行良好,但有字符转义的网址正在“未转义”。 For example : 例如 :

When I copy some url from Chrome browser I get escaped url: url = input query : ' http://website.com/search/my%20query ' 当我从Chrome浏览器中复制一些网址时,我获得了转义网址:url =输入查询:' http//wewesite.com/search/my%20query '

echo $_POST['q'] ~ result is ' http://website.com/search/my query' echo $ _POST ['q']〜结果是' http://website.com/search/my query'

The query is submitted by $.ajax : 该查询由$ .ajax提交:

var qurl = $("#url input[name=q]").val();
var queryString = "q="+qurl;

$.ajax({ 
url : "script.php", 
type: "POST", 
data: queryString,
.....
  1. How can I receive the query with the escaped characters ? 如何使用转义字符接收查询?
  2. Should I bother and process(escape the query of my query) the query url myself? 我应该打扰和处理(逃避我的查询的查询)查询网址吗?

Magic quotes you are off: 魔术引用你关闭:

php.ini

magic_quotes_gpc=Off
magic_quotes_runtime=Off
magic_quotes_sybase=Off

Your question is a bit short on detail so, unfortunately, there is a fair bit of speculation in this answer: 你的问题在细节上有点短,所以不幸的是,在这个答案中有一些猜测:

The % in %20 should be encoded as %25 (giving %2520 ) when the data is submitted. %%20应当被编码为%25 (给%2520当数据被提交)。

PHP will then decode it to give you %20 in $_POST . 然后,PHP将对其进行解码,以便在$_POST为您提供%20

I've never heard of PHP double decoding the data before. 我以前从未听说过PHP双重解码数据。 So it sounds like the problem is that whatever is making the request is failing to encode the data. 所以听起来问题是,无论是什么使得请求都无法对数据进行编码。 This is most likely caused by not using a regular form to submit it and using JavaScript instead. 这很可能是因为没有使用常规表单来提交它而是使用JavaScript。

The solution therefore, is to encode it properly in the JavaScript. 因此,解决方案是在JavaScript中正确编码。

For example: 例如:

data = "query=" + encodeURIComponent( document.getElementById('myInput').value );

Update in response to JavaScript being added to the question: 响应JavaScript添加到问题中的更新:

var qurl = $("#url input[name=q]").val();
var queryString = "q="+qurl;

My earlier speculation is confirmed. 我早先的猜测得到了证实。 You haven't encoded the data into the correct format for a query string. 您尚未将数据编码为查询字符串的正确格式。 Since you are using jQuery.ajax, you can use that to handle your encoding (by passing it an object of data) instead of building the query string manually (and then passing a string): 由于您使用的是jQuery.ajax,因此可以使用它来处理编码(通过传递数据对象),而不是手动构建查询字符串(然后传递字符串):

$.ajax({ 
url : "script.php", 
type: "POST", 
data: {
    q: $("#url input[name=q]").val()
},

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM