跨域的几种方案

1. 跨域

1.1什么是跨域?

域下的文档或脚本试图去请求另一个域下的资源。

1.2为什么跨不过去?

由于浏览器的同源策略(域名、协议、端口均相同即为同源),浏览器不能执行其他网站的脚本。

https://github.com/index.html 调用  
https://github.com/server.php  非跨域

http://github.com/index.html 
调用   http://gitee.com/server.php 
跨域,主域不同

http://abc.github.com/index.html 
调用   http://def.github.com/server.php 
跨域,子域名不同

http://localhost:8080/index.html 调用  
http://localhost:5000/server.php  跨域,端口不同

https://localhost:8080index.html 调用  
http://localhost:5000/server.php  跨域,协议不同

localhost   调用 127.0.0.1 跨域

1.3跨域原理

通过各种方式,避开浏览器对JavaScript实施的安全限制(同源策略)。

2.跨域的方法

2.1 JSONP

2.1.1 实现原理

利用script 标签 src 属性中的链接可以访问跨域的 js 脚本的特性。
去创建一个script标签,script的src属性设置接口地址,接口参数必须要带一个自定义函数名,后台通过定义函数名去接受返回的数据。

2.1.2 实现代码

//原生的实现方式

//动态创建 script
let script = document.createElement('script');

// 设置回调函数
function getData(data) {
   
    console.log(data);
}
//设置 script 的 src 属性,并设置请求地址
script.src = 'https://so.csdn.net/so/search?q=%E8%B7%A8%E5%9F%9F&t=blog&u=CathyleeQ';

// 让 script 生效
document.body.appendChild(script);



2.1.3缺点

只支持GET方式的请求

2.2 document.domain + iframe跨域

2.2.1 实现原理

两个页面都通过js强制设置document.domain为基础主域,就实现了同域。

2.2.2 实现代码

主窗口

<iframe id="iframe" src="http://child.domain.com/b.html"></iframe>
<script>
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
class Session { //mysql的主机地址 const db_host = "localhost"; //需要第三方指定ip地址 //数据库用户名 const db_user = "root"; //需要第三方指定自己的用户名 //数据库密码 const db_pwd = ""; //需要第三方指定自己的库据库密码 //数据库 const db_name = "thinkphp"; //需要第三方指定数据库 //数据库表 const db_table = "tbl_session"; //需要第三方指定数据表 //mysql-handle private $db_handle; //session-lifetime private $lifeTime; function open($savePath, $sessName) { // get session-lifetime $this--->lifeTime = get_cfg_var("session.gc_maxlifetime"); // open database-connection $db_handle = @mysql_connect(self::db_host, self::db_user, self::db_pwd); $dbSel = @mysql_select_db(self::db_name, $db_handle); // return success if(!$db_handle || !$dbSel) return false; $this->db_handle = $db_handle; return true; } function close() { $this->gc(ini_get('session.gc_maxlifetime')); // close database-connection return @mysql_close($this->db_handle); } function read($sessID) { // fetch session-data $res = @mysql_query("SELECT session_data AS d FROM ".self::db_table." WHERE session_id = '$sessID' AND session_expires > ".time(), $this->db_handle); // return data or an empty string at failure if($row = @mysql_fetch_assoc($res)) return $row['d']; return ""; } function write($sessID, $sessData) { // new session-expire-time $newExp = time() + $this->lifeTime; // is a session with this id in the database? $res = @mysql_query("SELECT * FROM ".self::db_table." WHERE session_id = '$sessID'", $this->db_handle); // if yes, if(@mysql_num_rows($res)) { // ...update session-data @mysql_query("UPDATE ".self::db_table." SET session_expires = '$newExp', session_data = '$sessData' WHERE session_id = '$sessID'", $this->db_handle); // if something happened, return true if(@mysql_affected_rows($this->db_handle)) return true; } else // if no session-data was found, { // create a new row @mysql_query("INSERT INTO ".self::db_table." ( session_id, session_expires, session_data) VALUES( '$sessID', '$newExp', '$sessData')", $this->db_handle); // if row was created, return true if(@mysql_affected_rows($this->db_handle)) return true; } // an unknown error occured return false; }

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值