PHP imitation blog Park, personal blog (1)

Source: Internet
Author: User
Tags crypt
I have a bachelor's degree and I have learned PHP for more than half a year. I have been on and off, but eventually I have strengthened my idea to continue with PHP, so I wrote this PHP blog to find a stable PHP job. I don't want to pay a high salary, but I want to keep it. I can understand most of the English documents ,... syntaxHighlighter. all (); I have a bachelor's degree and I have taught myself PHP for more than half a year. it is intermittent, but in the end I have strengthened my idea and will continue to use PHP, so I wrote this PHP blog to find a stable PHP job. I don't want to pay a high salary, but I want to keep it. I can understand most of the English documents. I am not stupid and love to learn. if you are interested, please contact me! Come on with sincerity! Qq: 240382473
I will publish all the key code and documentation three to five times, and apply all the styles in the blog background to the blog garden!
Note:
1. the MVC architecture is not fully used, but the idea is like this. Because it cannot write a very stable MVC architecture.
2. jquery ajax is rarely used. it is not very familiar with jquery ajax and cannot be used freely. it is no problem to use AJAX for message Books.
3. there are several public classes and other codes are hand-written. if there are any deficiencies, please point out them a lot. thank you very much.
4. you are welcome to give criticism and guidance, but please give your reasons.
 
Let's get down to the truth: first look at the database architecture




The engines of these tables are MYISAM, which facilitates access. (The Yellow key represents the primary key, the Blue Diamond represents the non-empty field, and the white diamond represents the null field.) the link in the diagram only indicates a potential relationship between them, cannot be associated during operation. because the search engine is MyISAM. Therefore, joint queries and multi-table operations are required.
I will pick out the most important post and special fields in the category two tables for detailed description. Others are important.
Post:
Post_id
Category_id varchar (10) is used to index the classification of a blog. here, category_id is also a string type, so you can set multiple categories for each blog.
Type varchar (20) is used to distinguish post, article, and diary. It can also be set to postDraft and articleDraft;
Whether the visiable blog is visible
Other common fields such as title, content, creation time, last modification time, browsing times, comments, tags, comments allowed, and reserved fields.
Category:
Parent, count_child_number, count_parent_number for future extension
You can set the category of the album, blog, and Diary respectively.
Other common fields such as name, description, creation time, and visibility
Comment:
Address user IP address
User_agent user browser type

Other fields...
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
Background Directory:



 

Background directory description:
Assert stores various resources such as js, css, and image.
Class stores common classes of our classes, such as database operations, paging classes, and most of our models...
Extention stores some extensions such as the mce rich editor
Config stores our configuration information
Templates stores all templates (smarty is not used)
Upload stores photos and other files.
The admin root directory contains some similar controllers such as index. php, post. php, article. php, and photo. php.
 
Let's first look at admin/config. php
 
Ini_set ("display_errors", true );
Date_default_timezone_set ("Asia/Shanghai ");
// Root and direcotry separate
Define ('Ds', DIRECTORY_SEPARATOR );
Define ('root', 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. 'class'. 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.
The _ autoload () function loads all classes in admin/class /.
The hasher () function is used to encrypt an 88-bit irreversible password. during login, the constant and hasher () function in config. php are used for verification.
Let's take a look at our admin/index. php background controller. the controller homepage displays some blog-related data.
 
 
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 ");
}
 
 
 
This design model is learned from a foreigner!
The principle is:
First, we load our config. php, initialize the session variable, and obtain the value of $ action, an important variable;
Then we judge the values of $ action and $ username. if the user is not logged on and the user name is empty, return to the logon page;
If you have entered the user name and password correctly, register a session variable $ username and jump to the homepage index. php, then we will call the default $ action admin (), this function will load a template admin. php; there is an array variable $ results ['pagetitle'] and our background blog style framework.
If an error is entered, a prompt is displayed.
 
The core of this design concept is give {action} then {do something}
We will see it repeatedly in the code below.
 
 
This is the framework style of the blog background. it is copied from the blog garden and adopts the table layout. it is compatible and can be customized with other styles. it is simple, practical, and scalable, perfect background framework.



 

This style is also compatible with other browsers. I have completed some of the functions when I write this blog. Next Article: implement essays, articles, diaries, and CRUD for their classification.
Ps: these operations have not yet used ajax, because I am not familiar with ajax.

 


From warcraft



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.