程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 網頁編程 >> PHP編程 >> PHP綜合 >> Codeigniter注冊登錄代碼示例

Codeigniter注冊登錄代碼示例

編輯:PHP綜合

本示例Codeigniter注冊登錄代碼源自一個codeigniter的項目,現分享下~

由於使用了 Active Record 類,一般數據庫操作只有兩層,即是C和V

controllers/login.php文件:

復制代碼 代碼如下:<?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文件:

復制代碼 代碼如下:<html>
<?php echo validation_errors(); ?>
<?php echo form_open('login/formsubmit'); ?>
<table>
<tr>
<td>用戶名</td>
<td><input type="text" name="username"></td>
</tr>
<tr>
<td>密碼</td>
<td><input type="password" name="password"></td>
</tr>
<tr>
<td>
<input type="submit" name="submit" value="login">
</td>
<td>
<input type="submit" name="submit" value="register">
</td>
</tr>
</table>
</form>
</html>

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 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved