简体   繁体   English

PHP在JS函数调用中的简单引用

[英]Simple quote in PHP inside a JS function call

I have already read a lot of responses about this issue, and I didn't found the 100% correct. 我已经阅读了很多有关此问题的回复,但我没有发现100%正确。

The code I expect to create in php looks like this: 我希望在php中创建的代码如下所示:

<a href="#" onclick="a_js_function('moduleSee.php','sql_restriction','popup')">See</a>

a_js_function() is an internal javascript function, it receives some arguments and will be called simply like this a_js_function()是一个内部javascript函数,它接收一些参数,将像这样简单地被调用

$str = '<a href="#" onclick="';
$str .= " a_js_function('moduleSee.php','sql_restriction','popup')";
$str .='">See</a>';

But the sql_restriction argument contains a quote : 但是sql_restriction参数包含一个引号:

concat('000000',table_id)

And the NOT 100%-working solution is putting \\' instead of ' => this allows to SQL query doing properly but creates a javascript error that blocks part of the page. 而且,并非100%正常工作的解决方案是使用\\'代替' =>,这可以使SQL查询正常运行,但会产生JavaScript错误,从而阻止了页面的一部分。

$str = '<a href="#" onclick="';
$str .= " a_js_function('moduleSee.php','concat(\'000000\',table.id)','popup')";
$str .='">See</a>';

See what chrome says: 看看chrome怎么说:

Uncaught SyntaxError: Unexpected number -> point the numbers of received argument: concat('000000',table.id) 未捕获到的SyntaxError:意外的数字->指向接收到的参数的数目:concat('000000',table.id)

because with the \\' receive ' between the 000, the argument of the js function make error, not SQL. 因为在000之间使用\\' receive ' ,js函数的参数会产生错误,而不是SQL。 Avoiding this quotes in the number make that SQL query doesn't work 避免在数字中使用引号使SQL查询不起作用

putting ' or \\" or \\\\' or '' doesn't work too, addslashes() neither 放置'\\"\\\\'''也不起作用,既不添加addslashes()

Any ideas? 有任何想法吗?

The problem isn't how you are escaping your quotes - backslashes are correct. 问题不在于您如何转义引号-反斜杠是正确的。 What's happening though is that you aren't escaping enough, as each time the code passes through a language, the escaping is done and the next language won't see it anymore. 但是,发生的情况是您没有进行足够的转义,因为每次代码通过一种语言传递时,转义都已完成,而下一种语言将不再可见。 The code 编码

$str = '<a href="#" onclick="';
$str .= " a_js_function('moduleSee.php','concat(\'000000\',table.id)','popup')";
$str .='">See</a>';

is output from php as 从php输出为

<a href="#" onclick="
 a_js_function('moduleSee.php','concat('000000',table.id)','popup')
">See</a>

Javascript sees the single quotes in the concat and tries to end the string, and then gets confused by the 0s, which aren't a javascript keyword. Javascript在concat中看到单引号,并尝试结束字符串,然后被0(不是javascript关键字)所混淆。 You need the javascript to see the following 您需要javascript才能看到以下内容

<a href="#" onclick="
 a_js_function('moduleSee.php','concat(\'000000\',table.id)','popup')
">See</a>

which is accomplished by escaping both the \\ and ' characters in php, meaning your php needs to look like 这是通过转义php中的\\'字符来完成的,这意味着您的php需要看起来像

$str = '<a href="#" onclick="';
$str .= " a_js_function('moduleSee.php','concat(\\\'000000\\\',table.id)','popup')";
$str .='">See</a>';

I agree that you should be doing some processing of the input on your SQL server. 我同意您应该对SQL服务器上的输入进行一些处理。 You could correct the input at that stage. 您可以在该阶段更正输入。

You could also tries using escaped double-quotes instead in your PHP. 您也可以尝试在PHP中使用转义的双引号。 Eg: 例如:

$str = '<a href="#" onclick="';
$str .= " a_js_function('moduleSee.php','concat(\"000000\",table.id)','popup')";
$str .='">See</a>';

Javascript can concatenate without using a concat function, so this should work: Javascript可以在不使用concat函数的情况下进行连接,因此这应该可以工作:

a_js_function('moduleSee.php', '000000' + table.id, 'popup');

I'm not sure if your code is doing anything special to prevent the above code from working, but that works for me. 我不确定您的代码是否在做特殊的事情来阻止上述代码工作,但这对我有用。 I have table.id set to 25 , so the result comes out as: 00000025 我将table.id设置为25 ,所以结果显示为: 00000025

Thanks all for the answers, finally my boss solved changing the core of the CMS, filtering after use \\'00000\\' , use replace() to change \\' to " 感谢所有答案,最后我的老板解决了更改CMS核心的问题,在使用\\'00000 \\'之后进行过滤,使用replace()\\'更改为"

$_POST['fix'] = str_replace('\'','"', $_POST['fix'])); 

Query works good and no js errors 查询效果很好,没有js错误

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

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