标题:简单自定义函数
取消只看楼主
小罗bccn
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2018-3-29
结帖率:0
 问题点数:0 回复次数:1 
简单自定义函数

//随机生成4位验证码
function getCode($n=4){
    $strCode = 'qwertyuiopasdfghjklzxcvbnm1234567890QWERTYUIOPASDFGHJKLZMNXBCV';
    $res = '';
    for($i=0;$i<$n;$i++){
        $res .= $strCode{mt_rand(0,strlen($strCode)-1)};
    }
    return $res;
}
//echo getCode();
function getTime($del1,$del2,$del3){
    $arr = array('日','一','二','三','四','五','六');
    $week = date('w'); //0~6的数
    $time = date('今天是'."Y{$del1}m{$del2}d{$del3} 星期").$arr[$week];
    return $time;
}
//echo getTime('年','月','日').'<hr/>';


//获取客户端IP
function getClientIp(){
    $ip = NULL;
    if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
        $arr = explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']);
        $pos = array_search('unknown', $arr);
        if (false !== $pos) unset($arr[$pos]);
        $ip = trim($arr[0]);
    } elseif (isset($_SERVER['HTTP_CLIENT_IP'])) {
        $ip = $_SERVER['HTTP_CLIENT_IP'];
    } elseif (isset($_SERVER['REMOTE_ADDR'])) {
        $ip = $_SERVER['REMOTE_ADDR'];
    }     // IP地址合法验证
         $ip = (false !== ip2long($ip)) ? $ip : '0.0.0.0';
     return $ip;
}

//获取当前网页的url
function getUrl(){
    $pageURL = 'http';
    if (isset($_SERVER["HTTPS"]) && $_SERVER["HTTPS"] == "on") {
        $pageURL .= "s";
    }
    $pageURL .= "://";
    if ($_SERVER["SERVER_PORT"] != "80") {
        $pageURL .= $_SERVER["HTTP_HOST"] . ":" . $_SERVER["SERVER_PORT"] . $_SERVER["REQUEST_URI"];
    } else {
        $pageURL .= $_SERVER["HTTP_HOST"] . $_SERVER["REQUEST_URI"];
    }
    return $pageURL;
}


//将数组转成对象
function array2object($array) {
    if (is_array($array)) {
        $obj = new StdClass();
        foreach ($array as $key => $val){
            $obj->$key = $val;
        }
    }
    else {
        $obj = $array;
    }
    return $obj;
}
//调用的地方这样写
//$array=array('11',"22","test");
//p(array2object($array));

//将对象转化成数组
function object2array($object) {
    if (is_object($object)) {
        foreach ($object as $key => $value) {
            $array[$key] = $value;
        }
    }else {
        $array = $object;
    }
    return $array;
}
 
//PHP可阅读随机字符串
function readable_random_string($length = 6){
    $conso=array("b","c","d","f","g","h","j","k","l",
        "m","n","p","r","s","t","v","w","x","y","z");
    $vocal=array("a","e","i","o","u");
    $password="";
    srand ((double)microtime()*1000000);
    $max = $length/2;
    for($i=1; $i<=$max; $i++)
    {
        $password.=$conso[rand(0,19)];
        $password.=$vocal[rand(0,4)];
    }
    return $password;
}

//PHP防止SQL注入
function injCheck($sql_str) {
    $check = preg_match('/select|insert|update|delete|\'|\/\*|\*|\.\.\/|\.\/|union|into|load_file|outfile/', $sql_str);
    if ($check) {
        echo '非法字符!!';
        exit;
    } else {
        return $sql_str;
    }
}

搜索更多相关主题的帖子: function return array ip object 
2018-03-30 13:06
小罗bccn
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2018-3-29
得分:0 
新手上路,多多指教。
2018-03-30 13:18



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-485673-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.023340 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved