Heim > Backend-Entwicklung > PHP-Tutorial > php 依据session与cookie用户登录状态操作类,sessioncookie_PHP教程

php 依据session与cookie用户登录状态操作类,sessioncookie_PHP教程

WBOY
Freigeben: 2016-07-12 08:52:42
Original
815 Leute haben es durchsucht

php 依据session与cookie用户登录状态操作类,sessioncookie

1、用户登录状态操作类UserLogin

 

复制代码
<?<span>php

</span><span>final</span> <span>class</span><span> UserLogin {

    </span><span>public</span> <span>function</span><span> __construct() {
        
    }

    </span><span>public</span> <span>static</span> <span>function</span><span> getUserInfo() {
        </span><span>if</span> (<span>isset</span>(<span>$_COOKIE</span>["user_id"])&&<span>$_COOKIE</span>["user_id"]&&(<span>trim</span>(<span>$_COOKIE</span>["user_id"])!=""<span>)) {
            </span><span>if</span> (<span>isset</span>(<span>$_SESSION</span>["USER_INFO"<span>]))
                </span><span>return</span> <span>$_SESSION</span>["USER_INFO"<span>];
            </span><span>$dao</span> = <span>new</span><span> UserDao();
            </span><span>$user</span> = <span>$dao</span>->find(<span>$_COOKIE</span>["user_id"<span>]);
            </span><span>if</span> (<span>$user</span><span>) {
                </span><span>$_SESSION</span>["USER_INFO"] = <span>$user</span><span>;
                </span><span>setcookie</span>("docloud_sid", <span>session_id</span>(), <span>time</span>() + 36000<span>);
                </span><span>setcookie</span>("user_id", <span>$_COOKIE</span>["user_id"], <span>time</span>() + 36000<span>);
                
                </span><span>if</span> (<span>array_key_exists</span>("selected_prj_id", <span>$_COOKIE</span><span>))
                    </span><span>setcookie</span>("selected_prj_id", <span>$_COOKIE</span>["selected_prj_id"], <span>time</span>() + 36000<span>);
                
                </span><span>if</span> (<span>array_key_exists</span>("selected_class_id", <span>$_COOKIE</span><span>))
                    </span><span>setcookie</span>("selected_class_id", <span>$_COOKIE</span>["selected_class_id"], <span>time</span>() + 36000<span>);
                
                </span><span>if</span> (<span>array_key_exists</span>("selected_image_id", <span>$_COOKIE</span><span>))
                    </span><span>setcookie</span>("selected_image_id", <span>$_COOKIE</span>["selected_image_id"], <span>time</span>() + 36000<span>);
                
                </span><span>if</span> (<span>array_key_exists</span>("test_image_ids", <span>$_COOKIE</span><span>))
                    </span><span>setcookie</span>("test_image_ids", <span>$_COOKIE</span>["test_image_ids"], <span>time</span>() + 36000<span>);
                
                </span><span>if</span> (<span>array_key_exists</span>("upload_image_ids", <span>$_COOKIE</span><span>))
                    </span><span>setcookie</span>("upload_image_ids", <span>$_COOKIE</span>["upload_image_ids"], <span>time</span>() + 36000<span>);
                </span><span>return</span> <span>$user</span><span>;
            }
        }
        self</span>::<span>clearCookie();
        </span><span>return</span> <span>null</span><span>;
    }

    </span><span>public</span> <span>static</span> <span>function</span> setUserInfo(<span>$userInfo</span><span>) {
        </span><span>$_SESSION</span>["USER_INFO"] = <span>$userInfo</span><span>;
        </span><span>setcookie</span>("docloud_sid", <span>session_id</span>(), <span>time</span>() + 36000<span>);
        </span><span>setcookie</span>("user_id", <span>$userInfo</span>->getId(), <span>time</span>() + 36000<span>);
    }

    </span><span>public</span> <span>static</span> <span>function</span><span> isLogin() {
        </span><span>if</span> (self::<span>getUserInfo()) {
            </span><span>return</span> <span>true</span><span>;
        }
        </span><span>return</span> <span>false</span><span>;
    }

    </span><span>public</span> <span>static</span> <span>function</span><span> delUserInfo() {
        self</span>::<span>clearCookie();
        </span><span>session_destroy</span><span>();
    }
    
    </span><span>private</span> <span>static</span> <span>function</span><span> clearCookie() {
        </span><span>setcookie</span>("docloud_sid", "", <span>time</span>() - 36000<span>);
        </span><span>setcookie</span>("user_id", "", <span>time</span>() - 36000<span>);
        </span><span>setcookie</span>("selected_prj_id", "", <span>time</span>() - 36000<span>);
        </span><span>setcookie</span>("selected_class_id", "", <span>time</span>() - 36000<span>);
        </span><span>setcookie</span>("selected_image_id", "", <span>time</span>() - 36000<span>);
        </span><span>setcookie</span>("test_image_ids", "", <span>time</span>() - 36000<span>);
        </span><span>setcookie</span>("upload_image_ids", "", <span>time</span>() - 36000<span>);
    }

}

</span>?>
Nach dem Login kopieren
复制代码

2、在用户输入用户名、密码处调用来做相关判定

复制代码
<?<span>php
</span><span>require_once</span> 'Init.php'<span>;

</span><span>//</span><span> if logged in, logout</span>
<span>if</span> (UserLogin::isLogin() && <span>$_COOKIE</span>["user_id"]==1<span>) {
    UserLogin</span>::<span>delUserInfo();
}
</span><span>else</span> <span>if</span> (UserLogin::<span>isLogin()){
    Utils</span>::redirect('welcome'<span>);
}

</span><span>$username</span> = <span>null</span><span>;
</span><span>$password</span> = <span>null</span><span>;

</span><span>$msg</span> = ""<span>;

</span><span>if</span> (<span>isset</span>(<span>$_POST</span>['username']) && <span>isset</span>(<span>$_POST</span>['password'<span>])) {
    </span><span>$username</span> = <span>addslashes</span>(<span>trim</span>(<span>stripslashes</span>(<span>$_POST</span> ['username'<span>])));
    </span><span>$password</span> = <span>addslashes</span>(<span>trim</span>(<span>stripslashes</span>(<span>$_POST</span> ['password'<span>])));
    </span><span>//</span><span> validate</span>
    <span>$errors</span> = LoginValidator::validate(<span>$username</span>, <span>$password</span><span>);
    
    </span><span>if</span> (<span>empty</span>(<span>$errors</span><span>)) {
        </span><span>//</span><span> save</span>
        <span>$dao</span> = <span>new</span><span> UserDao();
        </span><span>$user</span> = <span>$dao</span>->findByName(<span>$username</span><span>);
        </span><span>$last_login_ip</span> = Utils::<span>getIpAddress();
        </span><span>$user</span>->setLastLoginIp(<span>$last_login_ip</span><span>);
        </span><span>$now</span> = <span>new</span><span> DateTime();
        </span><span>$user</span>->setLastLoginTime(<span>$now</span><span>);
        </span><span>$dao</span>->save(<span>$user</span><span>);
        UserLogin</span>::setUserInfo(<span>$user</span><span>);
        Flash</span>::addFlash('登录成功!'<span>);
        Utils</span>::redirect('welcome'<span>);
    }
    
    </span><span>foreach</span> (<span>$errors</span> <span>as</span> <span>$e</span><span>) {
        </span><span>$msg</span> .= <span>$e</span>->getMessage()."<br>"<span>;
    }
}

</span>?>
Nach dem Login kopieren
复制代码

 

 

 

 

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/1125996.htmlTechArticlephp 依据session与cookie用户登录状态操作类,sessioncookie 1、用户登录状态操作类UserLogin ? php final class UserLogin { public function __construct() { } publi...
Verwandte Etiketten:
Quelle:php.cn
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage