Code example for Codeigniter login registration

Source: Internet
Author: User
In this example, the Codeigniter registration logon code is derived from a codeigniter project. For more information, see the CI framework.

In this example, the Codeigniter registration logon code comes from a codeigniter project ~

Because the Active Record class is used, generally there are only two layers of database operations, namely C and V.

Controllers/login. php file:

The code is as follows: Class Login extends CI_Controller {
Private $ pass = '';
Public function _ construct (){
Parent: :__ construct ();
$ This-> load-> helper (array (
'Form ',
'URL'
));
$ This-> load-> library ('session ');
}
Public function index (){
$ This-> load-> view ('login ');
}
Public function formsubmit (){
$ This-> load-> library ('form _ validation ');

$ This-> form_validation-> set_rules ('username', 'username', 'required ');
$ This-> form_validation-> set_rules ('password', 'password', 'required ');
If ($ this-> form_validation-> run () = FALSE ){
$ This-> load-> view ('login ');
} Else {
If (isset ($ _ POST ['submit ']) &! Empty ($ _ POST ['submit ']) {
$ Data = array (
'User' => $ _ POST ['username'],
'Pass' => md5 ($ _ POST ['password'])
);
$ Newdata = array (
'Username' => $ data ['user'],
'Userip' => $ _ SERVER ['remote _ ADDR '],
'Luptime' => time ()
);
If ($ _ POST ['submit '] = 'login '){
$ Query = $ this-> db-> get_where ('UC _ user', array (
'User' => $ data ['user']
), 1, 0 );

Foreach ($ query-> result () as $ row ){
$ Pass = $ row-> pass;
}
If ($ pass = $ data ['pass']) {

$ This-> session-> set_userdata ($ newdata );
$ This-> load-> view ('usercenter', $ data );
}
} Else if ($ _ POST ['submit '] = 'register '){

$ This-> session-> set_userdata ($ newdata );
$ This-> db-> insert ('UC _ user', $ data );
$ This-> load-> view ('usercenter', $ data );
} Else {
$ This-> session-> sess_destroy ();
$ This-> load-> view ('login ');
}
}
}
}
}

Views/login. php file:

The code is as follows:















User name
Password






SQL statement:

Uc_user: The code is as follows: create table 'UC _ user '(
'Id' int (11) unsigned zerofill not null AUTO_INCREMENT,
'User' varchar (20) not null,
'Pass' char (32) not null,
'Role' enum ('root', 'normal') not null default 'root ',
'Del 'decimal (1, 0) not null default '0 ',
Primary key ('id ')
) ENGINE = InnoDB AUTO_INCREMENT = 5 default charset = latin1

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.