简体   繁体   English

PHP与javascript通信

[英]PHP to communicate with javascript

Static Page A has a form with an action submitting to a Authorization page B which is a dynamic page. 静态页面A具有一个表单,该表单的操作提交给作为动态页面的授权页面 B。 After authorization, B will redirect to a callback url C which is passed to B by A. 授权后,B将重定向到由A传递给B的回调URLC

Besides redirecting to page C, B also post some parameters indicated the auth states. 除了重定向到页面C,B还发布了一些指示auth状态的参数。 uin is a most important parameter that will be used in the content of page C namely the scripts. uin是最重要的参数,将在C页的内容(即脚本)中使用。 The scripts need uin to send Ajax request later. 脚本需要uin才能稍后发送Ajax请求。 Question is how can I pass uin to the static page C? 问题是如何将uin传递给静态页面C?

A quick and dirty idea I got is to wrap the static page C with a PHP file, and output the data in a hidden div for example: 我得到的一个快速而肮脏的想法是用一个PHP文件包装静态页面C,然后将数据输出到一个隐藏的div ,例如:

<?php
$html = file_get_contents("callback.html")
$div = "<div stype='display:none' uin={$_POST['uin']}></div>"
//add this div to $html and print it, need a little more work to figure out how to do this
?>

Is there a better way of doing this , because this is sort of 'idiot' I think... 有没有更好的方法可以做到这一点,因为我认为这是一种“白痴”。

Your code: (with stype typo fixed) 您的代码:(固定了stype错字)

$div = "<div style=\"display:none\" uin={$_POST['uin']}></div>";

Looking at this code, the biggest problem I can see with it is that you're outputting a $_POST value without doing any escaping on it. 看这段代码,我可以看到的最大问题是您正在输出$_POST值而没有对其进行任何转义。

This a potential security threat; 这是潜在的安全威胁; consider what would happen if someone provided a form that posted to your site, with the uin value set to a string of HTML code, starting with > to close the div . 考虑如果有人提供了将表单的uin值设置为HTML代码字符串(以>开头以关闭div将表单发布到您的网站会发生什么情况。 Their code would appear in your site, exactly as if you'd put it there. 他们的代码将出现在您的网站中,就像您将其放在那里一样。 With a careful bit of styling, they could use this make your site look and behave however they want. 经过精心的样式设置,他们可以使用此方法使您的网站外观和行为随心所欲。 Not great. 不是很好。

You can fix that by using wrapping the $_POST variable in html_entities() so it is properly escaped when it is output to the site. 您可以通过将$_POST变量包装在html_entities()来解决此问题,以便在将其输出到站点时正确地对其进行转义。

Or, in this case, since it is (or appears to be) a numeric ID value, you could simply cast it as an int to ensure that it contains no unwanted data, no matter what the actual post data contains: 或者,在这种情况下,由于它是(或似乎是)数字ID值,因此您可以将其简单地转换为int值,以确保它不包含不需要的数据,无论实际的过帐数据包含什么:

$uin = (int)$_POST['uin'];

...and then use $uin in the output rather than the raw post data. ...然后在输出中使用$uin而不是原始的post数据。

The second point I'd make is one of validity. 我要说的第二点是正确性之一。 uin is not a valid HTML attribute. uin不是有效的HTML属性。 It may work, but it's not valid. 它可能有效,但是无效。 The correct standards-compliant way to do custom attributes in HTML is to use a data attribute, like so: 在HTML中进行自定义属性的正确的符合标准的方法是使用数据属性,如下所示:

$div = "<div style=\"display:none\" data-uin={$uin}></div>";

... ie the names of all custom attributes should start with data- ...即所有自定义属性的名称均应以data-开头

This is recommended because it allows you to have custom attributes with the same name as real attributes without risking any problems. 推荐这样做是因为它允许您使用与真实属性同名的自定义属性,而不会冒任何问题的风险。 eg you could have data-style , without colliding with the real style attribute. 例如,您可以具有data-style ,而不会与real style属性冲突。

It also means that the HTML spec can have new attributes added to it without risking clashes with other people's code. 这也意味着HTML规范可以添加新的属性,而不会冒与他人代码冲突的风险。 eg if a future version of HTML includes a uin attribute that future browsers use to do something clever with the element, it would cause problems with your code. 例如,如果将来的HTML版本包含uin属性,以后的浏览器将使用uin属性对元素进行巧妙处理,则可能会导致代码出现问题。 Those problems would not happen if you use data-uin . 如果使用data-uin不会发生这些问题。 (okay, so uin is an unlikely name for a new standard HTML attribute, but the point stands) (好的,因此uin对于新的标准HTML属性来说是不太可能的名称,但这很重要)

You have an syntax errors in your php code you need to mask the quotes around your inline style and you missed to add some colons: 您的php代码中存在语法错误,您需要屏蔽内联样式周围的引号,而您错过了添加一些冒号的操作:

<?php
$html = file_get_contents("callback.html");
$div = "<div style=\"display:none\" uin={$_POST['uin']}></div>";
//add this div to $html and print it, need a little more work to figure out how to do this
echo($html); // print variable $html
echo($div); // print variable $div
?>

Perhaps you should store parameters from page B in user session. 也许您应该在用户会话中存储页面B中的参数。 Than on page C you can use these parameters (after calling session_start() before anything is outputted to the browser). 比在C页上,您可以使用这些参数(在调用session_start()之后,什么都没有输出到浏览器)。 Also, if you are using javascript, consider placing uin in javascript variable instead of html div. 另外,如果您使用的是javascript,请考虑将uin放在javascript变量而不是html div中。 Something like <script type="text/javascript">var uin = 123; </script> 类似于<script type="text/javascript">var uin = 123; </script> <script type="text/javascript">var uin = 123; </script> . <script type="text/javascript">var uin = 123; </script>

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

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