首页 > 代码库 > web典型应用

web典型应用

关于web 典型应用;

此处是以传智的论坛为模板写的项目;具有注册用户,登录,发帖,回复等功能;

第一个功能的实现:用户的注册

先是创建MySQL数据表:

1 create table user(
2     user_id int unsigned primary key auto_increment comment 主键ID,
3     user_name varchar(20) not null unique key comment 用户名,
4     user_password char(32) not null comment 用户密码
5 );

查看表结构:

技术分享

 

然后再根目录下创建目录文件:

 1 <?php
 2 /**
 3  * 项目初始化文件;
 4  */
 5 
 6 //1.设置响应头(设置文字编码)
 7 header("Content-type:text/html;charset=utf-8");
 8 //定义常量目录;
 9 //定义根目录常量;
10 define("DIR_ROOT", str_replace(\\,/,__DIR__)./);
11 //定义配置文件目录常量;
12 define("DIR_CONFIG", DIR_ROOT.config/);
13 //定义核心文件目录常量;
14 define("DIR_CORE",DIR_ROOT.core/);
15 //定义逻辑处理目录常量;
16 define("DIR_MODEL",DIR_ROOT.model/);
17 //定义模板文件目录常量;
18 define("DIR_VIEW",DIR_ROOT.view/);
19 //定义公开文件目录常量;
20 define("DIR_PUBLIC",/public);

加载初始视图文件index.php

1 <?php
2 //1,加载项目初始化文件;
3 include ./init.php;
4 
5 //2.加载视图文件;
6 include DIR_VIEW.index.html;

