|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?注册
×
Codeigniter处理用户登录验证后URL跳转,涉及到My_Controller.php以及登录验证模块User.php,代码如下:
1 c% |, [+ c" |9 D% p( A Codeigniter处理用户登录验证后URL跳转,涉及到My_Controller.php以及登录验证模块User.php,代码如下:
- j' T0 ]' h# U1 {! n My_Controller.php $ K, h4 y6 w/ K
class MY_Controller extends CI_Controller! |2 u" x/ k4 W, o. a/ b9 _
6 t" Z1 J5 j) g# k9 f
{ $ c6 o& n$ Q$ n. G
public function __construct()! e, |8 B o0 o" d \; a# V: f3 y- l
7 v, k' h6 @9 J; V { 9 L3 m- u2 w0 u9 j" d6 e2 C
parent::__construct();
: J3 R0 v a) c4 w5 q0 l /*判断是否登录,判断当前URL是否是auth/login*/
# ~# i, u/ \) ^' c+ Z) N6 R if ( ! $this->tank_auth->is_logged_in()
7 Q+ B- E8 f4 K && ( $this->router->fetch_class() != 'auth' && $this->router->fetch_method() != 'login'))$ t/ ]% i+ p6 Y6 N) d6 Z& O
. A; i* }4 u6 A. i) u9 R3 F
{
7 Z, F5 I0 n7 c) i $redirect = $this->uri->uri_string();
% V% H$ I8 @# g/ {5 L) T if ( $_SERVER['QUERY_STRING'])
" p# {' f! {- c& ` I( v4 B$ T" L; ^ ^7 z
{ & {6 y5 L* L- m. g+ [+ B4 w
$redirect .= '?' . $_SERVER['QUERY_STRING'];3 Z) d0 w0 A# g, r" O
|, k' F6 l% F* `7 i N' E: x
}
1 n6 v. Z* b. p w /*跳转到用户登陆页面,指定Login后跳转的URL*/ k) c3 C! q+ s
redirect('auth/login?redirect='.$redirect);
0 ]# W+ [; {" J. l
( W# ^! ]2 W+ n4 C } ; T8 A. J3 A& k
1 d, C* M" g/ A- \. u; E1 I1 P
}
, t. n( l& @9 ? C; T& e
/ \5 W' H8 j7 R" Y: p1 Q} " l/ p4 V# t3 ^% i- t
User.php
& t0 b' y, w% ?' q6 a Q% F class User extends MY_Controller
0 ~! C, J) A+ ?4 D, t+ B) h9 s; C/ a/ z h
{
1 _6 Y f$ T- ~5 d function login()' o' C& ^: F- m7 e6 j
# q7 c7 y5 }% ?$ D" k* K9 R8 R {
# [" l+ p" x7 H( E if ($this->tank_auth->is_logged_in()) { // logged in
! h- w. j5 h8 e. W! n redirect('/');' F3 V( g5 r7 L5 A/ X, N
} else {' [; q) b2 e% k% Z- |+ k
//other codes here......
$ L8 L* }! `7 F: d% m9 j /*判断是否有redirect信息*/
! m8 }8 Z- l- J- K" |9 h' E $data['redirect'] = isset($_GET['redirect']) ? $_GET['redirect'] : '/';
2 `! C- y1 N# g/ ` if ($this->form_validation->run()) { // validation ok
3 O/ v. m/ l- `& }& N& E if ($this->tank_auth->login(& `# p; ]( U9 R. V
$this->form_validation->set_value('login'),
) G. e6 A3 ?, _7 R- w $this->form_validation->set_value('password'),
) G& D O |* _* f) A $this->form_validation->set_value('remember'),9 a2 \* n' w& ]* j
$data['login_by_username'],
: r- }3 j) `0 l5 k8 u. o( M5 ?( I $data['login_by_email'])) { // success
( b$ p! v4 Y9 N, r2 u# }( E: Y redirect($data['redirect']);" \" o; T+ I( m' [+ w1 m' Z/ W# c6 t
} else {; A3 q( N6 K1 X, G2 Z0 g* f0 M2 a
//error handling
0 ?. _: g# ^2 b, C. L2 z
# {) v( z6 m$ W5 y; w! t2 Y- o! H% e }
4 W1 E& O, x# o, `2 c0 j$ b! B
]1 O+ X% }7 f- a }
: @+ A5 `- R( z+ O# H: C: t% L $this->load->view("login_form")
0 _% c1 b& x J! g* k
; [/ v& ^ w% E# K3 s2 k }
1 M9 q! k/ e' G- K' H" f
3 |2 l" a3 N" L5 v% |; f }
; ?# ]3 w& z+ K
1 e+ q6 y/ p& r/* " v/ z) ]: {, ?- @- K
Note: 在login_form中需要注意,提交表单的form地址:9 T/ _% I. k5 M8 d& M6 K9 U$ Q2 y
6 h; \ X+ v' y+ s9 i$ A$ G8 y
$ {' e D, s1 D3 O2 n. g2 F*/
) p. B0 o, B& m6 ^6 ?2 K( G0 Q/ ?( q4 S6 | A* q
} % K* t8 A& C3 H7 C4 @
在login_form中需要注意,提交表单的form地址:4 k1 R8 W! T0 e- t7 W
' [& y( P0 Z7 [' c4 o本文由外链代发http://t.qq.com/wailiandf整理 |
|