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. the user enters the user name and password for relevant determination.
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 ('login successful! ');
- Utils: redirect ('Welcome ');
- }
-
- Foreach ($ errors as $ e ){
- $ Msg. = $ e-> getMessage ()."
";
- }
- }
- ?>
|