编写index.html文件:

  1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="zh-cn">
  3 <head>
  4     <meta http-equiv="Content-Type" content="text/html;charset=UTF-8" />
  5     <title>传智播客论坛_首页</title>
  6     <meta name="keywords" content="关键字列表" />
  7     <meta name="description" content="网页描述" />
  8     <link rel="stylesheet" type="text/css" href="<?php echo DIR_PUBLIC;?>/css/public.css" />
  9     <link rel="stylesheet" type="text/css" href="<?php echo DIR_PUBLIC;?>/css/index.css" />
 10     <style type="text/css"></style>
 11     <script type="text/javascript"></script>
 12 </head>
 13 <body>
 14     <div class="header_wrap">
 15         <div id="header" class="auto">
 16             <div class="logo">itcast</div>
 17             <div class="nav">
 18                 <a class="hover">首页</a>
 19             </div>
 20             <div class="serarch">
 21                 <form>
 22                     <input class="keyword" type="text" name="keyword" placeholder="搜索其实很简单" />
 23                     <input class="submit" type="submit" name="submit" value="" />
 24                 </form>
 25             </div>
 26             <div class="login">
 27                 <a>登录</a>&nbsp;
 28                 <a href = "./model/register.php">注册</a>
 29             </div>
 30         </div>
 31     </div>
 32     <div style="margin-top:55px;"></div>
 33     <div id="hot" class="auto">
 34         <div class="title">学院消息</div>
 35         <ul class="newslist">
 36             <!-- 20条 -->
 37             <li><a href="#">[PHP培训]</a> <a href="#">PHP16期基础班:微笑传递温暖</a></li>
 38             <li><a href="#">[Java培训]</a> <a href="#">2015年最后一班高薪直通车启动</a></li>
 39             <li><a href="#">[Android培训]</a> <a href="#">6期两个男生也公主抱?这画面..</a></li>
 40             <li><a href="#">[网页培训]</a> <a href="#">燃烧吧青春!火热上线—网页22期</a></li>
 41             <li><a href="#">[网络营销培训]</a> <a href="#">网络营销学院,3月强势进驻广州</a></li>
 42             <li><a href="#">[Android培训]</a> <a href="#">安卓5期就业班热闹开班典礼</a></li>
 43             <li><a href="#">[PHP培训]</a> <a href="#">探知广州入冬失败的原因—PHP15期</a></li>
 44             <li><a href="#">[网页培训]</a> <a href="#">颜值爆表的网页班开学啦,女神在这</a></li>
 45             <li><a href="#">[PHP培训]</a> <a href="#">PHP16期基础班:微笑传递温暖</a></li>
 46             <li><a href="#">[Java培训]</a> <a href="#">2015年最后一班高薪直通车启动</a></li>
 47             <li><a href="#">[Android培训]</a> <a href="#">6期两个男生也公主抱?这画面..</a></li>
 48             <li><a href="#">[网页培训]</a> <a href="#">燃烧吧青春!火热上线—网页22期</a></li>
 49             <li><a href="#">[网络营销培训]</a> <a href="#">网络营销学院,3月强势进驻广州</a></li>
 50             <li><a href="#">[Android培训]</a> <a href="#">安卓5期就业班热闹开班典礼</a></li>
 51             <li><a href="#">[PHP培训]</a> <a href="#">探知广州入冬失败的原因—PHP15期</a></li>
 52             <li><a href="#">[网页培训]</a> <a href="#">颜值爆表的网页班开学啦,女神在这</a></li>    
 53         </ul>
 54         <div style="clear:both;"></div>
 55     </div>
 56     <div class="box auto">
 57         <div class="title">
 58             国际足球
 59         </div>
 60         <div class="classList">
 61             <div style="padding:10px 0;">暂无子版块...</div>
 62         </div>
 63     </div>
 64     <div class="box auto">
 65         <div class="title">
 66             CBA
 67         </div>
 68         <div class="classList">
 69             <div style="padding:10px 0;">暂无子版块...</div>
 70             <div style="clear:both;"></div>
 71         </div>
 72     </div>
 73     <div class="box auto">
 74         <div class="title">
 75             NBA
 76         </div>
 77         <div class="classList">
 78             <div class="childBox new">
 79                 <h2><a href="#">A队</a> <span>(今日38)</span></h2>
 80                 帖子:1939539<br />
 81             </div>
 82             <div class="childBox old">
 83                 <h2><a href="#">B队</a> <span>(今日38)</span></h2>
 84                 帖子:1939539<br />
 85             </div>
 86             <div class="childBox lock">
 87                 <h2><a href="#">C队</a> <span>(今日38)</span></h2>
 88                 帖子:1939539<br />
 89             </div>
 90             <div class="childBox new">
 91                 <h2><a href="#">D队</a> <span>(今日38)</span></h2>
 92                 帖子:1939539<br />
 93             </div>
 94             <div style="clear:both;"></div>
 95         </div>
 96     </div>
 97     <div id="footer" class="auto">
 98         <div class="bottom">
 99             <a>传智播客</a>
100         </div>
101         <div class="copyright">Powered by itcast ?2015 itcast.cn</div>
102     </div>
103 </body>
104 </html>

效果如图:

技术分享

 

创建register.php文件

用于加载视图文件;

1 <?php 
2 //1加载项目初始化文件;
3 include ‘../init.php‘;
4 
5 //2.加载视图文件;
6 include DIR_VIEW .‘register.html‘;

编写register.html文件:

 1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 2 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="zh-cn">
 3 <head>
 4     <meta http-equiv="Content-Type" content="text/html;charset=UTF-8" />
 5     <title>传智播客论坛_注册页</title>
 6     <meta name="keywords" content="关键字列表" />
 7     <meta name="description" content="网页描述" />
 8     <link rel="stylesheet" type="text/css" href="http://www.mamicode.com/<?php echo DIR_PUBLIC; ?>/css/public.css" />
 9     <link rel="stylesheet" type="text/css" href="http://www.mamicode.com/<?php echo DIR_PUBLIC; ?>/css/register.css" />
