当前位置:首页 > CMS教程 > Thinkphp > 列表

thinkphp框架 实现注册登录程序

发布:smiling 来源: PHP粉丝网  添加日期:2015-09-30 14:24:09 浏览: 评论:0 

thinkphp框架有感(一)

一:每一个html表单都需要在控制器中进行调用,否则无法对表单中的数据进行控制。

二:html中,<form>标签里的action地址应指向控制器中所对应的方法名。

三:在浏览器中如果要通过某个控制器访问视图,应访问到该控制器中的各个方法。

如:http://localhost/tpone/index.php/Home/Index/register

不知为何我直接访问http://localhost/tpone/index.php/无法实现页面跳转的功能,但访问到方法层时就可以.

四:model

并不是必须要建,我目前对model不是很收悉,这次的程序完成品中,我最后是将所有的操作数据库代码写在了controller中,没有建立model.

五:多用var_dump()进行调试,它可以判断出具体问题到底出在哪。

六:之前整个程序中困扰我最厉害的是各个文件的调用路径和配置问题,好吧,其实我现在这方面也比较模糊.

这次”thinkphp框架实现注册登录程序“的最终成品一共由四个文件组成,一个控制器,三个html页面.

具体功能:

1,注册页面输入信息,信息会被写入后台数据库.

2,登录界面输入正确的用户名和密码后会跳转到新的页面将数据库中所有注册人的信息展示出来(除了密码).

3,登录和注册页面之间可通过按钮相互跳转.

IndexController.class.php文件:

  1. <?php 
  2. namespace Home\Controller; 
  3. use Think\Controller; 
  4.  
  5. class IndexController extends Controller 
  6.  
  7. //登录控制器 
  8.  public function Index() 
  9.  { 
  10.   $this->display(index);//加载view/index目录下的index.html文件 
  11.  } 
  12.  
  13.   function login() 
  14.   { 
  15.   $User =  M("User");    //  进行数据库表的实例化,User是我的表名。得到$User对象。 
  16.  
  17.   $data['user'] = $_POST["user"]; 
  18.   $data['password'] = $_POST["password"]; 
  19.  
  20.     if($data['user'] == "" || $data['password'] == "")   
  21.     {   
  22.     echo "<script>alert('请输入用户名或密码!'); history.back();</script>";  //js程序,弹出对话框显示信息,并返回上个页面 
  23.     }   
  24.     else  
  25.     $result = $User ->query("select user,password from user where user = '$_POST[user]' and password = '$_POST[password]'");//调用$User对象的query方法来执行sql语句。和数据库中的用户名和密码比对,看是否正确 
  26.         if($result)   
  27.         {   
  28. $this->success('登录成功''result'); 
  29.         }   
  30.         else   
  31.         {   
  32.             $this->error('用户名或密码不正确!');   
  33.         }   
  34.     } 
  35.   } 
  36.  
  37.   //-------------------------------------------------------------------------------------------------------------- 
  38.  
  39.   //注册控制器 
  40.   public function Register() 
  41.  { 
  42.   $this->display(register);//加载view/index目录下的register.html文件 
  43.  } 
  44.  
  45.     function zhuce() 
  46.   { 
  47.   $User =  M("User");    //  进行数据库表的实例化,User是我的表名。得到$User对象。 
  48.  
  49.   $data['user'] = $_POST["user"]; 
  50.   $data['password'] = $_POST["password"]; 
  51.   $data['passwordcheck'] = $_POST["passwordcheck"]; 
  52.   $data['sex'] = $_POST["sex"]; 
  53.   $data['subject'] = $_POST["subject"]; 
  54.  
  55.   if($data['user'] == "" || $data['password'] == ""|| $data['passwordcheck'] == ""|| $data['sex'] == ""|| $data['subject'] == "")   
  56.     {   
  57.         echo "<script>alert('请填写完整!');history.back(); </script>";  //js程序,弹出对话框显示信息,并返回上个页面 
  58.     }   
  59.   else  
  60. if($data['password'] == $data['passwordcheck'])     //密码和确认密码是否一致 
  61.     { 
  62.    $result = $User ->query("select user from user where user = '$_POST[user]'"); 
  63. if($result)    //如果为真,则已存在 
  64. echo "<script>alert('用户名已存在');history.back();</script>"
  65. else 
  66. $User->add($data); 
  67. if($User)  
  68. echo "<script>alert('注册成功!');history.back();</script>"
  69.         else  
  70. throw_exception("数据库添加失败"); 
  71.     } 
  72. else 
  73. echo "<script>alert('密码不一致!');history.back();</script>"
  74.   } 
  75.  
  76.   //-------------------------------------------------------------------------------------------------------------- 
  77.  
  78.   //登录后显示结果控制器 
  79.  
  80.   function result() 
  81.   { 
  82.  $User =  M("User"); 
  83.  $data=$User->select(); 
  84.  $this->data=$data
  85.  $this->display(); 
  86.   } //phpfensi.com 
  87.  
  88. ?> 

