think php框架 跨域,ThinkPHP

58e8d81c4a0d115f2a9f9f245fa77d87.png

...器名称时的一种报错行为的优化。第一步:在thinkphp框架中的Home/Comtroller中建一个EmptyController.class.php,其代码如下:

这篇文章主要介绍了ThinkPHP框架实现session跨域问题的解决方法,需要的朋友可以参考下

ThinkPHP的session跨域问题很多开发者都遇到过!

其实不管是ThinkPHP还是php本身,在解决session跨域问题的时候都需要设置session.cookie_domain。

在ThinkPHP里,需要修改配置文件conf/config.php

在第一行加上:ini_set('session.cookie_domain', ".domain.com");//跨域访问Session

经过总结,针对session跨域这一问题的解决方法主要有以下几种:

第一种情况:如果你目录下没有.htaccess这个文件,也就是没有采取url伪静态的话,那么,你在conf/config.php的第一行加上:ini_set('session.cookie_domain',".domain.com");//跨域访问Session

这种如果你开启了调试,可能用!但关闭了调试,可能就不管用了!

第二种情况:如果你目录下有.htaccess这个文件,那么你在根目录,index.php的第一行加入:<?php

ini_set('session.cookie_domain',".domain.com");//跨域访问Session

// 定义ThinkPHP框架路径

define('THINK_PATH', '/ThinkPHP/');

//定义项目名称和路径

define('APP_NAME', 'Www');

define(‘APP_PATH', '.');

// 加载框架入口文件

require(THINK_PATH."/ThinkPHP.php");

//实例化一个网站应用实例

App::run();

?>

这种方法不管开不开启调试都管用!

以上就是本篇文章的全部内容了,感谢大家的阅读,更多相关内容请关注点凡PHP技术博客!

相关推荐:

thinkPHP5.0框架整体架构总览【应用,模块,MVC,驱动,行为,命名空间等】

thinkPHP框架实现生成条形码的方法

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 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、付费专栏及课程。

余额充值