To allow easier integration with other scripts and make things more modular the core script is an object with very simple interface. class User { This is the class definition and the constructor of the object. OK it's not perfectly modular but a date isn't much of a problem. It is invoked like: $date = gmdate("'Y-m-d'"); Now to clear the code purpose, we check if the user is logged in. If he/she is then we check the session (remember it is a secure script), if not and a cookie named just for example mtwebLogin is checked - this is to let remembered visitors be recognized. Logging in Users To allow users to login you should build a web form, after validation of the form you can check if the user credentials are right with $user->_checkLogin('username', 'password', remember). Username and password should not be constants of course, remember is a boolean flag which if set will send a cookie to the visitor to allow later automatic logins. function _checkLogin($username, $password, $remember) { The function definition should be placed inside the User class definition as all code that follows. The function uses PEAR::DB's quote method to ensure that data that will be passed to the database is safely escaped. I've used PHP's md5 function rather than MySQL's because other databases may not have that. The WHERE statement is optimized (the order of checks) because username is defined as UNIQUE. No checks for a DB_Error object are needed because of the default error mode set above. If there is a match in the database $result will be an object, so set our session variables and return true (successful login). Otherwise set the failed property to true (checked to decide whether to display a login failed page or not) and do a logout of the visitor. The logout method just executes session_defaults(). Setting the Session function _setSession(&$values, $remember, $init = true) { $sql = "UPDATE member SET session = $session, ip = $ip WHERE " . This method sets the session variables and if requested sends the cookie for a persistent login, there is also a parameter which determines if this is an initial login (via the login form/via cookies) or a subsequent session check. Persistent Logins If the visitor requested a cookie will be send to allow skipping the login procedure on each visit to the site. The following two methods are used to handle this situation. function updateCookie($cookie, $save) { Checking Persistent Login Credentials If the user has chosen to let the script remember him/her then a cookie is saved, which is checked via the following method. function _checkRemembered($cookie) { This function should not trigger any error messages at all. To make things more secure a cookie value is saved in the cookie not the user password. This way one can request a password for areas which require even higher security. Ensuring Valid Session Data function _checkSession() { So this is the final part, we check if the cookie saved in the session is right, the session id and the IP address of the visitor. The call to setSession is with a parameter to let it know that this is not the first login to the system and thus not update the IP and session id which would be useless anyway.
blog comments powered by Disqus |
|
|
|
|
|
|
|