PHP Imitation Blog Garden Personal blog (1) database and interface Design _php example

Source: Internet
Author: User
Tags comments crypt
self-study php more than half a year, intermittent, but finally determined my idea, will continue to PHP, so write this PHP blog is to find a stable php work, do not ask for a high salary, but to find a shelter. I can read most English documents, people are not stupid, love to learn, have the interest to contact! Come in good faith! qq:240382473
I will distribute all the key code and document descriptions 3-5 times, all the styles in the blog background are applied to the blog park!

Description

1. The MVC architecture is not fully adopted, but the idea is this. Because it is not possible to write a stable MVC architecture.
2. Almost no jquery Ajax because it is not particularly familiar with the application is not comfortable, the message can be used Ajax, no problem.
3. There are several common classes, other codes are handwritten, there are insufficient places please point out, thank you very much.
4. Welcome criticism and guidance, but please give your reasons.

Go to the database schema

The engines of these tables are myisam to facilitate access. (The yellow key denotes primary key; The Blue Diamond denotes a non-empty field; white diamond represents a null field) the links in the diagram only indicate that they have a potential relationship and cannot be associated at operation time. Because search engines are MyISAM. Therefore, joint queries and multiple table operations are required.

I'll pick the most important post, category the special fields in 2 tables in detail, others say important.
Post
post_id
category_id varchar (10) This is the classification used to index posts, where category_id is also a string type, so you can set up multiple categories for each blog post.

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

Visiable Blog is visible

Other commonly-available fields such as title, content, creation time, last change time, number of views, number of comments, tags, allow comments, and some reserved field.

Category
Parent, Count_child_number, Count_parent_number for later expansion
Type can be set separately for albums, blogs, journal categories
Other commonly-seen fields 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 catalog:

Background Catalog Description:

Assert storage of various resources js,css,image
class to store our classes commonly used classes such as database operation class, pagination class, and most of our model ...
Extention Store rich editors with extensions like MCE
Config to store our configuration information
Templates to store all templates (no smarty used)
Upload is storing 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 Code code as follows:

<?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 ($encdata, 0, substr) = = 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 the classes in the admin/class/
Using the Hasher () function to encrypt a 88-bit irreversible password, the login process is validated with constants and Hasher () functions in config.php.

Come to see our admin/index.php background controller This Controller home page shows some of the blog's relevant data
Copy Code code as follows:

<?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 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 pattern is learned from a foreigner!

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

The core of this design philosophy is that give {action} then {do something}
We'll see it again and again in the code that follows.

This is the background of the blog frame style, from the blog Park copy come, using tabular layout, compatible, customizable other styles, simple, practical, extensible, perfect background frame.

This style is equally compatible with other browsers, and I've done some of the work while writing this blog post. Next: The implementation of essays, articles, diaries and their classification of crud.


PS: These operations are not using AJAX, because I am not familiar with Ajax.

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.