zoukankan      html  css  js  c++  java
  • 使用thinkPHP做注册程序的实例

    登录界面:

    数据库和数据表的结构

    具体的操作步骤如下:

    第一步:入口文件index.php内容 (此文件基本是属于固定的格式)

    <?php
    define('THINK_PATH','./ThinkPHP/');
    define('APP_NAME','MyApp');
    define('APP_PAHT','./MyApp/');
    require_once THINK_PATH.'ThinkPHP.php';
    $app=new App();
    $app->run();
    ?>

    第二步:Active文件夹中的IndexAction.class.php文件内容

    <?php
    class IndexAction extends Action
    {
     public function Index()
     {
      $this->display();//渲染到模板index.html
     }
     
     // 生成验证码  
     public function verify()//这是一个固定的格式
     {  
      import("ORG.Util.Image");  
      Image::buildImageVerify();  
     }  
     
     //检验验证码是否正确  
     public function verifyCheck()
     {     
      if (md5($_POST['verifyTest']) != Session::get('verify'))
      {  
       die('验证码错误');  //如果验证码不对就退出程序
      } 
     }   
     
      function insert()
      {
      header('Content-Type:text/html; charset=utf-8');//防止出现乱码
      $this->verifyCheck();//调用本类的函数,
      $Pagemodel = D("user");
      $vo = $Pagemodel->create();
      if(false === $vo) die($Pagemodel->getError());
      $topicid = $Pagemodel->add(); //add方法会返回新添加的记录的主键值
      if($topicid) echo "数据库添加成功";
      else throw_exception("数据库添加失败");
     }
    }
    ?>

    第三步:写模板文件,也就是写LIB文件夹中的HTML文件

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>无标题文档</title>
      <style type="text/css">
        #form1
     {
      250px;
      height:250px;
      margin:20px auto;
      border:1px #039 solid;
      padding:20px 20px;
     }
      </style>
      <script type='text/javascript'>
     function freshVerify()
     {  
       document.getElementById('verifyImg').src='__URL__/verify/'+Math.random();  
     }  
      </script>  
    </head>
    <body>
      <form name="form1" id="form1" method="post" action="__URL__/insert">
        注册帐号:<br /><br />
        帐号:<input type="text" name="user" id="user" maxlength="16" /><br /><br />
        密码:<input type="password" name="password" id="password" maxlength="16" /><br /><br />
        Q&nbsp;&nbsp;Q:<input type="text" name="qq" id="qq" maxlength="16" /><br /><br />
       
        验证码:<input type='text' name='verifyTest' size="5"> 
        <img style='cursor:pointer' title='刷新验证码' src='__URL__/verify' id='verifyImg' onClick='freshVerify()'/> <br /><br />
          
        <input type="submit" name="btn1" id="btn1" value="提交" />
        <input type="reset" name="btn2" id="btn2" value="重置" />
      </form>
    </body>
    </html>
    注意:

    1、也就是一个form,action="__URL__/insert"表示提交到当前Action类(即IndexAction.class.php文件中)的insert函数;

    2、此模板(静态网页)中的各个name要与user数据表的各个字段是一样的名字,否则在insert函数中数据不能自动进库。

    3、验证码的刷新由静态网页负责。值相等由IndexAction类的verifyCheck()负责。

    第四步:写Model类,在model目录中,文件名为:UserModel.class.php

    <?php
      class UserModel extends Model//User与数据库中数据表的名字相同
      {
       var $_validate=array  //自动验证
       (
       array('user','require','账号不能为空',1),  //1表示必须验证
       array('qq','number','QQ号必须是数字,注册失败!',2),//2表示不为空时要验证
       array('user','','此帐号己经存在!',0,'unique','add')  //不能有同时账号出现
       );   
       var $_auto=array
       (
             array('password','md5','add','function'),  //密码用md5加密后填入数据表中
             array('create_time','time','add','function')  //在增加时自动将时间擢填入表中
       );
      }
    ?>

    注解:

    1、文件名,类名必须用user,因为数据库中对应的是user表;

    2、其实只要写一个框架就行了:

      class UserModel extends Model
      {

      }

    但为什么还要var $_validate=array()和var $_auto=array()呢?那是因为:

    var $_validate=array()是自动验证的意思,var $_auto=array()是自动填充的意思。

    自动验证就是验证数据的格式是否正确,自动填充就是你不输入的值,它自动给你灌进去,比如'create_time'建立时间,我们在模板中没有这个,但这里它就自动进库了。

  • 相关阅读:
    桥梁模式
    OpenGL中的功能与OSG对应功能 (摘)
    STL源码学习----lower_bound和upper_bound算法[转]
    [转载]GMT地形数据总结
    关于OPenGL和OSG的矩阵 (转)
    GEOS库 介绍 (转)
    一个小巧的C++Log输出到文件类 (转)
    如何优化,让电脑真正快起来(转)
    数据库的应用——直接从内存中读取osg节点 (转)
    OSG 实现跟随节点的相机(转)
  • 原文地址:https://www.cnblogs.com/licanhui/p/11174035.html
Copyright © 2011-2022 走看看