我記得剛碰php的時候, 之前貌似是只有cookie, 而後來因為有需求, session才發展起來 (PHP4?) , 有了SESSION能讓功能更全面, 我個人覺得倆哥有一點相似, 但後來的session不算來取代cookie的, 反而cookie搭配session 有時可以做出更多更方便的東西
session: 儲存於server端, 所以不必擔心禁用的問題
有些不重要的資料其實用 –>cookie
但是重要的資訊(ex:帳號密碼)–>session
簡單使用紀錄
當時用AdminLTE 套框了一哥後台系統, 其中的呼叫session是這樣的
class All extends CI_Controller {
function __construct() {//檢查網頁是否有登入使用者
parent::__construct();
$this->load->model('Usermodel');
$this->load->library('session');
if (!$this->session->userdata('admin_')) {
redirect('login');
}
$data['user'] = $this->session->userdata('user_name');
$data['pg'] = 'Web';
$data['inpg'] = 'edit_ask.php';
}
public function index() {
$this->load->library('session');
$this->load->model('Msgmodel');
$data['mg']= $this->Msgmodel->msg_all()->result_array();
if (!$this->session->userdata('admin_')) {
redirect('login');
}
$data['name'] = Array(
'id' => $this->session->userdata('id'),
'email' => $this->session->userdata('email'),
'name' => $this->session->userdata('name'),
'password' => $this->session->userdata('password'),
'loged_in' => $this->session->userdata('loged_in')
);
$this->load->view('all', $data);
}
public function deleteuser($id) {//點選writer的id
$this->load->model('Msgmodel');
$this->Msgmodel->delete('course', $id);
echo 'ok';
}
}
class Register extends CI_Controller {
function __construct() {
parent::__construct();
//載入CI的session 存放驗證碼的數字
$this->load->library('session'); //初始化session
$this->load->helper('url');
}
public function index() {
$this->load->view('register');
}
function Keycik() {
$img_height = 30; // 圖形高度
$img_width = 60; // 圖形寬度
$mass = 100; // 雜點的數量,數字愈大愈不容易辨識
$num = ""; // rand後所存的地方
$num_max = 4; // 產生4個驗證碼
for ($i = 0; $i < $num_max; $i++) {
$num .= rand(0, 9);
}
//把驗證碼存進session (name,value)
$this->session->set_userdata('Checknum', $num);
// 創造圖片,定義圖形和文字顏色
$im = imagecreate($img_width, $img_height);
$black = ImageColorAllocate($im, 250, 250, 250); // (0,0,0)文字為黑色
$gray = ImageColorAllocate($im, 0, 0, 0); // (200,200,200)背景是灰色
imagefill($im, 0, 0, $gray);
// 在圖形產上黑點,起干擾作用; (圖片,起始x軸,起始y軸,顏色)
for ($i = 0; $i < $mass; $i++) {
imagesetpixel($im, rand(0, $img_width), rand(0, $img_height), $black);
}
// 將數字隨機顯示在圖形上,文字的位置都按一定波動範圍隨機生成
$strx = rand(3, 8);
for ($i = 0; $i < $num_max; $i++) {
$strpos = rand(1, 8);
imagestring($im, 5, $strx, $strpos, substr($num, $i, 1), $black); //在圖片上列出字串(圖片,font,x軸起始位置,y軸起始位置,顯示的字串,顏色)
$strx+=rand(8, 14);
}
ImagePNG($im);
ImageDestroy($im);
}
function insert() {//插入使用者
$this->load->model('Usermodel');
$st = $this->input->post('status');
$checknum = $this->session->userdata('Checknum');
$account = $this->input->post('account');
$name = $this->input->post('name');
$password1 = $this->input->post('password1');
$password2 = $this->input->post('password2');
if (empty($account) or empty($name) or empty($password1) or empty($password2)) {
echo "empty"; //判斷是否有未填欄位
} else if ($password1 !== $password2) {
echo "wrong"; //密碼輸入是否正確
} else if (mb_strlen($password1,'utf-8') < 6) {
echo "tooshort"; //密碼長度
} else if (mb_strlen($password1,'utf-8') > 20) {
echo "toolong";
} else if (!filter_var($account, FILTER_VALIDATE_EMAIL)) {
$emailerr = "email"; //檢查帳號格式
echo $emailerr;
} else {
$this->Usermodel->regist($account, $name, $password1, $st);
echo "ok";
}
}
}
主要是用來對登入的使用者作一些判斷, 超過時間也會自動登出這樣