简单自定义函数
//随机生成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;
}
}