CSRF token 无法被验证.

我使用的是mongodb+ yii1.1

What is CSRF, please see the details here.  http://en.wikipedia.org/wiki/Cross-site_request_forgery

In Yii, how to start the CSRF authorization? It is very easy to do that.

Just add this to main.php

'components'=>array(
    'request'=>array(
      'enableCsrfValidation'=>true,
    ),
),  

And then, do something else to send a request to the server, you have to provide the  YII_CSRF_TOKEN ( the browser will do for us when click a link), otherwise, you will get this message

The CSRF token could not be verified.

when you post a form, if you do not use CActiveForm or its children, you have to provide a hidden field to store the YII_CSRF_TOKEN.

<input type="hidden" name="YII_CSRF_TOKEN" value="<?php echo Yii::app()->request->csrfToken; ?>" />  

If you use CActiveForm or its children, you just use the same code no matter you set enableCsrfValidation to true or false.

如果你使用CActiveForm类或其继承的子类进行创建表单时,在POST提交数据的时候会有一个$_POST['YII_CSRF_TOKEN'] 字段,让程序识别这是程序合法的提交数据。

<?php $form=$this->beginWidget('CActiveForm'); ?>  
<--表单信息--> <?php $this->endWidget(); ?>

顺便补充一下:在使用CActiveForm时,可以自定义设置参数,

<?php $form = $this->beginWidget('CActiveForm', array(
  'id' => 'register-form',
  'action' => ['test/register'],
));?>

通过action选项设置当前表单数据的提交处理url. 详细参数可以参考参考手册

感谢原作者:http://www.cnblogs.com/davidhhuan/archive/2011/01/19/1939253.html

友情参考:http://blog.csdn.net/zm2714/article/details/8814663

CSRF token 无法被验证. ----Yii连接数据库后数据库错误日志报错的更多相关文章

  1. FastDFS整合nginx后,nginx一直报错

    FastDFS整合nginx后,nginx一直报错: 报错内容: [2018-06-11 09:41:21] ERROR - file: ../common/fdfs_http_shared.c, l ...

  2. 普通的jdbc事务在插入数据后 下面的代码报错时 数据不会回滚 但是 spring的事务会回滚

    普通的jdbc事务在插入数据后 下面的代码报错时 数据不会回滚 但是 spring的事务会回滚

  3. Python3安装Celery模块后执行Celery命令报错

    1 Python3安装Celery模块后执行Celery命令报错 pip3 install celery # 安装正常,但是执行celery 命令的时候提示没有_ssl模块什么的 手动在Python解 ...

  4. 【docker】【redis】2.docker上设置redis集群---Redis Cluster部署【集群服务】【解决在docker中redis启动后,状态为Restarting,日志报错:Configured to not listen anywhere, exiting.问题】【Waiting for the cluster to join...问题】

    参考地址:https://www.cnblogs.com/zhoujinyi/p/6477133.html https://www.cnblogs.com/cxbhakim/p/9151720.htm ...

  5. Hadoop0.20.203.0在关机重启后,namenode启动报错(/dfs/name is in an inconsistent state)

    Hadoop0.20.203.0在关机重启后,namenode启动报错: 2011-10-21 05:22:20,504 INFO org.apache.hadoop.hdfs.server.comm ...

  6. AppiumLibrary库倒入后显示红色,日志报错:ImportError: cannot import name 'InvalidArgumentException'

    AppiumLibrary安装后,robotframe worke 倒入后一直显示红色,查看日志报错:ImportError: cannot import name 'InvalidArgumentE ...

  7. Springboot项目 配置数据库连接属性后,启动项目报错

    Springboot项目 配置数据库连接属性后,启动项目报错,错误如下: 错误原因分析: 1.连接信息配置错误 当使用properties为配置文件时,如图所示,上面的 spring.datasour ...

  8. Sql Server 2008卸载后再次安装一直报错

    sql server 2008卸载之后再次安装一直报错问题. 第一:由于上一次的卸载不干净,可参照百度完全卸载sql server2008 的方式 1. 用WindowsInstaller删除所有与S ...

  9. 配置recovery_min_apply_delay后重启standby节点报错:psql: FATAL: the database system is starting up

    环境: pg版本:PostgreSQL 9.4.4 on x86_64 系统版本:CentOS release 6.6 linux内核版本:2.6.32-504.8.1.el6.x86_64 今天测试 ...

随机推荐

  1. 浅析JS中的模块规范(CommonJS,AMD,CMD)////////////////////////zzzzzz

    浅析JS中的模块规范(CommonJS,AMD,CMD)   如果你听过js模块化这个东西,那么你就应该听过或CommonJS或AMD甚至是CMD这些规范咯,我也听过,但之前也真的是听听而已.     ...

  2. [LeeCode]Power of Two

    Given an integer, write a function to determine if it is a power of two. My initial code: class Solu ...

  3. table 边框显示

    .td{border:solid #add9c0; border-width:0px 1px 1px 0px;}.table{border:solid #add9c0; border-width:1p ...

  4. 如何写一个HttpClient[1]——URI的处理

    如何写一个HttpClient[1]--URI的处理 在翻阅apache的http client的代码的时候,看到org.apache.http.client.utils.URIBuilder.jav ...

  5. Make cnblogs mobile Compatible

    Introduction 博客园的许多页面都是只支持PC的,没有手机端的,也没有手机端的app.每次查看都有放大了才能看清楚字体,手指和游泳似的,左右开弓,很不方便.还有上次我修改了theme之后,手 ...

  6. CSS3 笔记五(Buttons)

    Some examples 1> Input width animation Search: Code <!DOCTYPE html> <html> <head&g ...

  7. github开发

    从0开始学习 GitHub 系列之「初识 GitHub」 从0开始学习 GitHub 系列之「加入 GitHub」 从0开始学习 GitHub 系列之「Git 速成」 从0开始学习 GitHub 系列 ...

  8. CI 框架导出文件

    CI框架目录结构: |-application (应用目录) |-system (核心目录) |-downexcel (文件存在目录) |-ZipBackDir (备份目录) |-index.php ...

  9. 用eclipse打开已经编译的工程

    第一种方法: eciplise------>File------>Import------>General------>Existing Project into Worksp ...

  10. 通过Ajax异步提交的方法实现从数据库获取省份和城市信息实现二级联动(xml方法)

    之前有写过是从JavaScript数组里获取省市信息来实现二级联动,但是似乎有很多需求是要从数据库里获取信息,所以就需要根据异步提交,局部刷新的思想来实现来提高用户交互问题 第一种方法是xml方法 1 ...