1、用户登录状态操作类UserLogin
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
|
<?php
final class UserLogin {
public function __construct() {
}
public static function getUserInfo() {
if (isset($_COOKIE[ "user_id" ])&&$_COOKIE[ "user_id" ]&&(trim($_COOKIE[ "user_id" ])!= "" )) {
if (isset($_SESSION[ "USER_INFO" ]))
return $_SESSION[ "USER_INFO" ];
$dao = new UserDao();
$user = $dao->find($_COOKIE[ "user_id" ]);
if ($user) {
$_SESSION[ "USER_INFO" ] = $user;
setcookie( "docloud_sid" , session_id(), time() + 36000);
setcookie( "user_id" , $_COOKIE[ "user_id" ], time() + 36000);
if (array_key_exists( "selected_prj_id" , $_COOKIE))
setcookie( "selected_prj_id" , $_COOKIE[ "selected_prj_id" ], time() + 36000);
if (array_key_exists( "selected_class_id" , $_COOKIE))
setcookie( "selected_class_id" , $_COOKIE[ "selected_class_id" ], time() + 36000);
if (array_key_exists( "selected_image_id" , $_COOKIE))
setcookie( "selected_image_id" , $_COOKIE[ "selected_image_id" ], time() + 36000);
if (array_key_exists( "test_image_ids" , $_COOKIE))
setcookie( "test_image_ids" , $_COOKIE[ "test_image_ids" ], time() + 36000);
if (array_key_exists( "upload_image_ids" , $_COOKIE))
setcookie( "upload_image_ids" , $_COOKIE[ "upload_image_ids" ], time() + 36000);
return $user;
}
}
self::clearCookie();
return null ;
}
public static function setUserInfo($userInfo) {
$_SESSION[ "USER_INFO" ] = $userInfo;
setcookie( "docloud_sid" , session_id(), time() + 36000);
setcookie( "user_id" , $userInfo->getId(), time() + 36000);
}
public static function isLogin() {
if (self::getUserInfo()) {
return true ;
}
return false ;
}
public static function delUserInfo() {
self::clearCookie();
session_destroy();
}
private static function clearCookie() {
setcookie( "docloud_sid" , "" , time() - 36000);
setcookie( "user_id" , "" , time() - 36000);
setcookie( "selected_prj_id" , "" , time() - 36000);
setcookie( "selected_class_id" , "" , time() - 36000);
setcookie( "selected_image_id" , "" , time() - 36000);
setcookie( "test_image_ids" , "" , time() - 36000);
setcookie( "upload_image_ids" , "" , time() - 36000);
}
}
?>
|
2、在用户输入用户名、密码处调用来做相关判定
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
|
<?php
require_once 'Init.php' ;
// if logged in, logout
if (UserLogin::isLogin() && $_COOKIE [ "user_id" ]==1) {
UserLogin::delUserInfo();
}
else if (UserLogin::isLogin()){
Utils::redirect( 'welcome' );
}
$username = null;
$password = null;
$msg = "" ;
if (isset( $_POST [ 'username' ]) && isset( $_POST [ 'password' ])) {
$username = addslashes (trim( stripslashes ( $_POST [ 'username' ])));
$password = addslashes (trim( stripslashes ( $_POST [ 'password' ])));
// validate
$errors = LoginValidator::validate( $username , $password );
if ( empty ( $errors )) {
// save
$dao = new UserDao();
$user = $dao ->findByName( $username );
$last_login_ip = Utils::getIpAddress();
$user ->setLastLoginIp( $last_login_ip );
$now = new DateTime();
$user ->setLastLoginTime( $now );
$dao ->save( $user );
UserLogin::setUserInfo( $user );
Flash::addFlash( '登录成功!' );
Utils::redirect( 'welcome' );
}
foreach ( $errors as $e ) {
$msg .= $e ->getMessage(). "<br>" ;
}
}
?>
|
以上这篇php同时使用session和cookie来保存用户登录信息的实现代码就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持服务器之家。