ci框架 权限 页面跳转 php,Codeigniter实例之用户登录验证与URL跳转

Codeigniter处理用户登录验证后URL跳转,主要涉及到了My_Controller.php页面以及登录验证模块User.php页面。

1,My_Controller.php页面:

复制代码 代码示例:

class MY_Controller extends CI_Controller

{

public function __construct()

{

parent::__construct();

/*判断是否登录,判断当前URL是否是auth/login*/

if ( ! $this->tank_auth->is_logged_in()

&& ( $this->router->fetch_class() != 'auth' && $this->router->fetch_method() != 'login'))

{

$redirect = $this->uri->uri_string();

if ( $_SERVER['QUERY_STRING'])

{

$redirect .= '?' . $_SERVER['QUERY_STRING'];

}

/*跳转到用户登陆页面,指定Login后跳转的URL*/

redirect('auth/login?redirect='.$redirect);

}

}

}

2,User.php页面:

复制代码 代码示例:

class User extends MY_Controller

{

function login()

{

if ($this->tank_auth->is_logged_in()) { // logged in

redirect('/');

} else {

//other codes here......

/*判断是否有redirect信息*/

$data['redirect'] = isset($_GET['redirect']) ? $_GET['redirect'] : '/';

if ($this->form_validation->run()) {  // validation ok

if ($this->tank_auth->login(

$this->form_validation->set_value('login'),

$this->form_validation->set_value('password'),

$this->form_validation->set_value('remember'),

$data['login_by_username'],

$data['login_by_email'])) { // success

redirect($data['redirect']);

} else {

//error handling

}

}

$this->load->view("login_form")

}

}

/*

Note: 在login_form中注意,提交表单的form地址:

*/

}

3,在login_form中注意提交表单的form地址为:

复制代码 代码示例:

echo form_open(site_url("/auth/login?redirect=".$redirect));

