Home Backend Development PHP Tutorial 一个简单的PHP网站结构

一个简单的PHP网站结构

Jun 23, 2016 pm 02:38 PM

一个简单的PHP网站结构

由于之前学习过asp.net mvc的网站开发,因此,在新接触一门新的语言时,也会自然或不自然地参考这种经历,然后做出网站。如果你也是刚开始接触php,并从最原始的方式开始写网站,那么,你可以参考下面一种做法,它也是经过参考和探索而形成的 一种可能的PHP网站结构:

一种PHP网站结构

其中,网站名即为项目文件夹,文件夹下分有css、js、images、includes、templates、up等等文件夹,其功能如下:

目录及功能列表

css

专门用来存放css文件,一般每个模块独立成一个css文件,如用户(user.css),产品(product.css)等

images

用来存放网站所需的图片,如网站logo,背景图处,增删查改的图标等

includes

存放业务逻辑的代码,每个模块独立成一个文件,如用户(users.php),产品(products.php)等,每个文件是各种业务逻辑的处理方法的集合,如增、删等

集中存放js代码,如提交前验证,改善用户体验等,每个模块自成一个文件,如用户(user.js)

templates

这个是模块,如每个网页均需要html头部和html尾部,可以写成header.php,footer.php,然后在每个页面上把头和尾包含(include或require)一次,即可统一网站风格

up 存放用户上传的图片,先按类别,再按时间分成多个文件,如/up/user/2012/2/12/xxxx.jpg

根目录下,还会有许多独立的php文件,这些是呈现给用户的php文件,如用户注册(register.php),登录(login.php),首页(index.php)等文件。

接下来,将更详细地说明每个文件夹下功能的实现方式:

(1)css文件夹

这个文件夹主要是把一个模块的所有css文件集中在一起,可以一处定义多处引用,这样当需要修改时,只需要改一处,比较方便修改和维护。

如上所示,commom.css即是通用的Css属性,如链接的颜色,input元素去边框,统一规定网站文字大小,还有常见的clearfix或透明处理等代码,一般是会放在网站模板的头部(/templates/header.php);home.css则主要用在首页上;jquery-ui-1.8.19.custom.css则是jquery的标准界面文件

(2)images文件夹

主要是集中存放网站会用到的一些图片资源,如网站不同规格的logo,背景图片,增删查改的图标,加载图标等等

(3)includes文件夹

主要是集中保存业务逻辑处理文件,每个模块独立成一个文件,这些文件使用时,都需要进行包含(include或require):

其中,

a)_logOn.php是登录状态栏,并有一些快捷菜单,算是快捷通道板:

b)baseConfigus.php则是数据库配置信息,是一些常量,方便访问数据库是统一代码,且不易错:

c)commom.php是通用方法,如验证是否登录,是否管理员,上传文件,显示分页等

d)users.php则用来处理与用户有关的业务逻辑,如修改用户信息,登录等

(4)js文件夹

功能结构与includes文件夹类似,此不多述。

(5)templates文件夹

用来存放网站模板,整个网站的风格在此统一:

其中,header.php定义网页的头部,每个基本页面开头都需要包含它(include /templates/header.php):

footer.php定义网页的尾部,每个基本页面的末尾也需要包含它:

其中可以包含回顶部,到底部的功能,或者加上网站流量统计代码,如51la

header_admin.php和footer_admin.php则是后台页面使用的模板,道理类似。

(6)up文件夹

这个文件夹保存用户上传的各种图片,如用户头像,产品头像等,各成一个文件夹:

在用户文件夹下面,按年月日进行保存。

(7)根目录下的php文件

这些文件,一般就是直接面向用户的,即呈现给用户看的,虽然业务逻辑主要在includes文件夹中集中保存,但页面中不免仍需要一些访问数据库的代码。这些文件的共同特点是,必须包含网站模板,以统一风格:

下面以用户登录(login.php)为例:

a)头部

b)尾部

c)中间

注意,这里主要是html语言写成的用户界面,需要一些构成:

css或js引用,form,提交前的有效性验证onsubmit="return validateLogin();",以及登录出错时保留用户之前输入的信息if($_SERVER['REQUEST_METHOD']=='POST'){print $_POST['name'];}等

d)登录提交(post方式)的处理,这里采用同一个页面进行处理

即提交时,仍跳转到当前页面,只是需要对提交方式进行判断(是get还是post),然后做出不同的处理:

其中Get方式时,若已经登录,则自动跳转到首页;若是Post方式时,时进行登录验证,验证成功则跳转到首页,失败时停留在当前页面。

(8).一些处理技巧:

a)借鉴C#的String.Format方法(保存在/includes/commom.php中):

使用时,很方便,如打印从数据库中读出的数据时:

b)由于以上一步a)的方式访问时,关于引号(单引号和双引号的问题),有时难以进行转义或转义时即出错,因此,可能灵活使用jquery来进行事件绑定(而非html元素事件的方式),如删除前的确认提示:

本文由bluesky原创,具有独立产权,如转载请注明出处。

Statement of this Website
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

Explain the concept of late static binding in PHP. Explain the concept of late static binding in PHP. Mar 21, 2025 pm 01:33 PM

Article discusses late static binding (LSB) in PHP, introduced in PHP 5.3, allowing runtime resolution of static method calls for more flexible inheritance.Main issue: LSB vs. traditional polymorphism; LSB's practical applications and potential perfo

Framework Security Features: Protecting against vulnerabilities. Framework Security Features: Protecting against vulnerabilities. Mar 28, 2025 pm 05:11 PM

Article discusses essential security features in frameworks to protect against vulnerabilities, including input validation, authentication, and regular updates.

How to send a POST request containing JSON data using PHP's cURL library? How to send a POST request containing JSON data using PHP's cURL library? Apr 01, 2025 pm 03:12 PM

Sending JSON data using PHP's cURL library In PHP development, it is often necessary to interact with external APIs. One of the common ways is to use cURL library to send POST�...

Customizing/Extending Frameworks: How to add custom functionality. Customizing/Extending Frameworks: How to add custom functionality. Mar 28, 2025 pm 05:12 PM

The article discusses adding custom functionality to frameworks, focusing on understanding architecture, identifying extension points, and best practices for integration and debugging.

Describe the SOLID principles and how they apply to PHP development. Describe the SOLID principles and how they apply to PHP development. Apr 03, 2025 am 12:04 AM

The application of SOLID principle in PHP development includes: 1. Single responsibility principle (SRP): Each class is responsible for only one function. 2. Open and close principle (OCP): Changes are achieved through extension rather than modification. 3. Lisch's Substitution Principle (LSP): Subclasses can replace base classes without affecting program accuracy. 4. Interface isolation principle (ISP): Use fine-grained interfaces to avoid dependencies and unused methods. 5. Dependency inversion principle (DIP): High and low-level modules rely on abstraction and are implemented through dependency injection.

How does session hijacking work and how can you mitigate it in PHP? How does session hijacking work and how can you mitigate it in PHP? Apr 06, 2025 am 12:02 AM

Session hijacking can be achieved through the following steps: 1. Obtain the session ID, 2. Use the session ID, 3. Keep the session active. The methods to prevent session hijacking in PHP include: 1. Use the session_regenerate_id() function to regenerate the session ID, 2. Store session data through the database, 3. Ensure that all session data is transmitted through HTTPS.

See all articles