Codeigniter input class

Source: Internet
Author: User
: This article mainly introduces the input class using codeigniter. if you are interested in the PHP Tutorial, refer to it. I recently wrote a system that does not have a secure filtering input. I want to use the CI input class. after analysis, three files are required under system/core.

Utf8.php, security. php, input. php

It can be integrated into one file, but it is troublesome to use three files, which saves time.

It is used like this:

// Instantiate the secure input class
$ UNI = new utf8 ();
$ SEC = new security ();
$ Input = new input ();

The format is $ username = $ input-> post ('Username.

Of course, you need to modify some configuration variables, such as $ cookiepath, $ cookieprefix, $ cookiedomain, and $ cookiesecure;

However, there is another key configuration, $ this-> _ enable_csrf = FALSE; this variable corresponds to var $ _ enable_csrf = FALSE; the default value is false. if you set it to TRUE,

This will add the _ csrf_token_name key-value pair after the url, because the following code will check this


The cookie value of _ csrf_cookie_name is required.

Refer:

In CI 2.0, there is a csrf (Cross Site Request Forgery) protection function.

If this function is enabled
The post table will send error 500 to the server.
An Error Was Encountered
The action you have requested is not allowed.
Could not parse rows
At this time, a token value should be added to the data value sent by the table ticket.
In order to use the table list function normally

You can find the following line in application/config. php:

1

2

3

4

$config['csrf_protection'] = TRUE;

$config['csrf_token_name'] ='csrf_test_name';

$config['csrf_cookie_name'] ='csrf_cookie_name';

$config['csrf_expire'] = 7200;

Originally, $ config ['csrf _ protection '] can be enabled if the parameter is set to FALSE and changed to TRUE.
After opening the cookie, it will automatically help you store a value in the cookie.
The cookie name can be set in config. php described above.
Then, the token must be added together to submit the table order.

The ajax function of jquery is used as the example below.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

$(function(){

$('#btn').click(function(){

$.ajax({

type:'POST'

,url:'/ajax'// Server side received by ajax

,data:$('#form').serialize()+'&csrf_test_name='+ getCookie('csrf_test_name')

,success:function(data){

alert(data.msg);

}

,dataType:'json'

});

});

});

functiongetCookie(name){

vararr = document.cookie.match(newRegExp("(^| )"+name+"=([^;]*)(;|$)"));

if(arr !=null)returnunescape(arr[2]); returnnull;

}

GetCookie () is the cookie value Retrieved using js.
This is something you can find on the Internet and use it directly.

Csrf_test_name is the number of records that can be set in config. php.
Capture the cookie and send the table list together.
You can use it normally.

Any comments ~

Referer: http://ericlbarnes.com/blog/post/codeigniter_csrf_protection_with_ajax


The above introduces the input class using codeigniter, including some content, and hope to be helpful to friends who are interested in PHP tutorials.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.