PHP+jQuery 注册模块的改进(三):更新到Smarty3.1


Posted in PHP onOctober 14, 2014

Smarty3.1X( 最新版本 3.1.19) 比起Smarty2.x修改了不少特性。我把这个模块使用Smarty3.1.18 ( 下载地址http://www.smarty.net/files/Smarty-3.1.18.zip )重新修改了一遍,是项目文件和目录看起来更干净更有调理。

把Smarty压缩包中的libs文件夹拷贝至模块根目录下,然后根目录创建init.inc.php:

<?php

/**

    file:init.inc.php Smarty对象的实例化及初始化文件

*/
/* *********************Smarty设置*********************** */

//根目录路径方式,用于Smarty设置

define("ROOT",str_replace("\\","/",dirname(__FILE__))."/");
require ROOT.'libs/Smarty.class.php';

$smarty = new Smarty();
//Smarty3设置默认路径

$smarty ->setTemplateDir(ROOT.'templates/')

        ->setCompileDir(ROOT.'templates_c/')

        ->setPluginsDir(ROOT.'plugins/')

        ->setCacheDir(ROOT.'cache/')

        ->setConfigDir(ROOT.'configs');
$smarty->caching = false;

$smarty->cache_lifetime = 60*60*24; //模版缓存有效时间为1天

$smarty->left_delimiter = '<{';

$smarty->right_delimiter = '}>';
/***********************************************************/
//根目录url方式

$PHP_SELF=$_SERVER['PHP_SELF'];

$ROOT_URL='http://'.$_SERVER['HTTP_HOST'].substr($PHP_SELF,0,strrpos($PHP_SELF,'/')+1);

define(ROOT_URL,$ROOT_URL);
//模版目录url方式

define("Template_Dir",$ROOT_URL.'templates');

创建初始化文件中出现的templates,templates_c,plugins,cache,configs文件夹。

修改的文件都比较相似,而且也非常easy,这里列出register.html和register.php文件的修改。

register.html是注册的前台页面,路径是/templates/register.html

<!DOCTYPE html>

<html>

<head>

<meta charset="utf-8">

<title>注册页面</title>

<link href="<{$Template_Dir}>/css/common.css"  rel="stylesheet" type="text/css">

<link href="<{$Template_Dir}>/css/register.css"  rel="stylesheet" type="text/css">
<script src="<{$Template_Dir}>/js/jquery-1.8.3.min.js"></script>

<script src="<{$Template_Dir}>/js/register.js"></script>

<!--邮箱下拉-->

<script src="<{$Template_Dir}>/js/emailup.js"></script>

</head>
<body>

<{*导入header.html*}>

<{include file="header.html"}>
<!-- 内容区 -->

<div id="container">
    <!--注册区-->

    <div id="register">
        <!-- 注册表单 -->

        <form id="register-form" action="register_chk.php" method="post">

            

            <!-- 用户名 -->        

            <!-- placeholder HTML5的属性,提供提示信息。输入字段为空时显示,并会在字段获得焦点时消失 -->

            <div class="ipt fipt">

                <input type="text" name="uname" id="uname" value="" placeholder="输入用户名"  autocomplete="off" />

                <!--提示文字-->

                <span id="unamechk"></span>

            </div>
            <!-- email -->            

            <div class="ipt">

                <input type="text" name="uemail" id="uemail" value="" placeholder="常用邮箱地址" autocomplete="off" /><span id="uemailchk"></span><ul class="autoul"></ul>

            </div>
            <!-- 密码 -->

            <div class="ipt">

                <input type="password" name="upwd" id="upwd" value="" placeholder="设置密码" /><div class="upwdpic"><span id="upwdchk"></span><img id="pictie" /></div>

            </div>

            

            <!-- 重复密码 -->

            <div class="ipt">

                <input type="password" name="rupwd" id="rupwd" value="" placeholder="确认密码" /><span id="rupwdchk"></span>

            </div>
            <!--验证码-->

            <div class="ipt iptend">

                <input type='text' id='yzm' name='yzm' placeholder="验证码" autocomplete="off" />

                <img id='yzmpic' src='valcode.php?num=<{showval}>' style="cursor:pointer" alt="验证码" title="验证码">

                <a style="cursor:pointer" id='changea'>

                    <img id="refpic" src="<{$Template_Dir}>/images/ref.jpg" alt="刷新验证码">

                </a>

                <span id='yzmchk'></span>

            </div>
            <!-- 提交 -->

            <button type="button" id="sub">立即注册</button>
            <!-- 服务条款 -->

            <span class="fuwu">

                <input type="checkbox" name="agree" id="agree" checked="checked">

                <label for="agree">我同意  <a href="#">" 服务条款  "</a> 和  <a href="#">" 网络游戏用户隐私权保护和个人信息利用政策 "</a>

                </label>

            </span>
        </form>
    </div>
</div>

</body>

</html>

register.php:

<?php
session_start();
require_once 'init.inc.php';
//设置模版目录,用于模版页头部引用CSS、JS、Images

$smarty->assign("Template_Dir",Template_Dir);
$smarty->display('register.html');

同时扩充了生成验证码插件,路径是/plugins/function.showval.php

<?php
//生成验证码

function smarty_function_showval($params,$smarty){
    $num = "";

    for($i=0;$i<4;$i++){
        $tmp = rand(1,15);

        if ($tmp > 9) {

            switch ($tmp) {

                case(10):

                    $num .= 'a';

                    break;

                case(11):

                    $num .= 'b';

                    break;

                case(12):

                    $num .= 'c';

                    break;

                case(13):

                    $num .= 'd';

                    break;

                case(14):

                    $num .= 'e';

                    break;

                case(15):

                    $num .= 'f';

                    break;

            }

        } else {

            $num .= $tmp;

        }    

    }
    $mdnum = md5($num);

    $_SESSION['num'] = $num;

    $_SESSION['mdnum'] = $mdnum;
    //写在session之后

    return $mdnum;

}
$_SESSION['num'] = smarty_function_showval($params,$smarty);

$_SESSION['mdnum'] = md5(smarty_function_showval($params,$smarty));

注意插件的命名:

文件名要放在根目录的plugins目录下,命名规则是 function.函数名.php,文件中函数的命名规则是 smarty_function_函数名($params,$smarty),其中第一个参数是传递给模板的关联数组,第二个参数是接收自动传入的smarty对象,函数要有返回值。

更多代码见:https://github.com/dee0912/myGit

PHP 相关文章推荐
提示Trying to clone an uncloneable object of class Imagic的解决
Oct 27 PHP
Php中文件下载功能实现超详细流程分析
Jun 13 PHP
PHP中操作ini配置文件的方法
Apr 25 PHP
php截取字符串函数substr,iconv_substr,mb_substr示例以及优劣分析
Jun 10 PHP
php简单smarty入门程序实例
Jun 11 PHP
PHP的Laravel框架中使用消息队列queue及异步队列的方法
Mar 21 PHP
php 实现进制相互转换
Apr 07 PHP
删除PHP数组中头部、尾部、任意元素的实现代码
Apr 10 PHP
PHP7中I/O模型内核剖析详解
Apr 14 PHP
Laravel Validator自定义错误返回提示消息并在前端展示
May 09 PHP
php array_chunk()函数用法与注意事项
Jul 12 PHP
Laravel框架实现文件上传的方法分析
Sep 29 PHP
PHP+jQuery 注册模块的改进(一):验证码存入SESSION
Oct 14 #PHP
PHP+jQuery 注册模块开发详解
Oct 14 #PHP
推荐一款PHP+jQuery制作的列表分页的功能模块
Oct 14 #PHP
php中动态修改ini配置
Oct 14 #PHP
php中的ini配置原理详解
Oct 14 #PHP
9段PHP实用功能的代码推荐
Oct 14 #PHP
五款PHP代码重构工具推荐
Oct 14 #PHP
You might like
用mysql内存表来代替php session的类
2009/02/01 PHP
php 文件上传类代码
2011/08/06 PHP
shopex主机报错误请求解决方案(No such file or directory)
2011/12/27 PHP
set_include_path和get_include_path使用及注意事项
2013/02/02 PHP
php实现对短信验证码发送次数的限制实例讲解
2021/03/04 PHP
javascript 动态参数判空操作
2008/12/22 Javascript
最精简的JavaScript实现鼠标拖动效果的方法
2015/05/11 Javascript
JS中mouseover和mouseout多次触发问题如何解决
2016/06/06 Javascript
利用jquery实现下拉框的禁用与启用
2016/12/07 Javascript
jQuery元素选择器实例代码
2017/02/06 Javascript
基于jQuery实现文字打印动态效果
2017/04/21 jQuery
jsonp跨域请求详解
2017/07/13 Javascript
实现一个完整的Node.js RESTful API的示例
2017/09/29 Javascript
基于js 字符串indexof与search方法的区别(详解)
2017/12/04 Javascript
vue项目实现github在线预览功能
2018/06/20 Javascript
详解Vue中watch的详细用法
2018/11/28 Javascript
js中怎么判断两个字符串相等的实例
2019/01/17 Javascript
Python编程语言的35个与众不同之处(语言特征和使用技巧)
2014/07/07 Python
Python中pip安装非PyPI官网第三方库的方法
2015/06/02 Python
python实现扫描日志关键字的示例
2018/04/28 Python
Python关于excel和shp的使用在matplotlib
2019/01/03 Python
浅谈python函数调用返回两个或多个变量的方法
2019/01/23 Python
浅析python 中大括号中括号小括号的区分
2019/07/29 Python
Python在OpenCV里实现极坐标变换功能
2019/09/02 Python
python中的subprocess.Popen()使用详解
2019/12/25 Python
Keras自定义实现带masking的meanpooling层方式
2020/06/16 Python
Python内置方法和属性应用:反射和单例(推荐)
2020/06/19 Python
Python包资源下载路径报404解决方案
2020/11/05 Python
全面介绍python中很常用的单元测试框架unitest
2020/12/14 Python
Jo Malone美国官网:祖玛珑香水
2017/03/27 全球购物
比利时的在线灯具店:Lampen24.be
2019/07/01 全球购物
以太网Ethernet IEEE802.3
2013/08/05 面试题
Linux开机引导的步骤是什么
2015/10/19 面试题
同意迁入证明模板
2014/10/26 职场文书
成本会计实训报告
2014/11/05 职场文书
python接口测试返回数据为字典取值方式
2022/02/12 Python