?>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
codeigniter 实现登陆 后台auth权限 管理员管理 项目文件请参考 根目录下的CI文件夹 个人娱乐,使用CI框架进行开发一个含有登陆,auth权限验证,后台管理员管理登陆的简单项目 1:数据库结构如下(使用mysql) /* Navicat Premium Data Transfer Source Server : mysql_localhot Source Server Type : MySQL Source Server Version : 50624 Source Host : localhost Source Database : codeigniter Target Server Type : MySQL Target Server Version : 50624 File Encoding : utf-8 Date: 09/28/2015 17:07:46 PM */ SET FOREIGN_KEY_CHECKS = 0; -- Table structure for auth_group DROP TABLE IF EXISTS auth_group; CREATE TABLE auth_group ( id mediumint(8) unsigned NOT NULL AUTO_INCREMENT, title char(100) NOT NULL DEFAULT '', status tinyint(1) NOT NULL DEFAULT '1', rules varchar(256) NOT NULL DEFAULT '', PRIMARY KEY (id) ) ENGINE=MyISAM AUTO_INCREMENT=44 DEFAULT CHARSET=utf8; -- Table structure for auth_group_access DROP TABLE IF EXISTS auth_group_access; CREATE TABLE auth_group_access ( uid mediumint(8) unsigned NOT NULL, group_id mediumint(8) unsigned NOT NULL, UNIQUE KEY uid_group_id (uid,group_id), KEY uid (uid), KEY group_id (group_id) ) ENGINE=MyISAM DEFAULT CHARSET=utf8; -- Table structure for auth_rule DROP TABLE IF EXISTS auth_rule; CREATE TABLE auth_rule ( id mediumint(8) unsigned NOT NULL AUTO_INCREMENT, name char(80) NOT NULL DEFAULT '', title varchar(255) NOT NULL DEFAULT '', type tinyint(1) NOT NULL DEFAULT '1', status tinyint(1) NOT NULL DEFAULT '1', condition char(100) NOT NULL DEFAULT '', parent_id mediumint(8) DEFAULT '0', sort mediumint(8) DEFAULT '1', display tinyint(1) NOT NULL DEFAULT '1' COMMENT '1:显示 0:不显示', class varchar(64) DEFAULT NULL, PRIMARY KEY (id), UNIQUE KEY name (name) ) ENGINE=MyISAM AUTO_INCREMENT=27 DEFAULT CHARSET=utf8; -- Table structure for user DROP TABLE IF EXISTS user; CREATE TABLE user ( id int(11) unsigned NOT NULL AUTO_INCREMENT, username varchar(255) NOT NULL, password varchar(64) NOT NULL, email char(100) DEFAULT NULL, mobile char(15) DEFAULT NULL, reg_ip varchar(15) DEFAULT NULL, reg_time int(10) DEFAULT NULL, last_login_time int(10) DEFAULT NULL, last_login_ip varchar(15) DEFAULT NULL, login_count int(11) DEFAULT NULL, update_time int(10) DEFAULT NULL, status enum('1','0') DEFAULT '1' COMMENT '1:激活 0:禁用', del enum('0','1') DEFAULT '0' COMMENT '1:删除', role tinyint(2) DEFAULT NULL, PRIMARY KEY (id) ) ENGINE=InnoDB AUTO_INCREMENT=10 DEFAULT CHARSET=utf8; SET FOREIGN_KEY_CHECKS = 1; 2:了解该项目主要是如何熟悉一个新框架的工作原理,如果快速开发,怎样搭建一个项目。 1):框架的layout(网上查找,主要有两种方式,比较方便是通过写layout类),如下:admin/libraries/Layout.php 在Controller中定义公共类My_Controller继承CI_Controller,在构造函数中直接加载,如下:admin/controllers/MY_Controller.php 2):在ci如何加载css,js,image等等公共文件,在对应的模块中helper中可以扩展url,我的扩展如下(我是使用bootstrap进行搭建后台):admin/helpers/MY_url_helper.php 3):在layout定义layout视图文件,具体看code 注意:加载url中助手类中自定义方法,需要$this->load->helper('url');例如 3,具体的ci项目是如何进行工作,参考codeigniter手册以及项目代码 4,时间匆忙,也属于学习ci框架阶段,可能有错误或者code有问题的地方,尽请谅解,后面继续完善
这里提供一个基于CodeIgniter框架和Bootstrap前端框架的页面布局示例: 1. 在CI框架中创建一个名为"app"的控制器(app.php),在其中定义一个名为"index"的方法,用于加载视图文件。 ```php class App extends CI_Controller { public function index() { $data['title'] = 'My Application'; $this->load->view('header', $data); $this->load->view('navbar'); $this->load->view('sidebar'); $this->load->view('content'); $this->load->view('footer'); } } ``` 2. 创建一个名为"header.php"的视图文件,用于页面头部的内容。 ```html <!DOCTYPE html> <html> <head> <title><?php echo $title; ?></title> <link rel="stylesheet" href="<?php echo base_url('assets/css/bootstrap.min.css'); ?>"> </head> <body> ``` 3. 创建一个名为"navbar.php"的视图文件,用于页面顶部的导航栏。 ```html <nav class="navbar navbar-expand-lg navbar-light bg-light"> <a class="navbar-brand" href="#">My Application</a> <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarNav" aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation"> <span class="navbar-toggler-icon"></span> </button> <div class="collapse navbar-collapse" id="navbarNav"> <ul class="navbar-nav"> <li class="nav-item active"> <a class="nav-link" href="#">Home <span class="sr-only">(current)</span></a> </li> <li class="nav-item"> <a class="nav-link" href="#">About</a> </li> <li class="nav-item"> <a class="nav-link" href="#">Contact</a> </li> </ul> </div> </nav> ``` 4. 创建一个名为"sidebar.php"的视图文件,用于页面左侧的侧边栏。 ```html <div class="container-fluid"> <div class="row"> <nav class="col-md-2 d-none d-md-block bg-light sidebar"> <div class="sidebar-sticky"> <ul class="nav flex-column"> <li class="nav-item"> <a class="nav-link active" href="#"> <span data-feather="home"></span> Dashboard <span class="sr-only">(current)</span> </a> </li> <li class="nav-item"> <a class="nav-link" href="#"> <span data-feather="file"></span> Orders </a> </li> <li class="nav-item"> <a class="nav-link" href="#"> <span data-feather="shopping-cart"></span> Products </a> </li> </ul> </div> </nav> </div> </div> ``` 5. 创建一个名为"content.php"的视图文件,用于页面中间的内容区域。 ```html <main role="main" class="col-md-9 ml-sm-auto col-lg-10 px-4"> <div class="d-flex justify-content-between flex-wrap flex-md-nowrap align-items-center pt-3 pb-2 mb-3 border-bottom"> <h1 class="h2">Dashboard</h1> <div class="btn-toolbar mb-2 mb-md-0"> <div class="btn-group mr-2"> <button type="button" class="btn btn-sm btn-outline-secondary">Share</button> <button type="button" class="btn btn-sm btn-outline-secondary">Export</button> </div> <button type="button" class="btn btn-sm btn-outline-secondary dropdown-toggle"> <span data-feather="calendar"></span> This week </button> </div> </div> <canvas class="my-4" id="myChart" width="900" height="380"></canvas> <h2>Section title</h2> <div class="table-responsive"> <table class="table table-striped table-sm"> <thead> <tr> <th>#</th> <th>Header</th> <th>Header</th> <th>Header</th> <th>Header</th> </tr> </thead> <tbody> <tr> <td>1,001</td> <td>Lorem</td> <td>ipsum</td> <td>dolor</td> <td>sit</td> </tr> </tbody> </table> </div> </main> ``` 6. 创建一个名为"footer.php"的视图文件,用于页面底部的内容。 ```html <script src="<?php echo base_url('assets/js/jquery-3.3.1.slim.min.js'); ?>"></script> <script src="<?php echo base_url('assets/js/popper.min.js'); ?>"></script> <script src="<?php echo base_url('assets/js/bootstrap.min.js'); ?>"></script> </body> </html> ``` 7. 将Bootstrap和jQuery的文件放到CI框架的"assets"文件夹中。 8. 访问"http://yourdomain.com/app"即可看到页面布局效果。 以上代码仅作为示例,具体的页面布局可以根据实际需求进行调整和修改。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值