search
Homephp教程php手册Codeigniter注册登录代码示例

本示例Codeigniter注册登录代码源自一个codeigniter的项目,现分享下~

由于使用了 Active Record 类,一般数据库操作只有两层,即是C和V

controllers/login.php文件:

复制代码 代码如下:

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文件:

复制代码 代码如下:

















用户名
密码






sql语句:

uc_user:

复制代码 代码如下:

  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 

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment