tp5 自定义函数

2019-08-04

   一、写自定义函数

      1、在项目的Common目录下新建common.php文件

      2、在该文件中写自己的函数,函数的写法跟普通PHP定义函数的方法是一样的,只是函数名个人建议不要写的太简单,跟系统的函数冲突了就不讲会不会出问题。我的函数举例:

<?php
function lock_url($txt, $key)
{
    $txt = $txt . $key;
    $chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-=+";
    $nh = rand(0, 64);
    $ch = $chars[$nh];
    $mdKey = md5($key . $ch);
    $mdKey = substr($mdKey, $nh % 8, $nh % 8 + 7);
    $txt = base64_encode($txt);
    $tmp = '';
    $i = 0;
    $j = 0;
    $k = 0;
    for ($i = 0; $i < strlen($txt); $i++) {
        $k = $k == strlen($mdKey) ? 0 : $k;
        $j = ($nh + strpos($chars, $txt[$i]) + ord($mdKey[$k++])) % 64;
        $tmp .= $chars[$j];
    }
    return urlencode(base64_encode($ch . $tmp));
}

二、模板中调用(使用自己时,用###)

例子:<a href="{:url('index/'.$controller)}?id={$rsp.pid|lock_url='var1','var2'}"

{/if}