用户登陆
1.user_login.php
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gbk">
<title>Insert title here</title>
</head>
<body>
<center>
<h1><?php $this->load->view('index.html');?></h1>
<h1>用户登陆</h1>
<form action="/php/myweb/index.php/login/logina" method="post">
用户名: <input type="text" name="username"/><br />
密 码: <input type="password" name="password"/><br />
<input type="submit" value="提交"><br />
</form>
</center>
</body>
</html>
2.LoginController
<?php
class Login extends CI_Controller
{
function index()
{
$this->load->view ( "aa/user_login" );
}
function logina()
{
$this->load->model('user_model');
$user = $this->user_model->user_find_by_username ( $_POST ['username'] );
if ($user)
{
if ($user [0]->password == $_POST ['password'])
{
$arr = Array ('id' => $user [0]->id );
$this->load->library ( 'session' );
$this->session->set_userdata ( $arr );
echo 'aa<br />';
echo $this->session->userdata ( 'id' );
}
else
{
echo '密码不正确';
}
}
else
{
echo "用户名不存在";
}
// var_dump($reult);
// echo $reult[0]->username;
}
function check_login()
{
$this->load->library ( 'session' );
if ($this->session->userdata ( 'id' ))
{
echo '已经登陆';
}
else
{
echo '没有登陆';
}
}
function login_out()
{
$this->load->library ( 'session' );
$this->session->unset_userdata ( 'id' );
}
}
3.userControl
<?php
class User extends CI_Controller
{
function insert()
{
$this->load->model('user_model');
$arr = Array('username'=>'hwpok', 'password'=>'123456');
$this->user_model->user_insert($arr);
}
function update()
{
$this->load->model('user_model');
$id = 2;
$arr = Array('username'=>'hwpokay', 'password'=>'123456');
$this->user_model->user_update($id, $arr);
}
function del()
{
$this->load->model('user_model');
$id = 4;
$this->user_model->user_del($id);
}
function find()
{
$this->load->model('user_model');
$id = 2;
$reult = $this->user_model->user_find($id);
//var_dump($reult);
echo $reult[0]->username;
}
}
4. userModel
<?php
class User_model extends CI_Model {
function __construct()
{
parent::__construct ();
$this->load->database ();
}
function user_insert($arr)
{
$this->db->insert ( 'php_t_user', $arr );
}
function user_update($id, $arr)
{
$this->db->where('id',$id);
$this->db->update('php_t_user',$arr);
}
function user_del($id)
{
$this->db->where('id',$id);
$this->db->delete('php_t_user');
}
function user_find($id)
{
$this->db->where('id',$id);
$this->db->select('*');
$res = $this->db->get('php_t_user');
return $res->result();
}
function user_find_by_username($username)
{
$this->db->where('username',$username);
$this->db->select('*');
$res = $this->db->get('php_t_user');
return $res->result();
}
}