当前位置:网站首页>PHP网站设计思路
PHP网站设计思路
2022-07-25 09:17:00 【目标是技术宅】
本文是对《PHP and MySQL Web Development》 第5版27章中项目的总结。
1.分析所需功能
列出主要功能模块:登录、注册、忘记密码、重设密码、登出、书签浏览、书签增加、书签删除、书签推荐
确定模块之间的先后转移关系:

2.分解所需模块
将模块的逻辑和内容分开:
- 内容写在function_form.php中
- 逻辑写在function.php中
设置专门文件用于:
- 连接数据库
- 输入数据检验
- 用户身份验证
- 浏览器输出
该项目所有文件可以分为以下几类:
1.数据定义文件:bookmarks.sql
用于建立所需数据库和数据表。
2.数据库连接文件:db_fns.php
用于连接数据库。保存着数据库用户的用户名和密码。
3.输入数据检验文件:user_auth_fns.php
用于检验输入数据。检验POST变量的值是否为空,email输入是否合法。
4.用户身份验证文件:user_auth_fns.php
用于验证用户是否能够注册、登录、修改密码、重设密码,以及用户是否已经登录。
由多个相应功能的函数组成,函数接收参数,连接数据库进行查询,返回查询结果或抛出异常。
5.书签操作文件:url_fns.php
用于对书签url进行查询、增加、删除与推荐。功能实现方法与上一类相似。
6.浏览器输出文件:output_fns.php
用于显示所有种类的浏览器输出:
网页名称、页内标题、各种表单、页内URL、网站信息、用户菜单、页尾。
每个函数实现一个种类部位的浏览器输出,函数根据需要可以接收参数,在HTML中插入php显示,也可以通过php输出HTML元素。
7.包含常被引用文件集的文件:bookmark_fns.php
8.9.表单文件及对应的逻辑处理文件
| 表单文件 | login.php | change_passwd_form.php | register_form.php | forgot_form.php | add_bm_form.php |
|---|---|---|---|---|---|
| 逻辑处理文件 | member.php | change_passwd.php | register_new.php | forgot_passwd.php | add_bms.php |
其它逻辑处理文件:
delete_bms.php、recommend.php
logout.php
表单文件的特点:
1.包含对bookmark_fns.php中output_fns.php文件中函数的引用(即一些浏览器输出函数)
2.对于登录后才能填写的表单,包含会话开始函数session_start()和检验用户是否已注册会话函数check_valid_user()。
逻辑处理文件的特点: 处理表单提交的数据
1.包含对bookmark_fns.php中output_fns.php文件中函数的引用(即一些浏览器输出函数)
2.包含会话开始函数session_start()。
3.接收POST变量的信息。
4.对表单是否填满、输入是否合法进行判断。
5.继而调用用户身份验证函数,如果验证通过,则显示相关信息。
6.捕捉所有抛出的异常,合理的显示在浏览器上。
3.主要模块关键思路
1.数据库连接模块:
连接数据库,返回mysqli对象。
function db_connect() {
$db = new mysqli('localhost', 'username', 'password', 'database');
if (!$db) {
throw new Exception("Could not connect to database server");
}else {
return $db;
}
}
2.输入数据检验模块:
//检验每个变量都有一个值
function filled_out($form_vars) {
foreach ($form_vars as $key => $value) {
if ((!isset($key)) || ($value == '')) {
return false;
}
}
return true;
}
//检验email
function valid_email($address) {
if(preg_match('/^[a-zA-Z0-9_\.\-][email protected][a-zA-Z0-9\-]+\.[a-zA-Z0-9\-\.]+$/', $address)) {
return true;
} else {
return false;
}
}
3.用户身份验证模块:
先连接数据库,然后执行查询语句(最好使用参数化查询方法),根据查询结果返回相应的值。
function login($username, $password) {
$conn = db_connect();
$results = $conn -> query("select * from user where username = '".$username."' and passwd = sha1('".$password."')");
if (!$results) {
throw new Exception('Could not log you in.');
}
if ($results -> num_rows > 0) {
return true;
} else {
throw new Exception('Could not log you in.');
}
}
4.登入登出模块:
//登入成功,设置session
$_SESSION['valid_user'] = $username;
//登出,先保存用户名,再注销,通过判断用户名是否存在给出不同提示
$old_user = $_SESSION['valid_user'];
unset($_SESSION['valid_user']);
$result_dest = session_destroy();
if (!empty($old_user)) {
if ($result_dest) {
echo 'Logged out.<br />';
}else {
echo 'Could not log you out.<br />';
}
}else {
echo 'You are not logged in ,so have not been logged out.<br />';
}
5.url推荐模块的sql语句:
如果两个用户收藏了相同的URL,那么可能他们有相似的爱好,就可以把其中一个用户收藏的其它URL推荐给另一个用户。
bookmark数据表一共有两列,bm_URL和username。
$query = "select bm_URL from bookmark where username in (select distinct(b2.username) from bookmark b1, bookmark b2 where b1.bm_URL = b2.bm_URL and b1.username != b2.username and b1.username = '".$valid_user."') and bm_URL not in (select bm_URL from bookmark where b1.username = '".$valid_user."') group by bm_URL having count(bm_URL)>".$popularity;
边栏推荐
- MySQL takes the query result as the data updated by update, and concatenates it after the original field data (Lej)
- [arm] Xintang nuc977 transplants wk2124 drive
- Difference between redis and mongodb (useful for interview)
- ActiveMQ -- kahadb of persistent mechanism
- Notes on in-depth analysis of C language 2
- Composition of the interview must ask items
- OmniPeek packet capturing tool
- [deep learning] overview | the latest progress of deep learning
- 对称式加密与非对称式加密的对比
- [SCADA case] myscada helps VIB company realize the modernization and upgrading of production line
猜你喜欢

What is steel grating?

Feiling ok1028a core board adapts to rtl8192cu WiFi module

Silicon Valley class lesson 11 - official account news and wechat authorization

Leetcode组合总和+剪枝

Live broadcast preview | how to build an enterprise cloud management platform in the cloudy era?

sql注入

The simplest sklearn environment configuration tutorial in the whole network (100% success)

Shell script

What version of Oracle10g single instance database is better to upgrade to? Ask for suggestions

2022-7-14 JMeter pressure test
随机推荐
Floating point number exploration
Ranking of data results in MySQL
Nacos搭建配置中心出现client error: invalid param. endpoint is blank
Mongodb installation and use
黑马程序员JDBC
『怎么用』代理模式
Guangzhou has carried out in-depth "100 day action" to check the safety of self built commercial houses, and more than 2 million houses have been checked in two months
使用nexus3发布yum私服(离线-内网)
抽象类和接口的区别(最详细)
Bi business interview with data center and business intelligence (I): preparation for Industry and business research
Silicon Valley classroom lesson 15 - Tencent cloud deployment
How to write the code of wechat applet implementation tab
Unity ugui interaction (new ideas)
Difference between redis and mongodb (useful for interview)
ActiveMQ -- kahadb of persistent mechanism
[C language] dynamic memory management, flexible array
C#语言和SQL Server数据库技术
通过robocopy对文件/夹进行复制
深入理解static关键字
Feiling ok1028a core board adapts to rtl8192cu WiFi module