<? Php
// Alert prompt
Function alert ($ msg ){
Echo "<script> alert ('$ msg'); </script> ";
}
// Convert some predefined characters into HTML Objects
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;
}
// Add a backslash before a predefined character, including single quotation marks, double quotation marks, backslash, and NULL, to protect database security
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;
}
// Generate a random string that contains uppercase and lowercase letters and numbers
Function randstr ($ length ){
$ Hash = '';
$ Chars = 'abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxy ';
$ Max = strlen ($ chars)-1;
Mt_srand (double) microtime () * 1000000 );
For ($ I = 0; $ I <$ length; $ I ++ ){
$ Hash. = $ chars [mt_rand (0, $ max)];
}
Return $ hash;
}
// Convert the timestamp to a common date format
Function trans_time ($ timestamp ){
If ($ timestamp <1) echo 'invalid Unix timestamp ';
Else return date ("Y-m-d H: I: s", $ timestamp );
}
// Obtain the IP address
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;
}
// Calculation time difference: the default return type is minute"
// $ Old_time can only be a timestamp. $ return_type indicates that h is an hour, and s is a 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;
}
}
// Obtain the URL 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
?>