|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?注册
×
Codeigniter处理用户登录验证后URL跳转,涉及到My_Controller.php以及登录验证模块User.php,代码如下:0 ?4 L1 U- X3 [* {( x" N8 {
Codeigniter处理用户登录验证后URL跳转,涉及到My_Controller.php以及登录验证模块User.php,代码如下:
1 ^' Z0 K3 M7 ?2 Y My_Controller.php
5 c' V5 e1 c- X- D U/ q' Q; y& L ^! w class MY_Controller extends CI_Controller( |" s3 c$ `2 m) l
: e9 Q% Z& B6 J
{ & y/ M! j0 \ c/ k/ R
public function __construct()8 y# B. P2 Q& m: g4 S
' i/ a: x Z2 U$ p' C u
{
; H# Y$ Z5 \+ F& x% W/ g parent::__construct(); u' Z5 Y! f. K' s' n: ~0 E+ j
/*判断是否登录,判断当前URL是否是auth/login*/
$ C0 Y5 X- r+ d! p& c5 C if ( ! $this->tank_auth->is_logged_in()
4 W) a4 H2 l S4 D) S && ( $this->router->fetch_class() != 'auth' && $this->router->fetch_method() != 'login'))
6 `; W9 Z3 j8 O* x) a& R
5 e5 j: O1 V7 n; \. w; E { + W: T. a* g# n. ^- _0 V* Y
$redirect = $this->uri->uri_string();& R$ o& G) b- K# E: W7 M$ l* d
if ( $_SERVER['QUERY_STRING'])
' t! m: n$ o& C, p+ Z
9 Q1 p6 `! z. V+ b+ C8 S' N { ' D: ^/ V X, _
$redirect .= '?' . $_SERVER['QUERY_STRING']; @+ }* b/ ?0 b
, n( j8 l1 L1 p/ s0 G# y% d+ p
}
. i* a! G4 F( m1 E1 l I3 G /*跳转到用户登陆页面,指定Login后跳转的URL*/
1 a: }$ j. e; G% m5 o redirect('auth/login?redirect='.$redirect);
6 R1 W4 S7 [4 z& o
; V, L4 A9 q# F, x3 D7 O } & U6 P8 t7 K; t4 W0 @
& e: G5 [; A4 r+ E2 x0 O
}
5 f; P' B+ g; D! k" G1 J0 r% Y" Z5 P& `& k
}
6 c5 E( L* a1 }3 o User.php
( Y6 a8 D! I+ W; b* }' ]' i% J class User extends MY_Controller( w5 d/ }4 q) N; W" D9 V, E G: P
6 h n! h/ X: B3 Q7 v, {/ r5 ]
{
6 Y/ b7 ~3 I, W function login()+ }) Y1 d& T$ a' M8 N$ @
+ s2 J0 v/ ?# Z a { ( A' d, G- s; r5 R u! ]
if ($this->tank_auth->is_logged_in()) { // logged in. P/ N2 o! E8 |* q/ Y& \4 w$ x
redirect('/');
# ^% z4 z; s" Y4 v* s% [2 k/ t } else {9 I/ }' E4 C- y! S( p( F
//other codes here......
\1 l& u M# a" T; j$ R /*判断是否有redirect信息*/
% V) l5 K; ~/ l6 S7 P; m; u $data['redirect'] = isset($_GET['redirect']) ? $_GET['redirect'] : '/';5 E0 D! A4 A4 B/ S
if ($this->form_validation->run()) { // validation ok
* G+ m) G4 r! Z if ($this->tank_auth->login(
: R" I) S0 ^% P! t f. t $this->form_validation->set_value('login'),
, l0 {# K5 s9 a. O8 M $this->form_validation->set_value('password'),2 S9 C! w4 _! Y9 x: G2 m# {: ]
$this->form_validation->set_value('remember'),4 x4 T( G7 v# E7 {) z0 H4 z
$data['login_by_username'],
) n) s# _% k# H! D2 _2 a" U0 ?6 o $data['login_by_email'])) { // success
9 ^7 }: {: F. n: q% K redirect($data['redirect']);
@# u& B2 x+ p" t4 U ] } else {% N8 }% s# f( C5 v w% l5 `
//error handling
8 _- E7 v3 |+ x* p
7 G, _: q2 k% g% L } ( F+ q G) x) O, v4 D
2 E/ R+ I/ F3 o$ i2 k- `) V: p$ D } ( x/ E' l' | Q. G1 x' M
$this->load->view("login_form")
9 i0 Q% B' Y1 W! I7 @( Y$ g* G' g0 v: |1 l# A- x9 ^' m
} # X3 b: I0 ^: d$ n7 x- y
/ a! c3 D8 m7 C( L, b: R6 V
}
% y2 {2 M2 f R4 l
& u/ l( ?+ R: U* w; X4 Z% H; ~/* , f3 z' C" @6 t. F* q! {: _( M* b1 y
Note: 在login_form中需要注意,提交表单的form地址:# q/ g& D) S' d& E" _4 C
$ z2 o, {& d1 `4 E: j" T
; ?) [7 J6 u$ z& l6 h* M
*/ 1 S1 D7 W/ Y# |9 J4 q+ Q
; i% j4 S3 b! N1 N
} 0 S; m" n o' h& k. w9 T7 l
在login_form中需要注意,提交表单的form地址:
) r) S" ^* n8 c* L
& h C8 D+ ?( \6 y( p( x1 O本文由外链代发http://t.qq.com/wailiandf整理 |
|