• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

10 个有用的 PHP 代码_php

php 搞代码 3年前 (2018-06-21) 115次浏览 已收录 0个评论

  获取浏览器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 = "[email protected]";

  $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: [email protected]/r/n";

  $headers .= "Return-Path: [email protected]/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 #<b>{$line_num}</b> : " . htmlspecialchars($line) . "<br>/n";

  }

 

  调整服务器时间

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

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

欢迎大家阅读《10 个有用的 PHP 代码_php》,跪求各位点评,若觉得好的话请收藏本文,by 搞代码


喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址