一个简单的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原创,具有独立产权,如转载请注明出处。

PHP is used to build dynamic websites, and its core functions include: 1. Generate dynamic content and generate web pages in real time by connecting with the database; 2. Process user interaction and form submissions, verify inputs and respond to operations; 3. Manage sessions and user authentication to provide a personalized experience; 4. Optimize performance and follow best practices to improve website efficiency and security.

PHP uses MySQLi and PDO extensions to interact in database operations and server-side logic processing, and processes server-side logic through functions such as session management. 1) Use MySQLi or PDO to connect to the database and execute SQL queries. 2) Handle HTTP requests and user status through session management and other functions. 3) Use transactions to ensure the atomicity of database operations. 4) Prevent SQL injection, use exception handling and closing connections for debugging. 5) Optimize performance through indexing and cache, write highly readable code and perform error handling.

Using preprocessing statements and PDO in PHP can effectively prevent SQL injection attacks. 1) Use PDO to connect to the database and set the error mode. 2) Create preprocessing statements through the prepare method and pass data using placeholders and execute methods. 3) Process query results and ensure the security and performance of the code.

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP makes it easy to create interactive web content. 1) Dynamically generate content by embedding HTML and display it in real time based on user input or database data. 2) Process form submission and generate dynamic output to ensure that htmlspecialchars is used to prevent XSS. 3) Use MySQL to create a user registration system, and use password_hash and preprocessing statements to enhance security. Mastering these techniques will improve the efficiency of web development.

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SublimeText3 Linux new version
SublimeText3 Linux latest version

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

Dreamweaver Mac version
Visual web development tools

Atom editor mac version download
The most popular open source editor