PHP项目开发中最常用的自定义函数整理
时间:2019-11-12 14:16 来源/作者:PHP教程网
-
<?php
-
-
function alert($msg){
-
echo "<script>alert('$msg');</script>";
-
}
-
-
function d_htmlspecialchars($string) {
-
if(is_array($string)) {
-
foreach($string as $key => $val) {
-
$string[$key] = d_htmlspecialchars($val);
-
}
-
} else {
-
$string = str_replace('&', '&', $string);
-
$string = str_replace('"', '"', $string);
-
$string = str_replace(''', ''', $string);
-
$string = str_replace('<', '<', $string);
-
$string = str_replace('>', '>', $string);
-
$string = preg_replace('/&(#\d;)/', '&\1', $string);
-
}
-
return $string;
-
}
-
-
function d_addslashes($string, $force = 0) {
-
if(!$GLOBALS['magic_quotes_gpc'] || $force) {
-
if(is_array($string)) {
-
foreach($string as $key => $val) $string[$key] = d_addslashes($val, $force);
-
}
-
else $string = addslashes($string);
-
}
-
return $string;
-
}
-
-
function randstr($length) {
-
$hash = '';
-
$chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz';
-
$max = strlen($chars) - 1;
-
mt_srand((double)microtime() * 1000000);
-
for($i = 0; $i < $length; $i++) {
-
$hash .= $chars[mt_rand(0, $max)];
-
}
-
return $hash;
-
}
-
-
function trans_time($timestamp){
-
if($timestamp < 1) echo '无效的Unix时间戳';
-
else return date("Y-m-d H:i:s",$timestamp);
-
}
-
-
function get_ip() {
-
if ($_SERVER["HTTP_X_FORWARDED_FOR"])
-
$ip = $_SERVER["HTTP_X_FORWARDED_FOR"];
-
else if ($_SERVER["HTTP_CLIENT_IP"])
-
$ip = $_SERVER["HTTP_CLIENT_IP"];
-
else if ($_SERVER["REMOTE_ADDR"])
-
$ip = $_SERVER["REMOTE_ADDR"];
-
else if (getenv("HTTP_X_FORWARDED_FOR"))
-
$ip = getenv("HTTP_X_FORWARDED_FOR");
-
else if (getenv("HTTP_CLIENT_IP"))
-
$ip = getenv("HTTP_CLIENT_IP");
-
else if (getenv("REMOTE_ADDR"))
-
$ip = getenv("REMOTE_ADDR");
-
else
-
$ip = "Unknown";
-
return $ip;
-
}
-
-
-
function timelag($old_time,$return_type='m'){
-
if($old_time < 1){
-
echo '无效的Unix时间戳';
-
}else{
-
switch($return_type){
-
case 'h':
-
$type = 3600; break;
-
case 'm':
-
$type = 60; break;
-
case 's':
-
$type = 1; break;
-
case '':
-
$type = 60; break;
-
}
-
$dif = round( (time()-$old_time)/$type ) ;
-
return $dif;
-
}
-
}
-
-
function url_this(){
-
$url = "http://".$_SERVER ["HTTP_HOST"].$_SERVER["REQUEST_URI"];
-
$return_url = "<a href='$url'>$url</a>";
-
return $return_url;
-
}
-
-
function url_redirect($url,$delay=''){
-
if($delay == ''){
-
echo "<script>window.location.href='$url'</script>";
-
}else{
-
echo "<meta http-equiv='refresh' content='$delay;URL=$url' />";
-
}
-
}
-
}
-
-
?>
相关文章
热门资讯