sandyhmily 2013-07-05
代码如下:
<?php ini_set( "display_errors", true ); date_default_timezone_set( "Asia/Shanghai" ); // root and direcotry separate define('DS', DIRECTORY_SEPARATOR); define('ROOT', dirname(dirname(__FILE__))); // database information // need hash define( "DB_USERNAME", "****" ); define( "DB_PASSWORD", '*****' ); define( "DB_NAME", "blog" ); // important directory define( "CLASS_PATH", "classes" ); define( "TEMPLATE_PATH", "templates" ); // user imformation define( "ADMIN_USERNAME", "admin" ); define( "ADMIN_PASSWORD", '$2a$08$wim8kpwHhAKa6MBSsGUMGOYfjkU1xvRKd4Fxwal.wj8dqFboCVSFawim8kpwHhAKa6MBSsGUMGO'); // hash and verified the password function hasher($info, $encdata = false){ $strength = "08"; //if encrypted data is passed, check it against input ($info) if ($encdata) { if (substr($encdata, 0, 60) == crypt($info, "$2a$".$strength."$".substr($encdata, 60))) { return true; }else { return false; } } else { //make a salt and hash it with input, and add salt to end $salt = ""; for ($i = 0; $i < 22; $i++) { $salt .= substr("./ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789", mt_rand(0, 63), 1); } //return 82 char string (60 char hash & 22 char salt) return crypt($info, "$2a$".$strength."$".$salt).$salt; } } function __autoload($className) { if (file_exists(ROOT . DS . 'classes' . DS . strtolower($className) . '.class.php')) { require_once(ROOT . DS . 'classes' . DS . strtolower($className) . '.class.php'); } else { /* Error Generation Code Here */ } }
代码如下:
<?php require_once( "config/config.php" ); session_start( ); $action = isset( $_GET['action'] ) ? $_GET['action'] : ""; $username = isset( $_SESSION['username'] ) ? $_SESSION['username'] : ""; if ( $action != "login" && $action != "logout" && !$username ) { login(); exit; } switch( $action ){ case "login" : login( ) ; break; case "logout"; logout( ); break; default : admin( ); break; } function login( ){ $results['pageTitle'] = "Login Form"; // handle login if( isset( $_POST['login'] ) ){ // we simple verify it from constant variable // if we need to verify the user from database , do this later // $user = new User ; // $user->isValidateUser( $name, $password ); if ( $_POST['username'] == ADMIN_USERNAME && $_POST['password'] == hasher($_POST['password'], ADMIN_PASSWORD ) ){ // register a session data $_SESSION['username'] = ADMIN_USERNAME ; // location to admin page header( "Location: index.php"); } else { // Login failed: display an error message to the user $results['errorMessage'] = "Incorrect username or password. Please try again."; require( TEMPLATE_PATH . "/loginForm.php" ); } } else { require( TEMPLATE_PATH . "/loginForm.php" ); } } function admin( ){ $results['pageTitle'] = "Administrator Page"; require( TEMPLATE_PATH . "/admin.php" ); } function logout( ){ $results['pageTitle'] = "Login Page"; unset( $_SESSION['username'] ); header( "Location: index.php "); }