http://www.learncomputer.com/10-useful-php-code-snippets/
- function getRemoteIPAddress() {
- $ip = $_SERVER['REMOTE_ADDR'];
- return $ip;
- }
}
}
> 複製程式碼
-
-
-
- function getRealIPAddress() {
- if (!empty($_SERVER['HTTP_CLIENT_IP'])) { // 檢查共享網際網路的ip
- $ip = $_SERVER ['HTTP_CLIENT_IP'];
- } elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) { // 檢查ip 是否從代理傳遞
- $ip = $_SERVER['HTTP_X_FORWARD $ip = $_SERVER['REMOTE_ADDR'];
- }
return $ip; }
複製代碼
-
-
- $query = "從mytable 選擇UNIX_TIMESTAMP(date_field) 為mydate,其中1=1";
- $records = mysql_query($query) 或die(mysql_error()die(mysql_error) );
- while($row = mysql_fetch_array($records)) {
echo $row; }
複製代碼
-
-
- function checkDateFormat($date) {
- // 符合日期的格式
- if (preg_match("/^([0-9]{4})) - ([0-9]{2})-([0-9]{2})$/", $date, $parts)) {
- // 檢查日期是否有效not
- if (checkdate( $parts[2], $parts[3], $parts[1])) {
- 回傳true;
- } else {
- 回傳false;
- }
- } else {
- return false;
} }
複製
header('位置:http://www.oschina.net/project/zh');
複製代碼
-
- $to = "someone@oschina.net";
- $subject = "您的主題在這裡";
- $body = "您可以在此處使用您的郵件內文HTML 也如
粗體";
- $headers = "來自: Yourn";
- $headers .= "回覆至: info@yoursite.comrn" ;
- $headers .= "返迴路徑: info@yoursite.comrn";
- $headers .= "X-Mailer: PHPn";
- $headers .= 'MIME-版本: 1.0' . " n";
- $headers .= '內容類型:text/html;字元集=iso-8859-1' 。 "rn";
mail($to, $subject, $body, $headers);
複製程式碼
-
- 函數base64url_encode($plainText) {
- $base64 = base64_encode($plainText);
- $base64url = strtr($base64, '+/='
- $base64url = strtr($base64, '- _,');
- return $base64url;
- }
-
- function base64url_decode($plainText) {
- $base64url = strtr($plainText, '-_,', '+/= ');
- $base64 = base64_decode($base64url);
return $base64; }
複製代碼
-
-
- $json_data = array ('id'=>1,'name'=>"約翰",'國'=>'加拿大',"工作"=> ;array( "Google","Oracle"));
- echo json_encode($json_data);
-
- $json_string='{"id":1,"name":"John","country ":" Canada","work":["Google","Oracle"]} ';
- $obj=json_decode($json_string);
-
- //列印解析後的資料
echo $obj ->名稱; //顯示John echo $obj->work[0]; //顯示Google
複製程式碼
-
-
$useragent = $_SERVER ['HTTP_USER_AGENT']; echo " 您的使用者代理是: " 。 $useragent;
複製程式碼
- $lines = file('http://www.oschina.net/home/about');
- foreach ($lines as $line_num => $line) {
- // 循環遍歷每一行並新增行號
- echo "Line #{$line_num} : " . htmlspecialchars($line) 。 "
n";
- }
複製程式碼
- $now = date('Y-m-d-G');
- $now = strftime("%Y-%m-%d-%H", strtotime("$現在-8小時"));
複製程式碼
|