首页 > php教程 > php手册 > 正文

10 个有用的 PHP 代码

php中文网
发布: 2016-06-21 08:51:38
原创
1465人浏览过

  获取浏览器IP地址

  function getRemoteIPAddress() {

  $ip = $_SERVER['REMOTE_ADDR'];

  return $ip;

  }

 

  如果有代理服务器的情况下获取IP

  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;

  }

 

  获取 MySQL 时间戳

  $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;

  }

 

  验证日期格式:YYYY-MM-DD

  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;

  }

  }

 

  重定向

  header('Location: http://www.php100.com');

 

  发送邮件

  $to = "someone@oschina.net";

  $subject = "Your Subject here";

  $body = "Body of your message here you can use HTML too. e.g.
Bold ";

  $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);

 

  BASE64 编码和解码

  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;

  }

 

  JSON 处理

  $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

 

  检测用户浏览器类型

  $useragent = $_SERVER ['HTTP_USER_AGENT'];

  echo "Your User Agent is: " . $useragent;

 

  显示网页源码

  $lines = file('http://www.php100.com/index.php');

  foreach ($lines as $line_num => $line) {

  // loop thru each line and prepend line numbers

  echo "Line #{$line_num} : " . htmlspecialchars($line) . "
\n";

  }

 

  调整服务器时间

  $now = date('Y-m-d-G');

  $now = strftime("%Y-%m-%d-%H", strtotime("$now -8 hours"));



PHP速学教程(入门到精通)
PHP速学教程(入门到精通)

PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号