10个必看的PHP小代码,很实用!


 1、获取浏览器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;

  }



  2、获取 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;

  }



  3、验证日期格式: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;

  }

  }



  4、重定向

  header('Location:  http://www.xurida.net ');



  5、发送邮件

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



  6、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;

  }



  7、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



  8、检测用户浏览器类型

  $useragent = $_SERVER ['HTTP_USER_AGENT'];

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



  9、显示网页源码

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

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

  // loop thru each line and prepend line numbers

  echo "Line #<b>{$line_num}</b> : " . htmlspecialchars($line) . "<br>\n";

  }



  10、调整服务器时间

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

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




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值