?
function GetIP () {//Get IP
if ($_server["http_x_forwarded_for"])
$ip = $_server["Http_x_forwarded_for"]; OSPHP.com.CN
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;
}
?>
<?php
function DateAdd ($date, $int, $unit = "D") {//Time increase (can also be improved as time and minutes can be increased, time to fill)
$DATEARR = Explode ("-", $date);
$value [$unit] = $int; OSPHP.COm.CN
Return date ("Y-m-d", Mktime (0,0,0, $DATEARR [1] + $value [' m '], $DATEARR [2] + $value [' d '], $DATEARR [0] + $value [' Y ']));
}
function Getweekday ($date) {//Calculate the date given is the day of the week
$DATEARR = Explode ("-", $date);
Return Date ("W", Mktime (0,0,0, $DATEARR [1], $DATEARR [2], $DATEARR [0])); OsPHP.COM.CN
}
?>
?
function Check_date ($date) {//check date is legal date
$DATEARR = Explode ("-", $date);
if (is_numeric ($dateArr [0]) && is_numeric ($DATEARR [1]) && is_numeric ($DATEARR [2])) {
return Checkdate ($DATEARR [1], $DATEARR [2], $DATEARR [0]);
}
return false;
}
function Check_time ($time) {//Check time is legal time
$TIMEARR = Explode (":", $time);
if (is_numeric ($timeArr [0]) && is_numeric ($TIMEARR [1]) && is_numeric ($TIMEARR [2])) {// Open Source Code OSPhP.COm.CN
if ($timeArr [0] >= 0 && $timeArr [0] <=) && ($TIMEARR [1] >= 0 && $timeArr [1] <= 59) && ($TIMEARR [2] >= 0 && $timeArr [2] <= 59))
return true;
Else
return false;
}
return false;
}
function DateDiff ($date 1, $date 2, $unit = "") {//Time comparison function, return two date difference a few seconds, minutes, hours or days
Switch ($unit) {
Case ' s ':
$dividend = 1;
Break
Case ' I ':
$dividend = 60;
Break
Case ' H ':
$dividend = 3600;
Break
Case ' d ':
$dividend = 86400;
Break
Default
$dividend = 86400;
}
$time 1 = strtotime ($date 1);
$time 2 = strtotime ($date 2);
if ($time 1 && $time 2)
return (float) ($time 1-$time 2)/$dividend;
return false;
}
?>
PHP redirect
?
Method One: Header ("Location:index.php");
Method Two: echo "<scrīpt>window.location =" $PHP _self ";</scrīpt>"; PHP Open Source Code
Method Three: Echo "<meta http-equiv=" Refresh "content=" 0; Url=index.php ">";
?>
Get Visitor Browser
?
function Browse_infor ()
{
$browser = ""; $browserver = "";
$Browsers =array ("Lynx", "MOSAIC", "AOL", "Opera", "JAVA", "Macweb", "Webexplorer", "OmniWeb");
$Agent = $GLOBALS ["Http_user_agent"];
For ($i =0 $i <=7; $i + +)
{
if (Strpos ($Agent, $Browsers [$i])
{
$browser = $Browsers [$i];
$browserver = "";
}
}
if (Ereg ("Mozilla", $Agent) &&!ereg ("MSIE", $Agent))
{
$temp =explode ("(", $Agent); $Part = $temp [0];
$temp =explode ("/", $Part); $browserver = $temp [1]; Open Source Code OSPhP.COm.CN
$temp =explode ("", $browserver); $browserver = $temp [0];
$browserver =preg_replace ("/([D.] +)/"," 1 ", $browserver);
$browserver = "$browserver"; OSPHP.COM.CN
$browser = "Netscape Navigator";
}
if (Ereg ("Mozilla", $Agent) && ereg ("Opera", $Agent))
{
$temp =explode ("(", $Agent); $Part = $temp [1]; OSPHP.COM.Cn Open Source
$temp =explode (")", $Part); $browserver = $temp [1];
$temp =explode ("", $browserver); $browserver = $temp [2];
$browserver =preg_replace ("/([D.] +)/"," 1 ", $browserver);
$browserver = "$browserver";
$browser = "Opera";
}
if (Ereg ("Mozilla", $Agent) && ereg ("MSIE", $Agent))
{
$temp = Explode ("(", $Agent); $Part = $temp [1];
$temp = Explode (";", $Part); $Part = $temp [1];
$temp = Explode ("", $Part); $browserver = $temp [2];
$browserver =preg_replace ("/([D.] +)/"," 1 ", $browserver);
$browserver = "$browserver"; OSPHP.COM.Cn Open Source
$browser = "Internet Explorer";
}
if ($browser!= "")
{
$browseinfo = "$browser $browserver";
}
Else
{
$browseinfo = "Unknown";
}
return $browseinfo;
}
Call Method $browser=browseinfo (), return results directly
?>
Get the visitor's operating system
?
function Osinfo () {
$os = "";
$Agent = $GLOBALS ["Http_user_agent"];
if (eregi (' win ', $Agent) && strpos ($Agent, ' 95 ')) {//Open source OSPhP.COM.CN
$os = "Windows 95";
}
ElseIf (Eregi (' Win 9x ', $Agent) && strpos ($Agent, ' 4.90 ')) {
$os = "Windows ME";
}
ElseIf (' Win ', $Agent) && ereg (' Eregi ', $Agent)) {
$os = "Windows 98";
}
ElseIf (eregi (' win ', $Agent) && eregi (' NT 5.0 ', $Agent)) {
$os = "Windows 2000";
}
ElseIf (eregi (' win ', $Agent) && eregi (' NT ', $Agent)) {
$os = "Windows NT";
}
ElseIf (eregi (' win ', $Agent) && eregi (' NT 5.1 ', $Agent)) {
$os = "Windows XP";
}
ElseIf (' Win ', $Agent) && ereg (' Eregi ', $Agent)) {//php Open source code
$os = "Windows 32";
}
ElseIf (eregi (' Linux ', $Agent)) {
$os = "Linux";
}
ElseIf (eregi (' Unix ', $Agent)) {
$os = "Unix";
}
ElseIf (Eregi (' Sun ', $Agent) && eregi (' OS ', $Agent)) {
$os = "SunOS";
}
ElseIf (eregi (' IBM ', $Agent) && eregi (' OS ', $Agent)) {
$os = "IBM os/2";
}
ElseIf (eregi (' Mac ', $Agent) && eregi (' PC ', $Agent)) {
$os = "Macintosh";
}
ElseIf (eregi (' PowerPC ', $Agent)) {
$os = "PowerPC";
}
ElseIf (eregi (' AIX ', $Agent)) {
$os = "AIX";
}
ElseIf (eregi (' HPUX ', $Agent)) {
$os = "HPUX";
}
ElseIf (eregi (' NetBSD ', $Agent)) {
$os = "NetBSD";
}
ElseIf (eregi (' BSD ', $Agent)) {
$os = "BSD";
}
ElseIf (Ereg (' OSF1 ', $Agent)) {
$os = "OSF1";
}
ElseIf (Ereg (' IRIX ', $Agent)) {
$os = "IRIX";
}
ElseIf (eregi (' FreeBSD ', $Agent)) {
$os = "FreeBSD";
}
if ($os = = ") $os =" Unknown ";
return $os;
}
Call Method $os=os_infor ();
?>
File Format class
?
$mime _types = Array (
' gif ' => ' image/gif ',
' jpg ' => ' image/jpeg ',
' JPEG ' => ' image/jpeg ',
' Jpe ' => ' image/jpeg ',
' BMP ' => ' image/bmp ',
' PNG ' => ' image/png ',
' tif ' => ' Image/tiff ',//osphp.com.cn
' TIFF ' => ' Image/tiff ',
' Pict ' => ' image/x-pict ',
' Pic ' => ' image/x-pict ',
' pct ' => ' image/x-pict ',
' tif ' => ' Image/tiff ',
' TIFF ' => ' Image/tiff ',
' PSD ' => ' image/x-photoshop ',
' swf ' => ' Application/x-shockwave-flash ',
' JS ' => ' application/x-javascrīpt ',
' PDF ' => ' application/pdf ',
' ps ' => ' application/postscrīpt ',
' EPS ' => ' application/postscrīpt ',
' AI ' => ' application/postscrīpt ',
' WMF ' => ' application/x-msmetafile ',
' CSS ' => ' text/css ',
' htm ' => ' text/html ',
' HTML ' => ' text/html ',
' txt ' => ' text/plain ',
' xml ' => ' Text/xml ',
' WML ' => ' text/wml ',
' Wbmp ' => ' image/vnd.wap.wbmp ',
' Mid ' => ' Audio/midi ',
' WAV ' => ' audio/wav ',
' mp3 ' => ' audio/mpeg ',
' MP2 ' => ' audio/mpeg ',
' avi ' => ' Video/x-msvideo ',
' MPEG ' => ' video/mpeg ',
' mpg ' => ' video/mpeg ',
' Qt ' => ' video/quicktime ',
' mov ' => ' video/quicktime ',
' Lha ' => ' Application/x-lha ',
' Lzh ' => ' Application/x-lha ',
' Z ' => ' application/x-compress ',
' Gtar ' => ' Application/x-gtar ',
' GZ ' => ' application/x-gzip ',
' gzip ' => ' application/x-gzip ',
' tgz ' => ' application/x-gzip ',
' Tar ' => ' Application/x-tar ',
' bz2 ' => ' application/bzip2 ',
' Zip ' => ' application/zip ',
' Arj ' => ' Application/x-arj ',
' rar ' => ' application/x-rar-compressed ',
' hqx ' => ' application/mac-binhex40 ',
' Sit ' => ' application/x-stuffit ',
' Bin ' => ' application/x-macbinary ',
' UU ' => ' Text/x-uuencode ',
' Uue ' => ' Text/x-uuencode ',
' Latex ' => ' Application/x-latex ',
' Ltx ' => ' Application/x-latex ',
' Tcl ' => ' application/x-tcl ',
' PGP ' => ' APPLICATION/PGP ',
' ASC ' => ' APPLICATION/PGP ',
' exe ' => ' application/x-msdownload ',
' Doc ' => ' Application/msword ',//Open source OSPhP.COM.CN
' RTF ' => ' application/rtf ',
' xls ' => ' application/vnd.ms-excel ',
' ppt ' => ' application/vnd.ms-powerpoint ',
' MDB ' => ' application/x-msaccess ',
' WRI ' => ' application/x-mswrite ',
);
?>
PHP generates Excel documents
?
Header ("Content-type:application/vnd.ms-excel");
Header ("Content-disposition:filename=test.xls");
echo "TEST1T";
echo "Test2tn";
echo "TEST1T";
echo "Test2tn";
echo "TEST1T";
echo "Test2tn";
echo "TEST1T";
echo "Test2tn";
echo "TEST1T";
echo "Test2tn";
echo "TEST1T";
echo "Test2tn";
Change the file header to output the. doc. xls file format
?>
Time comparison issues
Give a simple example to illustrate: for example, a forum on the day posted posts with new pictures marked. Open Source OSPhP.COM.CN
Method One:
?
$db->rows[$i][date] is the value of the DateTime field in the database.
$today =time ();
$theDay =date ("y-m-d h:i:s", $today -24*3600);
$newTag = $db->rows[$i][date]>= $theDay? " ': ""; OSPHP.com.CN
Method Two:
$newTag = $db->rows[$i][date]>=date ("y-m-d 00:00:00")? ': "";
?>
Extracts the page and the browser submits the variable, the function is equivalent causes the php.ini to open the global variable//osphp.com.cn
?
@extract ($_server, Extr_skip);
@extract ($_session, Extr_skip);
@extract ($_post, Extr_skip);
@extract ($_files, Extr_skip);
@extract ($_get, Extr_skip);
@extract ($_env, Extr_skip);
?>
Reading file functions
?
function ReadFromFile ($file _name) {
if (file_exists ($file _name)) {
$filenum =fopen ($file _name, "R");
Flock ($filenum, LOCK_EX);
$file _data=fread ($filenum, FileSize ($file _name));
Rewind ($filenum); OSPHP.COM.CN
Fclose ($filenum);
return $file _data;
}
}
?>
Write to a file function
?
function WriteToFile ($file _name, $data, $method = "W") {
$filenum =fopen ($file _name, $method);
Flock ($filenum, LOCK_EX);
$file _data=fwrite ($filenum, $data);
Fclose ($filenum);
return $file _data;
}
?>
Open Source Code OSPhP.COm.CN
Page Quick Turn
?
function Turntopage ($url = "index.php", $info = "page shift ...", $second =2) {
Print "<html>n<head>n<title> page shift ... </title>n";
Print "<meta http-equiv=" Refresh "content=" $second; url= $url ">n";
Print "<style type=" text/css ">n<!--n";
Print "TD {Font-family:" Verdana "," Arial "; Font-size:12px}n";
Print "A {COLOR: #000000; Text-decoration:none}n ";
print "-->n</style>n";
print "</head>n<body>n";
Print "<table width=" 100% "border=" 0 "align=" center ">n";
print "<tr>n";
Print "<td height=" "> </td>n";
print "</tr>n";
print "<tr>n";
Print "<TD align=" center ">n";
Print "<table width=" 60% "border=" 0 "cellpadding=" 8 "bgcolor=" #AA9FFF ">n"; OSPHP.COm.CN
print "<tr>n";
Print "<td height=" align= "Center" > page turn to hint information </td>n ";
print "</tr>n";
print "<tr>n";
Print "<TD align=" center "> $info </td>n";
print "</tr>n"; OsPHP.COM.CN
print "<tr>n";
Print "<TD align=" center ">n";
Print "<a href=" $url > if your browser does not support automatic jump, please click here </a></td>n ";
print "</tr>n";
print "</tr>n";
print "</table></td>n";
print "</tr>n";
print "<tr>n";
Print "<td height=" "> </td>n";
print "</tr>n";
print "</table>n";
print "</body>n</html>";
Exit
?>
Generating random String functions
?
function Random ($length) {
$hash = @#@#;
$chars = @ #ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz @#;
$max = strlen ($chars)-1;
Mt_srand (Double) microtime () * 1000000);
for ($i = 0; $i < $length; $i + +) {//php Open source code
$hash. = $chars [Mt_rand (0, $max)];
}
return $hash;
}
?>
Intercepts a certain length of string (this function is valid for GB2312 use)
?
function Wordscut ($string, $length, $sss =0) {
if (strlen ($string) > $length) {
if ($sss) {
$length = $length-3;
$addstr =@# ... @#; Open Source Code OSPhP.COm.CN
}
for ($i = 0; $i < $length; $i + +) {
if (Ord ($string [$i]) > 127) {
$wordscut. = $string [$i]. $string [$i + 1]; OSPHP.COM.CN
$i + +;
} else {
$wordscut. = $string [$i];
}
}
return $wordscut. $addstr;
}
return $string;
}
?>
Get Client IP Address
?
function GetIP () {
if (getenv ("Http_client_ip") && strcasecmp (getenv ("Http_client_ip"), "Unknown")
$ip = getenv ("Http_client_ip");
else if (getenv ("Http_x_forwarded_for") && strcasecmp (getenv ("Http_x_forwarded_for"), "Unknown")
$ip = getenv ("Http_x_forwarded_for"); OSPHP.COM.CN
else if (getenv ("REMOTE_ADDR") && strcasecmp (getenv ("REMOTE_ADDR"), "Unknown")
$ip = getenv ("REMOTE_ADDR");
else if (isset ($_server[@ #REMOTE_ADDR @#]) && $_server[@ #REMOTE_ADDR @#] && strcasecmp ($_server[@# remote_addr@#], "unknown")
$ip = $_server[@ #REMOTE_ADDR @#];
Else
$ip = "Unknown";
return ($IP);
}
?>
PHP Open Source Code
Determine your email address
?
function Checkemail ($inAddress)
{
Return (Ereg ("^ ([a-za-z0-9_-]) +@" ([a-za-z0-9_-]) + (. [ A-za-z0-9_-]) + ", $inAddress));
}
?>
Paging (two functions used together)
?
function GetPage ($sql, $page _size=20)
{
Global $page, $totalpage, $sums; Out param
$page = $_get["page"];
$eachpage = $page _size;
$pagesql = Strstr ($sql, "from");
$pagesql = "SELECT count (*) as IDs". $pagesql;
$result = mysql_query ($pagesql);
if ($rs = mysql_fetch_array ($result)) $sums = $rs [0];
$totalpage = Ceil ($sums/$page _size);
if ((! $page) ($page <1)) $page = 1;
$startpos = ($page-1) * $page _size;
$sql. = "Limit $startpos, $page _size";
return $sql;
}
function Showbar ($string = "")
{
Global $page, $totalpage;
$out = "Altogether <font". $totalpage. " color=@ #red @#><b> ". $totalpage." </b></font> page ";
$linkNum = 4;
$start = ($page-round ($linkNum/2)) >0? ($page-round ($linkNum/2)): "1";
$end = ($page +round ($linkNum/2)) < $totalpage? ($page +round ($linkNum/2)): $totalpage;
$prestart = $start-1;
$nextend = $end +1;
if ($page <>1)
$out. = "<a href=@#?page=1&&". $string. " @ #title = First page > first page </a> ";
if ($start >1)
$out. = "<a href=@#?page=". $prestart. " @# title= Previous Page "... <<</a> ";
for ($t = $start; $t <= $end; $t + +)
{
$out. = ($page = = $t)? "<font [". $t. "] color=@ #red @#><b>[". $t."] </b></font> ":" <a $thref =@#?page= $t && ". $string." @#> $t </a> ";
}
if ($end < $totalpage)
$out. = "<a href=@#?page=". $nextend. " && ". $string." @# title= next page >>> </a> ";
if ($page <> $totalpage)
$out. = "<a href=@#?page=". $totalpage. " && ". $string." @# title= last page > last page </a> "; Open Source OSPhP.COM.CN
return $out;
}
?>
Gets the ID of the newly inserted data
?
MYSQL_INSERT_ID ();
?>
Get the current script URL
?
function Get_php_url () {
if (!empty ($_server["Request_uri"])) {
$scriptName = $_server["Request_uri"];
$nowurl = $scriptName;
}else{
$scriptName = $_server["Php_self"];
if (Empty ($_server["query_string")) $nowurl = $scriptName;
else $nowurl = $scriptName. "?" $_server["Query_string"];
}
return $nowurl;
}
?>
Convert full-width digits to half-width digits
?
function Getalabnum ($fnum) {
$nums = Array ("0", "1", "2", "3", "4", "5", "6", "7", "8", "9");
$fnums = "0123456789";
For ($i =0 $i <=9; $i + +) $fnum = Str_replace ($nums [$i], $fnums [$i], $fnum);
$fnum = Ereg_replace ("[^0-9.]| ^0{1,} "," ", $fnum);
if ($fnum = = "") $fnum = 0;
return $fnum;
}
?>
Remove HTML tags
?
function text2html ($txt) {
$txt = Str_replace ("", "", $txt);
$txt = Str_replace ("<", "<", $txt);
$txt = Str_replace (">", ">", $txt);
$txt = Preg_replace ("/[rn]{1,}/isu", "
RN ", $txt);
return $txt;
}
?>
Convert a relative path into an absolute path
?
function Relative_to_absolute ($content, $feed _url) {
Preg_match ('/(HTTP|HTTPS|FTP):///', $feed _url, $protocol);
$server _url = Preg_replace ("/(http|https|ftp|news):///", "", $feed _url);
$server _url = preg_replace ("//.*/", "", $server _url);
if ($server _url = = ") {
return $content;
}
if (isset ($protocol [0])) {
$new _content = preg_replace ('/href= '//', ' href= '. $protocol [0]. $server _url. ' /', $content);
$new _content = preg_replace ('/src= '//', ' src= '. $protocol [0]. $server _url. ' /', $new _content);
} else {
$new _content = $content;
}
return $new _content;
}
?>
Get all Links
?
function Get_all_url ($code) {
Preg_match_all ('/<as+href=[' | ')? ([^> "']+") ["|']? S*[^>]*> ([^>]+) </a>/i ', $code, $arr); OsPHP.COM.CN
Return Array (' name ' => $arr [2], ' URL ' => $arr [1]);
}
?>
&NBSP
//html each row of the table to a CSV format array
.?
Function Get_tr_array ($table) {
$table = preg_replace ("' <td[^ >]*?> ' si ', ' "', $table);
$table = Str_replace ("</td>", ' ", ', $table);
$table = Str_replace ("</tr>", "{tr}", $table);
//Remove HTML tags
$ Table = Preg_replace ("' <[/!] *? [^<>]*?> ' Si ', "", $table);
//Remove white space characters
$table = Preg_replace ("' ([RN]) [s]+ '", "", $table);
$table = Str_replace ("", "", $table);
$table = Str_replace ("", "", $table);
$table = Explode (", {tr}", $table);
Array_pop ($table);
return $table;
}
?>
To convert each row of HTML tables into an array, collect tabular data
?
function Get_td_array ($table) {
$table = Preg_replace ("' <table[^>]*?> ' si", "", $table); Open Source Code OSPhP.COm.CN
$table = Preg_replace ("' <tr[^>]*?> ' si", "", $table);
$table = Preg_replace ("' <td[^>]*?> ' si", "", $table);
$table = Str_replace ("</tr>", "{tr}", $table); Open Source Code OSPHP.COM.Cn
$table = Str_replace ("</td>", "{td}", $table);
Get rid of HTML tags
$table = Preg_replace ("' <[/!] *? [^<>]*?> ' Si ', "", $table);
Remove white space characters
$table = Preg_replace ("' ([RN]) [s]+ '", "", $table);
$table = Str_replace ("", "", $table);
$table = Str_replace ("", "", $table);
$table = Explode (' {tr} ', $table);
Array_pop ($table); OSPHP.COM.Cn Open Source
foreach ($table as $key => $tr) {
$TD = Explode (' {TD} ', $TR);
Array_pop ($TD);
$TD _array[] = $TD;
}
return $TD _array;
}
?>
Returns all words in a string $distinct =true remove Duplicates
?
function Split_en_str ($str, $distinct =true) {
Preg_match_all ('/([a-za-z]+)/', $STR, $match);
if ($distinct = = True) {
$match [1] = Array_unique ($match [1]);
}
Sort ($match [1]);
return $match [1];
}
?>
Print out a list of people who contribute to this PHP project
?
String phpcredits (void)
?>