设为首页 收藏本站
查看: 920|回复: 0

[经验分享] PHP yii学习3

[复制链接]

尚未签到

发表于 2018-12-24 09:32:15 | 显示全部楼层 |阅读模式
  一,在Yii中使用session
  1,CHttpSession
  与原生态php5的session使用差别是,php5使用session_start();$_session['key'] = $value;
  在yii中,session已经被封装。
  To start the session, call open(); To complete and send out session data, call close(); To destroy the session, call destroy().
  If autoStart is set true, the session will be started automatically when the application component is initialized by the application.
Java代码  

  •   /***** 方式一、实例添加 *****/
  •   $session=new CHttpSession;
  •   $session->open();
  •   $value1=$session['name1'];
  •   /***** 方式二、直接调用应用添加 *****/
  •   Yii::app()->session->add('name','foobar');
  •   Yii::app()->session->add('name2','foobar');
  •   Yii::app()->session->add('name3','foobar');
  •   //或者
  •   $session = Yii::app()->session;
  •   $session['key'] = 'value';
  •   var_dump($session['key']);
  •   //遍历
  •   foreach($session as $name=>$value)
  一个实例
Java代码  

  •   $session = new CHttpSession;
  •   $session->open();
  •   $user_id = $this->user->id;
  •   $sessionKey = $user_id.'_is_sending';
  •   if(isset($session[$sessionKey])){
  •   $first_submit_time = $session[$sessionKey];
  •   $current_time      = time();
  •   if($current_time - $first_submit_time < 10){
  •   $session[$sessionKey] = $current_time;
  •   $this->response(array('status'=>1, 'msg'=>'不能在10秒钟内连续发送两次。'));
  •   }else{
  •   unset($session[$sessionKey]);//超过限制时间,释放session&quot;;
  •   }
  •   }
  •   //第一次点击确认按钮时执行
  •   if(!isset($session[$sessionKey])){
  •   $session[$sessionKey] = time();
  •   }
  •   var_dump($sessionKey);var_dump($session[$sessionKey]);exit();
  在index.php
  在$app->run();前
Java代码  

  •   $session = Yii::app()->session;
  •   session_set_save_handler(
  •   array($session,'openSession'),
  •   array($session,'closeSession'),
  •   array($session,'readSession'),
  •   array($session,'writeSession'),
  •   array($session,'destroySession'),
  •   array($session,'gcSession')
  •   );
  2,CDbHttpSession
  CDbHttpSession继承自 CHttpSession ,把session数据存储在数据库中(表名是YiiSession),
  The table name can be changed by setting sessionTableName. If the table does not exist, it will be automatically created if autoCreateSessionTable is set true.
  The following is the table structure:
  CREATE TABLE YiiSession
  (
  id CHAR(32) PRIMARY KEY,
  expire INTEGER,
  data TEXT
  )

  CDbHttpSession>  By default, it will use an SQLite3 database named 'session-YiiVersion.db' under the application runtime directory. You can also specify connectionID so that it makes use of a DB application component to access database.
  When using CDbHttpSession in a production server, we recommend you pre-create the session DB table and set autoCreateSessionTable to be false. This will greatly improve the performance. You may also create a DB index for the 'expire' column in the session table to further improve the performance.
Sql代码  

  •   CREATETABLE `YiiSession` (
  •   `id` char(32) NOTNULL,
  •   `expire` int(11) defaultNULL,
  •   `data` text,
  •   PRIMARYKEY  (`id`),
  •   KEY `expire` (`expire`)
  •   ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
  例,在../config/main.php中配置
Php代码  

  •   'session'=>array(
  •   'class' => 'CDbHttpSession',
  •   'autoStart' => true,
  •   'sessionTableName'=>'YiiSession',
  •   'autoCreateSessionTable'=> false,
  •   'connectionID'=>'db',
  •   ),
  二,在Yii中使用cookie
  Yii实现了一个cookie验证机制,可以防止cookie被修改。启用之后可以对cookie的值进行HMAC检查。
  Cookie验证在默认情况下是禁用的。如果你要启用它,可以编辑应用配置 中的组件中的CHttpRequest部分。
  一定要使用经过Yii验证过的cookie数据。使用Yii内置的cookies组件来进行cookie操作,不要使用$_COOKIES。
  实例:
Php代码  

  •   // 检索一个名为$name的cookie值
  •   $cookie=Yii::app()->request->cookies[$name];
  •   $value=$cookie->value;
  •   ......
  •   // 设置一个cookie
  •   $cookie=new CHttpCookie($name,$value);
  •   Yii::app()->request->cookies[$name]=$cookie;


运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-655124-1-1.html 上篇帖子: PHP 5.1 编译 下篇帖子: PHP数据类型
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表