10     <style type="text/css"></style>
11     <script type="text/javascript"></script>
12 </head>
13 <body>
14     <div class="header_wrap">
15         <div id="header" class="auto">
16             <div class="logo">itcast</div>
17             <div class="nav">
18                 <a class="hover">首页</a>
19                 <a>新帖</a>
20                 <a>话题</a>
21             </div>
22             <div class="serarch">
23                 <form>
24                     <input class="keyword" type="text" name="keyword" placeholder="搜索其实很简单" />
25                     <input class="submit" type="submit" name="submit" value="" />
26                 </form>
27             </div>
28             <div class="login">
29                 <a>登录</a>&nbsp;
30                 <a>注册</a>
31             </div>
32         </div>
33     </div>
34     <div style="margin-top:55px;"></div>
35     <div id="register" class="auto">
36         <h2>欢迎注册成为 传智播客BBS论坛会员</h2>
37         <form action = ‘register_deal.php‘ method="POST">
38             <label>用户名:<input type="text"  name="user_name" /><span>*6-10位,请使用英文字母、数字</span></label>
39             <label>密码:<input type="password"  name = "user_password1" /><span>*6-10位,请使用英文字母、数字,注意区分大小写</span></label>
40             <label>确认密码:<input type="password" name = "user_password2" /><span>*两次输入的密码必须一致</span></label>
41             <label>验证码:<input name="vcode" type="text"  /><span>*请输入下方验证码</span></label>
42             <img class="vcode" src="http://www.mamicode.com/<?php echo DIR_PUBLIC;?>/image/show_code.php.jpg" />
43             <div style="clear:both;"></div>
44             <input class="btn" type="submit" value="http://www.mamicode.com/确定注册" />
45         </form>
46     </div>
47     <div id="footer" class="auto">
48         <div class="bottom">
49             <a>传智播客</a>
50         </div>
51         <div class="copyright">Powered by itcast ?2015 itcast.cn</div>
52     </div>
53 </body>
54 </html>

 

当用户点击了注册按钮之后就跳转到以下的界面:

技术分享

 

 

 

完善regiser.html中的注册表;

 1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 2 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="zh-cn">
 3 <head>
 4     <meta http-equiv="Content-Type" content="text/html;charset=UTF-8" />
 5     <title>传智播客论坛_注册页</title>
 6     <meta name="keywords" content="关键字列表" />
 7     <meta name="description" content="网页描述" />
 8     <link rel="stylesheet" type="text/css" href="<?php echo DIR_PUBLIC; ?>/css/public.css" />
 9     <link rel="stylesheet" type="text/css" href="<?php echo DIR_PUBLIC; ?>/css/register.css" />
10     <style type="text/css"></style>
11     <script type="text/javascript"></script>
12 </head>
13 <body>
14     <div class="header_wrap">
15         <div id="header" class="auto">
16             <div class="logo">itcast</div>
17             <div class="nav">
18                 <a class="hover">首页</a>
19                 <a>新帖</a>
20                 <a>话题</a>
21             </div>
22             <div class="serarch">
23                 <form>
24                     <input class="keyword" type="text" name="keyword" placeholder="搜索其实很简单" />
25                     <input class="submit" type="submit" name="submit" value="" />
26                 </form>
27             </div>
28             <div class="login">
29                 <a>登录</a>&nbsp;
30                 <a>注册</a>
31             </div>
32         </div>
33     </div>
34     <div style="margin-top:55px;"></div>
35     <div id="register" class="auto">
36         <h2>欢迎注册成为 传智播客BBS论坛会员</h2>
37         <form action = ‘register_deal.php‘ method="POST">
38             <label>用户名:<input type="text"  name="user_name" /><span>*6-10位,请使用英文字母、数字</span></label>
39             <label>密码:<input type="password"  name = "user_password1" /><span>*6-10位,请使用英文字母、数字,注意区分大小写</span></label>
40             <label>确认密码:<input type="password" name = "user_password2" /><span>*两次输入的密码必须一致</span></label>
41             <label>验证码:<input name="vcode" type="text"  /><span>*请输入下方验证码</span></label>
42             <img class="vcode" src="<?php echo DIR_PUBLIC;?>/image/show_code.php.jpg" />
43             <div style="clear:both;"></div>
44             <input class="btn" type="submit" value="确定注册" />
45         </form>
46     </div>
47     <div id="footer" class="auto">
48         <div class="bottom">
49             <a>传智播客</a>
50         </div>
51         <div class="copyright">Powered by itcast ?2015 itcast.cn</div>
52     </div>
53 </body>
54 </html>

 