index.html文件:

  1. <!DOCTYPE 
  2. html 
  3. PUBLIC 
  4. "-//W3C//DTD XHTML 1.0 Transitional//EN" 
  5. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
  6. <html 
  7. xmlns="http://www.w3.org/1999/xhtml"> 
  8. <head> 
  9. <meta 
  10. http-equiv="Content-Type" 
  11. content="text/html; charset=utf-8" 
  12. /> 
  13. <title>登录页面</title> 
  14.  
  15. <style 
  16. type="text/css"> 
  17.  
  18.  
  19. #form1 
  20.  { 
  21.  
  22. width:250px; 
  23.  
  24. height:250px; 
  25.  
  26. margin:20px 
  27. auto; 
  28.  
  29. border:1px 
  30. #039 
  31. solid; 
  32.  
  33. padding:20px 
  34. 20px; 
  35.  } 
  36.  
  37. </style> 
  38.  
  39. <script 
  40. type='text/javascript'> 
  41.  function 
  42. freshVerify() 
  43.  { 
  44.  
  45.  
  46.  document.getElementByIdx_x_x_xx('verifyImg').src='__URL__/verify/'+Math.random(); 
  47.  
  48.  } 
  49.  
  50.  
  51. </script> 
  52.  
  53. </head> 
  54. <body> 
  55.  
  56. <form 
  57. name="form1" id="form1" method="post" 
  58. action="__URL__/login"> 
  59.  
  60. 登录帐号:<br/><br/> 
  61.  
  62. 姓名:<input type="text" name="user"/> 
  63. <br/><br/> 
  64.  
  65. 密码:<input type="password" 
  66. name="password"/> 
  67.  <br/><br/> 
  68.  
  69. <input type="submit" 
  70. name="submit" value="登录"/> 
  71.  
  72. <a href="register.html">注册</a>  
  73.  
  74. </form> 
  75. </body> 
  76. </html> 

register.html文件:

  1. <!DOCTYPE 
  2. html 
  3. PUBLIC 
  4. "-//W3C//DTD XHTML 1.0 Transitional//EN" 
  5. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
  6. <html 
  7. xmlns="http://www.w3.org/1999/xhtml"> 
  8. <head> 
  9. <meta 
  10. http-equiv="Content-Type" 
  11. content="text/html; charset=utf-8" 
  12. /> 
  13. <title>注册页面</title> 
  14.  
  15. <style 
  16. type="text/css"> 
  17.  
  18.  
  19. #form1 
  20.  { 
  21.  
  22. width:250px; 
  23.  
  24. height:250px; 
  25.  
  26. margin:20px 
  27. auto; 
  28.  
  29. border:1px 
  30. #039 
  31. solid; 
  32.  
  33. padding:20px 
  34. 20px; 
  35.  } 
  36.  
  37. </style> 
  38.  
  39. <script 
  40. type='text/javascript'> 
  41.  function 
  42. freshVerify() 
  43.  { 
  44.  
  45.  
  46.  document.getElementByIdx_x_x_xx('verifyImg').src='__URL__/verify/'+Math.random(); 
  47.  
  48.  } 
  49.  
  50.  
  51. </script> 
  52.  
  53. </head> 
  54. <body> 
  55.  
  56. <form 
  57. name="form1" id="form1" method="post" action="__URL__/zhuce">  
  58.  
  59. 注册帐号:<br/><br/>  
  60.  
  61. 姓名:<input type="text" name="user" maxlength="16"/><br /><br />  
  62. 性别:<input type="radio" name="sex"  value="男" maxlength="16" />男  
  63.  
  64.  
  65.  <input 
  66. type="radio" 
  67. name="sex"  value="女" maxlength="16"/><br /><br />  
  68. 专业:<input 
  69. type="text" 
  70. name="subject"  maxlength="16"/><br /><br />  
  71.  
  72.  
  73. 密码:<input type="password" 
  74. name="password" 
  75.  maxlength="16"/><br /><br />  
  76. 确认密码:<input type="password"name="passwordcheck" maxlength="16"/><br /><br />     
  77.  
  78. <input type="submit" name="btn1"     
  79. id="btn1" 
  80. value="提交" /> 
  81. <a 
  82. href="index.html">登录</a> 
  83.  
  84. </form> 
  85. </body> 
  86. </html> 

result.thml文件:

  1. <html> 
  2. <head> 
  3. <title>信息页面</title> 
  4. </head> 
  5. <body> 
  6. <b>目前已注册的用户</b><br/><br />  
  7. <foreach name="data" item="vo">   
  8.  
  9. <li>用户名:{$vo.user}&nbsp;&nbsp; 
  10.  
  11.  
  12. 性别:{$vo.sex}&nbsp;&nbsp; 
  13. 专业:{$vo.subject}<br/><br />  
  14. </li> 
  15.  
  16. </foreach> 
  17.  
  18. </body> 
  19. </html>

Tags: thinkphp框架 thinkphp注册登录

分享到: