Share a php user login class
In php programming, user logon is often used. here is a good php user logon class. if you need it, refer to it. Php user logon class, code:
ErrReport ($ this-> err_mysql); $ this-> err = $ this-> err_mysql;} function isLoggedin () // determine whether to log on to {if (isset ($ _ COOKIE ['Sid ']) // if the cookie contains sid {session_id ($ _ COOKIE ['Sid']); session_start (); $ this-> username = $ _ SESSION ['username']; $ this-> userid = $ _ SESSION ['userid']; $ this-> userlevel = $ _ SESSION ['userlevel']; return true;} else // if no sid is saved in the cookie, check session {session_start () directly (); if (isset ($ _ SESSION ['username ']) Return true;} return false;} function userAuth ($ username, $ userpass) // user authentication {$ this-> username = $ username; $ this-> userpass = $ userpass; $ query = "select * from '". $ this-> authtable. "'where' username' = '$ username';"; $ result = mysql_query ($ query); if (mysql_num_rows ($ result )! = 0) // find this user {$ row = mysql_fetch_array ($ result); if ($ row ['bannd'] = 1) // this user is banned {$ this-> errReport ($ this-> err_user); $ this-> err = $ this-> err_user; return false ;} elseif (md5 ($ userpass) ==$ row ['userpass']) // password match {$ this-> userid = $ row ['id']; $ this-> userlevel = $ row ['userlevel']; return true;} else // password mismatch {$ this-> errReport ($ this-> err_password ); $ this-> err = $ this-> err_password; return false;} else // this is not found User {$ this-> errReport ($ this-> err_username); $ this-> err = $ this-> err_username; return false ;}} function setSession () // Set session {$ sid = uniqid ('Sid '); // Generate sid session_id ($ sid); session_start (); $ _ SESSION ['username'] = $ this-> username; // assign a value to the session variable $ _ SESSION ['userid'] = $ this-> userid ;//.. $ _ SESSION ['userlevel'] = $ this-> userlevel ;//.. if ($ this-> use_cookie) // if you use cookies to save sid {if (! Setcookie ('Sid ', $ sid, time () + $ this-> cookietime, $ this-> cookiepath) $ this-> errReport ("set cookie failed ");} else setcookie ('Sid ', ", time ()-3600); // clear the sid} function userLogout () in the cookie // log off {session_start (); unset ($ _ SESSION ['username']); // clear the username if (setcookie ('Sid', ", time ()-3600) in the session )) // clear sid return true in cookie; else return false;} function errReport ($ str) // error {if ($ this-> error_report) Echo "ERROR: $ str" ;}}?> |