自学PHP大半年多了,断断续续地,但是最终还是坚定了我的想法,将PHP继续下去,所以写这个PHP的博客是为了找个稳定的 PHP工作,不求工资多高,但求一收留之地
自学PHP大半年多了,断断续续地,但是最终还是坚定了我的想法,将PHP继续下去,所以写这个PHP的博客是为了找个稳定的 PHP工作,不求工资多高,但求一收留之地。我能看懂大部分英语文档,人不蠢,爱学习,有兴趣地可以联系下!有诚意的来吧!qq:240382473
复制代码 代码如下:
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 $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 */
}
}
复制代码 代码如下:
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 ");
}