用户
 找回密码
 入住 CI 中国社区
搜索
查看: 2535|回复: 5
收起左侧

[讨论/交流] 问一下各位,登录问题哦

[复制链接]
发表于 2011-7-13 15:25:42 | 显示全部楼层 |阅读模式
我想用codeigniter框架写一个登录注册的代码,刚看这框架有点摸不到头脑,有谁能帮我发过来一个,我看看研究一下
发表于 2011-7-13 15:41:58 | 显示全部楼层
1.把用户输入的用户名密码到数据库查询一下是否有对应的记录。
2.如果有记录生成session,没有跳出
3.跳转到相应页面
发表于 2011-7-13 16:36:17 | 显示全部楼层
统一楼上,这样可能简单点,可能新手还是不知道如何来写
 楼主| 发表于 2011-7-15 11:27:49 | 显示全部楼层
Ben 发表于 2011-7-13 16:36
统一楼上,这样可能简单点,可能新手还是不知道如何来写

能不能给个代码,我看看什么样
发表于 2011-7-16 14:48:19 | 显示全部楼层
本帖最后由 √__豿艉騲℡ 于 2011-7-16 14:55 编辑

昨天刚刚也在弄登陆注册。
本人新手菜鸟,欢迎高手指导……
Controller
PHP复制代码
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
 
 
class User extends CI_Controller {
    /**
     * CLASS USER 构造函数
     *
     * @access public
     * @return void
     */

    function __construct() {
        parent::__construct();
        header('Content-Type: text/html; charset=utf-8');
    }
    /**
     * 登陆
     *
     * @access public
     * @return void
     */

    function login() {
        header('refresh:5; url=' . site_url());
        $this->form_validation->set_rules('username', '用户名', 'trim|required|min_length[5]|max_length[16]');
        $this->form_validation->set_rules('password', '密码', 'trim|required|min_length[6]|max_length[16]');
        if ($this->User_model->login() > 0) {
            echo '登陆成功';
        } else {
            echo '登陆失败';
        }
        echo anchor('', '立即返回');
    }
    /**
     * 加载注册视图
     *
     * @access public
     * @return void
     */

    function registration() {
        $this->load->view('registration_view');
    }
    /**
     * 注册
     *
     * @access public
     * @return void
     */

    function regist() {
        $this->form_validation->set_error_delimiters('<div class="error">', '</div>');
        $this->form_validation->set_rules('username', '用户名', 'trim|required|min_length[5]|max_length[16]|xss_clean|callback_username_check');
        $this->form_validation->set_rules('password', '密码', 'trim|required|min_length[6]|max_length[16]');
        $this->form_validation->set_rules('passconf', '确认密码', 'trim|required|matches[password]');
        if ($this->form_validation->run() == FALSE) {
            $this->load->view('registration_view');
        } else {
            header('refresh:5; url=' . site_url());
            if ($this->User_model->regist()) {
                echo '注册成功.';
            } else {
                echo '注册失败.';
            }
            echo anchor('', '立即返回.');
        }
    }
    /**
     * 检查用户名是否存在
     *
     * @access public
     * @param mixed $str
     * @return bool
     */

    function username_check($str) {
        if ($this->User_model->isusername($str) > 0) {
            $this->form_validation->set_message('username_check', $str . ' 已注册.');
            $bool = false;
        } else {
            $bool = true;
        }
        return $bool;
    }
    /**
     * 会员登出
     *
     * @access public
     * @return void
     */

    function logout() {
        $this->session->sess_destroy();
        header('refresh:5; url=' . site_url());
        echo '已退出.' . anchor('', '立即返回.');
    }
}
 
复制代码


model
PHP复制代码
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
 
class User_model extends CI_Model {
    /**
     * 会员登陆函数
     *
     * @access public
     * @param string $table
     * @return Int
     */

    function login($table = 'user') {
        $username = $this->input->post('username');
        $password = $this->input->post('password');
        $this->db->where('username', $username);
        $this->db->where('password', $password);
        $query = $this->db->get($table);
        if ($query->num_rows() > 0) {
            $row = $query->row();
            $newdata = array(
                   'username'  => $row->username,
                   'logged_in' => TRUE
               );
            $this->session->set_userdata($newdata);
        }
        return $query->num_rows();
    }
    /**
     * 会员注册函数
     *
     * @access public
     * @param string $table
     * @return TRUE/FALSE
     */

    function regist($table = 'user') {
        $data = array(
            'username' => $this->input->post('username'),
            'password' => $this->input->post('password')
        );
        return $this->db->insert($table, $data);
    }
    /**
     * 判断用户名是否存在
     *
     * @access public
     * @param mixed $username
     * @param string $table
     * @return void
     */

    function isusername($username, $table = 'user') {
        $this->db->where('username', $username);
        $query = $this->db->get($table);
        return $query->num_rows();
    }
}
 
复制代码
发表于 2011-7-16 15:00:36 | 显示全部楼层
config/autoload.php
PHP复制代码
/*
| -------------------------------------------------------------------
|  Auto-load Libraries
| -------------------------------------------------------------------
| These are the classes located in the system/libraries folder
| or in your application/libraries folder.
|
| Prototype:
|
|       $autoload['libraries'] = array('database', 'session', 'xmlrpc');
*/

 
$autoload['libraries'] = array('database', 'table', 'pagination', 'form_validation', 'session', 'encrypt');
复制代码

PHP复制代码
/*
| -------------------------------------------------------------------
|  Auto-load Helper Files
| -------------------------------------------------------------------
| Prototype:
|
|       $autoload['helper'] = array('url', 'file');
*/

 
$autoload['helper'] = array('form', 'url');
复制代码

PHP复制代码
/*
| -------------------------------------------------------------------
|  Auto-load Models
| -------------------------------------------------------------------
| Prototype:
|
|       $autoload['model'] = array('model1', 'model2');
|
*/

 
$autoload['model'] = array('Message_model', 'User_model');
复制代码

本版积分规则