Login.php-controllerphp
<?php defined('BASEPATH') OR exit('No direct script access allowed'); class Login extends CI_Controller { public $users = [ 'admin' => '123456', 'user' => '123' ]; public function __construct() { parent::__construct(); } // 登錄 public function index() { $cookie_login = get_cookie('cookie_login'); $login_name = get_cookie('login_name'); if ($this->session->userdata('admin')) { redirect('/recentimgtext'); } if ($cookie_login == md5('maoriaty')) { $this->session->set_userdata('admin', ['username' => $login_name]); redirect('/recentimgtext'); } $message = ''; if (IS_POST) { $username = $this->input->post('username'); $password = $this->input->post('password'); $remember = $this->input->post('remember'); foreach ($this->users as $k => $user) { if ($k == strtolower($username) && $user == $password) { $cookie_login = md5('maoriaty'); if($remember == '1') { set_cookie('cookie_login', $cookie_login, 3600*24); set_cookie('login_name', $username, 3600*24); } $this->session->set_userdata('admin', ['username' => $username]); redirect('/recentimgtext'); } else { $message = '用戶名或密碼錯誤'; } } } $this->load->view('login', ['message' => $message]); } // 退出登錄 public function logout() { $this->session->unset_userdata('admin'); delete_cookie('cookie_login'); delete_cookie('login_name'); redirect('/login'); } }
login.php-viewcss
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <link rel="stylesheet" href="/resources/static/bootstrap.min.css"> <link rel="stylesheet" href="/resources/static/layout.css"> <script src="/resources/static/jquery.3.3.1.js"></script> <title>登錄</title> <style> .wrap { width: 500px; margin: 0 auto; background-color: #eee; padding: 40px; border-radius: 10px; } </style> </head> <body> <div class="wrap"> <form method="post"> <h3 class="title my-4">用戶登錄</h3> <div class="form-group"> <input type="text" class="form-control" name="username" id="username" aria-describedby="emailHelp" placeholder="請輸入用戶名"> </div> <div class="form-group"> <input type="password" class="form-control" name="password" id="password" placeholder="請輸入密碼"> </div> <div class="form-group form-check"> <input type="checkbox" name="remember" checked value="1" class="form-check-input" id="exampleCheck1"> <label class="form-check-label" for="exampleCheck1">記住我</label> </div> <?=$message?> <button type="submit" class="btn btn-lg btn-block btn-primary">登錄</button> </form> </div> </body> </html>
common_helper.php-helpershtml
// 登錄控制 function check_login($session) { $data = $session->userdata('admin'); $cookie_login = get_cookie('cookie_login'); $login_name = get_cookie('login_name'); if (empty($data)) { if ($cookie_login == md5('maoriaty')) { $session->set_userdata('admin', ['username' => $login_name]); redirect('/recentimgtext'); } redirect('/login'); } }
登錄方法控制,如:jquery
// 修改圖文 public function modimgtext() { check_login($this->session); $imgtextid = $this->input->get('imgtextid'); $template = $this->db->get($this->recent_bg_table)->result_array(); $musiclist = $this->db->get($this->recent_music_table)->result_array(); $data = $this->db->get_where($this->recent_imgtext_table, ['imgtextid' => $imgtextid])->row_array(); if (IS_POST) { $imgtextid = $this->input->post('imgtextid'); $data = [ 'title' => $this->input->post('title'), 'cover' => $this->input->post('o_cover'), 'username' => $this->input->post('username'), 'bgid' => $this->input->post('bgid'), 'musicid' => $this->input->post('musicid'), 'ishot' => $this->input->post('ishot'), 'content' => htmlspecialchars($this->input->post('content')), 'title_color' => $this->input->post('title_color'), ]; $file = 'cover'; $save_path = 'upload/imgtext/cover'; if ($_FILES[$file]['error'] == 0) { $res = $this->uploadimg($file, $save_path); if ($res['errcode'] == 0) { $data['cover'] = $res['url']; } } $this->db->update($this->recent_imgtext_table, $data, ['imgtextid' => $imgtextid]); redirect('/recentimgtext/index'); } $data['content'] = str_replace("'","\"", $data['content']); $this->load->view('recentimgtext/addimgtext', ['data' => $data, 'template' => $template, 'musiclist' => $musiclist, 'title' => '修改圖文']); }
修改配置:config.phpbootstrap
$config['sess_save_path'] = BASEPATH.'../'.'session_file';
自動加載:autoload.phpcookie
$autoload['libraries'] = array('session'); $autoload['helper'] = array('common', 'url', 'cookie');