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> 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="<?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> 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.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> 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 }?
以下是我注冊之后數據庫表中的信息:
?
以上就是實現了注冊用戶的具體功能;
轉載于:https://www.cnblogs.com/YeYunRong/p/6280203.html
總結
- 上一篇: 1.液晶显示器简介
- 下一篇: java 区位码转中文_Java根据汉字