编写regiser_deal.php

 1 <?php 
 2 //1.加载项目初始化文件;
 3 include ‘../init.php‘;
 4 
 5 //2.链接数据库;
 6 /*$link = mysql_connect(‘localhost:3306‘,‘root‘,‘admin‘);
 7 mysql_query(‘set names utf8‘);
 8 mysql_query(‘use bbs‘);*/
 9 include DIR_CORE . ‘MYSQLDB.php‘;
10 
11 //3.接收数据;
12 $user_name = trim($_POST[‘user_name‘]);
13 $user_password1 = trim($_POST[‘user_password1‘]);
14 $user_password2 = trim($_POST[‘user_password2‘]);
15 $vcode = trim($_POST[‘vcode‘]);
16 
17 //4.判断数据合法性;
18 //判断用户名和密码是否为空;
19 if(empty($user_name)||empty($user_password1)|| empty($user_password2)){
20     //用户名非法,跳转;
21     //header(‘refresh:2;url = ./register.php‘);
22     //die(‘用户名或密码不能为空!请你重新注册!‘);
23     jump(‘./register.php‘,‘用户名或密码不能为空!请你重新注册!‘);
24 }
25 //判断用户名的长度;
26 if(strlen($user_name) < 6 || strlen($user_name) > 16){
27     //header(‘refresh:2;url = ./register.php‘);
28     //die(‘用户名在6到10位之间!请你重新注册!‘);
29     jump(‘./register.php‘,‘用户名在6到10位之间!请你重新注册!‘);
30 }
31 //判断两次数据的密码是否一致
32 if ($user_password1 !== $user_password2) {
33     //非法跳转;
34     //header(‘refresh:2;url=./register.php‘);
35     //die(‘两次密码输入的不一致!请你重新注册!‘);
36     jump(‘./register.php‘,‘两次密码输入的不一致!请你重新注册!‘);
37 }
38 //判断密码长度;
39 if(strlen($user_password1) < 6 || strlen($user_password1) >16){
40     //header(‘refresh:2;url =./register.php‘);
41     //die(‘密码在6到10位之间!,请你重新注册!‘);
42     jump(‘./register.php‘,‘密码在6到10位之间!,请你重新注册!‘);
43 }
44 //判断用户是否存在;
45 $sql = "select * from user where user_name=‘$user_name‘";
46 mysql_query($sql);
47 if (mysql_affected_rows() >0 ) {
48     # code...
49     //header("refresh:2;url=./reguster.php");
50     //die("你输入的用户名已经存在!请你重新注册!");
51     jump(‘./register.php‘,‘你输入的用户名已经存在!请你重新注册!‘);
52 }
53 //数据入库;
54 $user_password = md5($user_password1);
55 $sql = "insert into user values(null,‘$user_name‘,‘$user_password‘)";
56 //执行;
57 $result = mysql_query($sql);
58 if ($result) {
59     # code...
60     //header("refresh:2;url=./login.php");
61     //die("注册成功,2秒后跳转到登录界面!");
62     jump(‘./login.php‘,‘注册成功,2秒后跳转到登录界面!‘);
63 }else{
64     //入库失败;
65     //header("refresh:2;url=./login.php");
66     //die(‘发生未知错误注册失败!‘);
67     jump(‘./login.php‘,‘注册失败,2秒后跳转到登录界面!‘);
68 }

 

以下是我注册之后数据库表中的信息:

技术分享

 

以上就是实现了注册用户的具体功能;

web典型应用