再来 10 个有用的 PHP 代码
http://www.learncomputer.com/10-useful-php-code-snippets/ br /
http://www.learncomputer.com/10-useful-php-code-snippets/
function getRemoteIPAddress() { $ip = $_SERVER['REMOTE_ADDR']; return $ip; }
2. [代码]如果有代理服务器的情况下获取IP 跳至 [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] [全屏预览]
function getRealIPAddress() { if (!empty($_SERVER['HTTP_CLIENT_IP'])) { // check ip from share internet $ip = $_SERVER['HTTP_CLIENT_IP']; } elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) { // to check ip is pass from proxy $ip = $_SERVER['HTTP_X_FORWARDED_FOR']; } else { $ip = $_SERVER['REMOTE_ADDR']; } return $ip; }
3. [代码]获取 MySQL 时间戳 跳至 [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] [全屏预览]
$query = "select UNIX_TIMESTAMP(date_field) as mydate from mytable where 1=1"; $records = mysql_query($query) or die(mysql_error()); while($row = mysql_fetch_array($records)) { echo $row; }
4. [代码]验证日期格式:YYYY-MM-DD 跳至 [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] [全屏预览]
function checkDateFormat($date) { // match the format of the date if (preg_match("/^([0-9]{4})-([0-9]{2})-([0-9]{2})$/", $date, $parts)) { // check whether the date is valid of not if (checkdate($parts[2], $parts[3], $parts[1])) { return true; } else { return false; } } else { return false; } }
5. [代码]重定向 跳至 [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] [全屏预览]
header('Location: http://www.oschina.net/project/zh');
6. [代码]发送邮件 跳至 [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] [全屏预览]
$to = "someone@oschina.net"; $subject = "Your Subject here"; $body = "Body of your message here you can use HTML too. e.g. <br><b> Bold </b>"; $headers = "From: You\r\n"; $headers .= "Reply-To: info@yoursite.com\r\n"; $headers .= "Return-Path: info@yoursite.com\r\n"; $headers .= "X-Mailer: PHP\n"; $headers .= 'MIME-Version: 1.0' . "\n"; $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n"; mail($to, $subject, $body, $headers);
7. [代码]BASE64 编码和解码 跳至 [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] [全屏预览]
function base64url_encode($plainText) { $base64 = base64_encode($plainText); $base64url = strtr($base64, '+/=', '-_,'); return $base64url; } function base64url_decode($plainText) { $base64url = strtr($plainText, '-_,', '+/='); $base64 = base64_decode($base64url); return $base64; }
8. [代码]JSON 处理 跳至 [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] [全屏预览]
$json_data = array ('id'=>1,'name'=>"John",'country'=>'Canada',"work"=>array("Google","Oracle")); echo json_encode($json_data); $json_string='{"id":1,"name":"John","country":"Canada","work":["Google","Oracle"]} '; $obj=json_decode($json_string); // print the parsed data echo $obj->name; //displays John echo $obj->work[0]; //displays Google
9. [代码]检测用户浏览器类型 跳至 [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] [全屏预览]
$useragent = $_SERVER ['HTTP_USER_AGENT']; echo "<b>Your User Agent is</b>: " . $useragent;
10. [代码]显示网页源码 跳至 [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] [全屏预览]
$lines = file('http://www.oschina.net/home/about'); foreach ($lines as $line_num => $line) { // loop thru each line and prepend line numbers echo "Line #<b>{$line_num}</b> : " . htmlspecialchars($line) . "<br>\n"; }
11. [代码]调整服务器时间 跳至 [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] [全屏预览]
$now = date('Y-m-d-G'); $now = strftime("%Y-%m-%d-%H", strtotime("$now -8 hours"));
- 上一篇:封装好的PHP分页类,简单好用
- 下一篇:php关机
精彩图集
精彩文章