PHP Faux Blog Garden Personal blog (1) _php tutorial on database and interface design

Source: Internet
Author: User
Tags crypt
Self-study PHP for more than half a year, intermittent, but eventually still determined my idea, will PHP continue, so write this PHP blog is to find a stable php work, do not ask for a high salary, but seek a place to take shelter. I can read most of the English documents, people are not stupid, love to learn, interested can contact! Come in good faith! qq:240382473
I will publish all the key codes and documentation in 3-5 times, all the styles of the blog background are applied to the blog park!

Description

1. The MVC architecture is not fully adopted, but this is the idea. Because it is not possible to write a very stable MVC architecture.
2. jquery Ajax is rarely used because it is not particularly familiar, the use of it is not comfortable, the message can be used Ajax, no problem.
3. There are several common classes, the other code is handwritten, there is insufficient place please point out, thank you very much.
4. You are welcome to criticize and guide, but please give your reasons.

To get started: Look at the database schema

The engines of these tables are myisam to facilitate access. (The yellow key denotes the primary key; the Blue Diamond represents a non-empty field; a white diamond represents a null field) the links in the diagram represent only a potential relationship between them and cannot be associated at the time of operation. Because the search engine is MyISAM. Therefore, it is necessary to combine queries and multi-table operations.

I will pick the most important post, Category 2 tables in the special fields detailed description, other said important.
Post
post_id
category_id varchar (10) This is the classification used to index posts, and here the category_id is a string type, so you can set up multiple categories for each blog post.

Type varchar (20) This field is used to distinguish between essays (post), articles (article), and Diaries (diary), and also can be set to Postdraft, Articledraft;

Visiable Blog is visible

Other characters commonly used segments such as title, content, creation time, last change time, number of views, number of reviews, tags, allow comments, and some reserved fields.

Category
Parent, Count_child_number, Count_parent_number for later expansion
Type can set the category of albums, posts, journals, respectively
Other characters commonly used segments such as name, description, creation time, visibility
Comment
Address User IP
User_agent User Browser type

Other fields 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

Background directory:

Background Directory Description:

Assert holds various resources js,css,image
Class stores our classes commonly used classes such as database operations classes, paging classes, and most of our model ...
Extention Store Some rich editors with extensions like MCE
Config to store our configuration information
Templates storage of all templates (not using Smarty)
Upload store photos and other documents
The admin root directory will have some similar controllers such as index.php, post.php, article.php, photo.php

Let's see admin/config/config.php first.
Copy CodeThe 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,) = = 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 < $i + +) {
$salt. = substr ("./abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz0123456789", Mt_rand (0, 63), 1);
}
return + char string (char hash & 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 a few functions.

The __autoload () function loads all classes in the admin/class/
A 88-bit irreversible password is encrypted with the Hasher () function, and the login process is verified with the constant and hasher () function in the config.php.

Come and see our admin/index.php backstage controller This Controller home page shows some blog related data
Copy CodeThe 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, does 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 pattern is learned from a foreigner!

The principle is:
First we load our config.php, initialize the session variable, get the value of $action this important variable;
Then we determine the value of $action and $username, if the user is not logged in and the user name is empty, return to the login page;
If the user correctly entered the user name and password, then register a session variable $username, and then jump to the main page index.php, then we will call the default $action admin (), This function will load a template admin.php, with the array of variables $results [' PageTitle '], and our background blog style framework.
If the user enters the wrong input, the prompt message is given.

At the heart of this design concept is give {action} then {do something}
We'll see it repeatedly in the code that follows.

This is the background of the blog frame style, from the blog Park copy, using table layout, compatible, can customize other styles, simple, practical, extensible, perfect background framework.

This style is equally compatible in other browsers, and when I write this blog post, I have done some of the functions. Next: Implement essays, articles, diaries, and the crud they classify.


PS: These operations have not yet used Ajax, because I am not familiar with Ajax.

http://www.bkjia.com/PHPjc/328067.html www.bkjia.com true http://www.bkjia.com/PHPjc/328067.html techarticle Self-study PHP for more than half a year, intermittent, but finally determined my idea, will PHP continue, so write this PHP blog is to find a stable php work, do not ask for wages ...

  • Related Article

    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.