Home  >  Article  >  Backend Development  >  PHP Imitation Blog Park Personal Blog (1) Database and Interface Design_PHP Tutorial

PHP Imitation Blog Park Personal Blog (1) Database and Interface Design_PHP Tutorial

WBOY
WBOYOriginal
2016-07-21 15:00:29886browse

I have been studying PHP by myself for more than half a year, intermittently, but in the end I have firmed up my mind and continued PHP. Therefore, I write this PHP blog to find a stable PHP job. I don’t ask for a high salary, but I want to be accepted. land. I can understand most English documents. I am not stupid and I love learning. If you are interested, please contact me! Come if you are sincere! qq:240382473
I will release all key codes and documentation instructions in 3-5 times, and all styles in the blog backend will be applied to Blog Park!

Note:

1. Not fully adopted MVC architecture, but the concept is like this. Because it is not possible to write a very stable MVC architecture.
2. I almost never use JQUERY AJAX because I am not particularly familiar with it and can’t use it freely. You can use AJAX for the guestbook, no problem.
3. There are several public classes, and other codes are all handwritten. Please point out any shortcomings. Thank you very much.
4. Criticism and guidance are welcome, but please give your reasons.

Back to business: Let’s look at the database architecture first

The engines of these tables are MYISAM, which is convenient for access. (The yellow key represents the primary key; the blue diamond represents a non-empty field; the white diamond represents a null field) The links in the picture only represent a potential relationship between them and cannot be associated during operation. Because the search engine is MyISAM . Therefore, joint queries and multi-table operations are required.

I will select the most important special fields in the post and category tables to explain in detail, and the others will be mentioned as important.
post:
post_id
category_id varchar(10) This is the category used to index blog posts. The category_id here is also a string type, so multiple categories can be set for each blog post.

type varchar(20) This field is used to distinguish post, article, and diary; it can also be set to postDraft, articleDraft;

visiable Whether the blog post is visible

Other commonly used fields such as title, content, creation time, last modification time, number of views, number of comments, tags, allowed comments, and some reserved fields.

category:
parent, count_child_number, count_parent_number for future expansion
type can set the categories of photo albums, blog posts, and diaries respectively
Other common fields such as name, description, creation time, visibility
comment:
address User IP
user_agent User browser type

Other fields are slightly...
Server architecture
PHP5.4.2 + MYSQL 5.523 + APACHE 2.2.22 + Windows NT ARIST-PC 6.1 build 7600 (Windows 7 Home Basic Edition) i586 (local)
Blog architecture

Backend directory:

Backend directory description:

assert stores various resources js, css, image
class stores our common classes such as database operation classes, paging classes, and our Most models. . .
extension stores rich editors with extensions such as mce
config stores our configuration information
templates stores all templates (smarty is not used)
upload stores photos and other files
admin There will be some similar controllers in the root directory such as index.php, post.php, article.php, photo.php

Let’s take a look at admin/config/config.php first
Copy code The code is as follows:

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 */
}
}


Here we define some basic constants and several functions.

__autoload() function loads all classes in admin/class/
Uses hasher() function to encrypt an 88-bit irreversible password. The login process uses constants and hasher() in config.php function to verify.

Let’s take a look at our admin/index.php backend controller. This controller homepage displays some blog related data
Copy the code The code is as follows :

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 ");
}


I learned this design pattern from a foreigner!

The principle is:
First we load our config.php, initialize the session variable and obtain the value of the important variable $action;
Then we judge the value of $action and $username. If the user is not logged in and the username is empty, return to the login page;
If the user After entering the username and password correctly, register a session variable $username, and then jump to the main page index.php. At this time, we will call the default $action admin(). This function will load a template admin.php; which contains array variable $results['pageTitle'], and our background blog style framework.
If the user inputs incorrectly, a prompt message will be given.

The core of this design concept is, give {action} then {do something}
We will see it repeatedly in the following code.

This is the frame style of the blog backend. It is copied from the blog park. It uses a table layout, is compatible, can be customized with other styles, is simple, practical, scalable, and a perfect backend frame.

This style is also compatible in other browsers. When writing this blog post, I have completed some functions. Next article: Implement CRUD for essays, articles, diaries and their classification.


ps: These operations have not used ajax because I am not familiar with ajax yet.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/328067.htmlTechArticleI have been learning PHP by myself for more than half a year, intermittently, but in the end I firmed up my idea and continued with PHP. So I write this PHP blog to find a stable PHP job without asking for salary...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn