Windows.name example analysis of Ajax cross domain solution under PHP

Time:2021-5-2

In this paper, we give an example of window. Name, which is a cross domain solution of Ajax in PHP. The details are as follows:

Principle core: the name attribute of the window object is a very special attribute. When the location of the window changes and then reloads, its name attribute can remain unchanged.

According to this principle, we can use iframe to load page B of other domains in page a, and use JavaScript to assign the data to window. Name in page B. after loading iframe of page a, page a will modify the address of iframe, change it into an address of the same domain, and then read out the value of window. Name.

There are two websites www.a.com and www.b.com We’re going to www.a.com/a.html Get under www.b.com/data.html Data.

We need three documents:

www.a.com Get the data and display it
www.b.com Provide data in data.html
www.a.com The proxy.html proxy file in the same domain as a.html is generally an empty HTML file.

www.b.com The data.html is as follows:

<!DOCTYPE html>
<html>
<head>
  <meta charset="UTF-8">
  <title>Insert title here</title>
</head>
<body>
  <script type="text/javascript">
    //Add the data that needs to be transferred, the size is generally 2m, and it can be as large as 32m in IE and firebox
    window.name = '[{"name":"test1"},{"name":"test2"}]';
  </script>
</body>
</html>

www.a.com The proxy.html under is as follows:

<!DOCTYPE html>
<html>
<head>
  <meta charset="UTF-8">
  <title>Insert title here</title>
</head>
<body>
  <!--  Empty HTML file -- >
</body>
</html>

www.a.com The a.html below is as follows:

<!DOCTYPE html>
<html>
<head>
  <meta charset="UTF-8">
  <title>Insert title here</title>
</head>
<body>
 
<!--  For reference www.b.com/data.html File -- >
<iframe src=""></iframe>
 
<!--  Display the acquired data -- >
<div></div>
 
<script type="text/javascript" src="./jquery.js"></script>
<script type="text/javascript">
var ifr = document.getElementById("iframe");
ifr.src = "http://www.b.com/data.html";
if (ifr.attachEvent) {
  ifr.attachEvent("onload", loadfunc);
} else {
  ifr.onload = loadfunc;
}
 
var state = 0;
function loadfunc() {
  if(state == 0) {
    state = 1;
    ifr.contentWindow.location = "http://www.a.com/proxy.html";
  } else {
    var data = ifr.contentWindow.name;
    $.each($.parseJSON(data), function(i, v) {
      $("#data").append(v.name);
    });
     
    //Destroy iframe to ensure safety
    ifr.contentWindow.document.write("");
    ifr.contentWindow.close();
    document.body.removeChild(ifr);
  }
}
</script>
</body>
</html>

More information about PHP can be found in the following topics: PHP + Ajax skills and application summary, PHP network programming skills summary, PHP string usage summary, PHP + MySQL database operation introductory course and PHP common database operation skills summary

I hope this article is helpful for PHP programming.

Recommended Today

Large scale distributed storage system: Principle Analysis and architecture practice.pdf

Focus on “Java back end technology stack” Reply to “interview” for full interview information Distributed storage system, which stores data in multiple independent devices. Traditional network storage system uses centralized storage server to store all data. Storage server becomes the bottleneck of system performance and the focus of reliability and security, which can not meet […]