The most commonly used custom function finishing _php techniques in PHP project development
Last Update:2017-01-19
Source: Internet
Author: User
<?php
Alert prompt
Function alert ($msg) {
echo "<script>alert (' $msg ');</script>";
}
Converts some predefined characters to HTML entities
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;
}
Precede predefined characters with backslashes, including single quotes, double quotes, backslashes, and NULL to secure the database
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;
}
Generates a random string containing uppercase, lowercase letters, and numbers
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;
}
Convert time stamp to a commonly used date format
function Trans_time ($timestamp) {
if ($timestamp < 1) echo ' Invalid Unix timestamp ';
else return date ("Y-m-d h:i:s", $timestamp);
}
Get IP
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;
}
Calculated time difference: The default return type is "minutes"
$old _time can only be timestamp, $return _type for H is hour, S is second
function Timelag ($old _time, $return _type= ' m ') {
if ($old _time < 1) {
echo ' Invalid Unix timestamp ';
}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;
}
}
Gets the URL address of the current page
function Url_this () {
$url = "http://" $_server ["Http_host"].$_server["Request_uri"];
$return _url = "<a href= ' $url ' > $url </a>";
return $return _url;
}
Jump function
function Url_redirect ($url, $delay = ' ") {
if ($delay = = ") {
echo "<script>window.location.href= ' $url ' </script>";
}else{
echo "<meta http-equiv= ' refresh ' content= ' $delay; url= $url '/> ';
}
}
}//end Func
?>