CI框架--session
用到的基本东西:
1.修改配置文件
config.php
$config['encryption_key']
2.加载session类
$this->load->library('session');
3.创建session
$this->session->set_userdata($array);
4.查看session
$this->session->userdata('session名');
5.删除session
$this->session->unset_userdata('session名');
例子:
views/login.php
<html> <center><h1>用户登录页面</h1></center> <form action="/ci/index.php/login/checklogin" method="post"> <center> <ul style="list-style:none"> <li>用户名</li> <li><input name="uname" type="text" /></li> <li>密码</li> <li><input name="upass" type="password" /></li> <li><input name="sub" type="submit" value="提交" /></li> </ul> </center> </form> </html>
models/test_m.php
<?php class Test_m extends CI_Model { function __construct() { parent::__construct(); $this->load->database(); } function user_select($uname) { $this->db->where('uname',$uname); $this->db->select('*'); $query=$this->db->get('user'); //var_dump($query->result());die; return $query->result(); } } ?>
controllers/login.php
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); class Login extends CI_Controller { function index() { $this->load->view("login"); } function checklogin() { $this->load->model("test_m"); $user=$this->test_m->user_select($_POST['uname']); if($user) { if($user[0]->upass==$_POST['upass']) { echo "密码正确"; $this->load->library('session'); $arr=array('uid'=>$user[0]->uid); $this->session->set_userdata($arr); echo $this->session->userdata('uid'); }else { echo "密码错误"; } }else { echo "用户不存在"; } } function checksession() { $this->load->library('session'); if($this->session->userdata('uid')) { echo "已经登录"; }else { echo "没登录"; } } function logout() { $this->load->library('session'); $this->session->unset_userdata('uid'); } } ?>
哎,代码乱的无法直视啊
=>array数组的键和值
->对象的成员