A simple PHP site structure

Source: Internet
Author: User
A simple PHP site structure

As a result of the previous study of the Web site development of ASP. NET MVC, it is natural or unnatural to refer to this experience when new contacts are made to a new language. If you are also just beginning to touch PHP and start writing the site from the most primitive way, then you can refer to one of the following approaches, which is also a possible PHP site structure that has been developed by reference and exploration:

A PHP web site structure

Where the site name is the project folder, the folder under the CSS, JS, images, includes, templates, up and so on, its functions are as follows:

List of directories and features

Store user uploaded pictures, first by category, then by time into multiple files, such as/up/user/2012/2/12/xxxx.jpg

CSS

dedicated to storing CSS files, typically each module is independent of a CSS file, such as user (USER.CSS), product (PRODUCT.CSS), etc.

images

used to store the images required for the site, such as website logo, background map, and other changes to the icon, etc.

includes

The code that holds the business logic, each module is separate into a file, such as user (users.php), product (products.php), etc., each file is a collection of various business logic processing methods, such as add, delete, etc.

Centrally store JS code, such as pre-commit validation, improved user experience, etc., each module becomes a file, such as user (User.js)

templates

This is a module that requires HTML headers and HTML for each page The tail, can be written header.php,footer.php, and then on each page the head and tail contains (include or require) once, you can unify the site style

up

root directory, there will be many separate PHP files, these are presented to the user's PHP files, such as user registration (register.php), login (login.php), Home (index.php) and other files.

Next, you'll explain in more detail how the features are implemented under each folder:

(1) CSS folder

This folder is mainly a module of all the CSS files together, you can define multiple references in one place, so when the need to modify, only need to change one place, more convenient to modify and maintain.

As shown above, COMMOM.CSS is a common CSS property, such as the color of the link, the input element to the border, the uniform specification of the site text size, as well as common clearfix or transparent processing code, usually placed in the site template head (/templates/ header.php); Home.css is mainly used on the first page; JQUERY-UI-1.8.19.CUSTOM.CSS is the standard interface file for jquery.

(2) Images folder

Mainly focus on the site will be used to some of the image resources, such as the site of different specifications of the logo, background images, add and delete check the icon, load icon and so on

(3) Includes folder

The main focus is to save the business logic processing files, each module is separate into a file, these files need to be included (include or require) when used:

which

A) _logon.php is the login status bar, and there are some shortcut menus, which are shortcut channel boards:

b) baseconfigus.php is the database configuration information, is a number of constants, easy access to the database is unified code, and not easy to wrong:

c) commom.php is a common method, such as verifying whether to log in, whether administrator, upload file, display paging, etc.

D) users.php is used to deal with user-related business logic, such as modifying user information, login, etc.

(4) JS folder

The functional structure is similar to the includes folder, which is not described in more.

(5) Templates folder

Used to store site templates, the entire site style is unified here:

Where header.php defines the head of the page, each basic page needs to contain it (include/templates/header.php):

Footer.php defines the end of the page, and it needs to be included at the bottom of each basic page:

Which can include back top, to the bottom of the function, or add website traffic statistics code, such as 51la

Header_admin.php and footer_admin.php are the templates used in background pages, which are similar.

(6) Up folder

This folder to save users upload a variety of images, such as user picture, product avatar, etc., into a folder:

Under the user folder, save by month date.

(7) php files under the root directory

These files, in general, are directly user-oriented, which is presented to the user, although the business logic is mainly stored in the includes folder, but the page will inevitably need some code to access the database. The common feature of these files is that the site template must be included to unify the style:

The following is an example of user login (login.php):

A) head

b) Tail

c) Intermediate

Note that this is primarily a user interface written in the HTML language and requires some composition:

CSS or JS reference, form, validation before submission onsubmit= "return Validatelogin ();", and the information that the user entered before the login error is retained if ($_server[' Request_method ']== ' POST ') {print $_post[' name '];} such as

d) Processing of login submission (post mode), which is processed by the same page

That is, when committing, still jumps to the current page, just need to judge the way of submission (is get or post), and then make different processing:

Where get mode, if already logged in, then automatically jump to the first page, if the post mode, when the login verification, verification success jumps to the first page, failed to stay in the current screen.

(8). Some processing techniques:

A) refer to C # 's String.Format method (saved in/includes/commom.php):

When used, it is convenient, such as when printing data that is read from a database:

b) As a result of the above step A, when accessing the quotation marks (single and double quotation marks), sometimes it is difficult to escape or escape error, it is possible to use jquery for event binding (not HTML element events), such as the confirmation prompt before deletion:

This article by BlueSky original, with independent property rights, such as reproduced please indicate the source.

  • 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.