php精准函数,PHP常用函数大全

PHP随机图片API本地图片版,页面直接输出图片<?php

$img_array = glob('images/*.{gif,jpg,png,jpeg,webp,bmp}', GLOB_BRACE);

if(count($img_array) == 0) die('没找到图片文件。请先上传一些图片到 '.dirname(__FILE__).'/images/ 文件夹');

header('Content-Type: image/png');

echo(file_get_contents($img_array[array_rand($img_array)]));

?>

将图片保存到images目录下,自动读取images图片并输出。

PHP随机图片API远程图片版,页面直接输出图片<?php

$imgku=file('pic.txt');

showImg($imgku[array_rand($imgku)]);

/*

* php 页面直接输出图片

*/

function showImg($img){

$img = trim($img);

$info = getimagesize($img);

$imgExt = image_type_to_extension($info[2], false); //获取文件后缀

$fun = "imagecreatefrom{$imgExt}";

$imgInfo = $fun($img);         //1.由文件或 URL 创建一个新图象。如:imagecreatefrompng ( string $filename )

//$mime = $info['mime'];

$mime = image_type_to_mime_type(exif_imagetype($img)); //获取图片的 MIME 类型

header('Content-Type:'.$mime);

$quality = 100;

if($imgExt == 'png') $quality = 9;   //输出质量,JPEG格式(0-100),PNG格式(0-9)

$getImgInfo = "image{$imgExt}";

$getImgInfo($imgInfo, null, $quality); //2.将图像输出到浏览器或文件。如: imagepng ( resource $image )

imagedestroy($imgInfo);

}

?>

需安装GD库及exif扩展,php.ini中开启allow_url_fopen函数,读取同目录下pic.txt文件中的图片网址,每行一个图片地址。

Cookie限制时间再次提交if(isset($_COOKIE['submission_time'])) {

$submissionTime =   $_COOKIE['submission_time'];

$currentTime    =   time();

$timePassed     =   ($currentTime - $submissionTime ) / 60 * 60;

if($timePassed 

echo "

You can record the sales after 24 hours! Please wait..
";

die();

}

}else {

$cookieName     =   'submission_time';

$cokkieValue    =   time();

setcookie($cookieName, $cokkieValue, time() + (+60*60*24*30 ), "/");

}

判断字符串是否含有某分割符,若包含分割符,分割后输出全部分割后的值if(strpos($qcont,',') === false){

echo "不包含,分割字段";

}else{

echo "包含,分割字段,下面进行切割并输出";

$qcontArr = explode(",", $qcont);

$qcontcount = count($qcontArr);

for ($i = 0; $i 

if ($qcontArr[$i] == "") {

continue;

}

echo $qcontArr[$i];

}

}

对错误的详情进行格式化输出,记入log文件。function slog($logs){

$toppath="log.htm";

$Ts=fopen($toppath,"a+");

fputs($Ts,$logs."rn");

fclose($Ts);

}

使用file_get_contents() 发送GET、POST请求

1、【GET请求】$data = array( 'name'=>'zhezhao','age'=>'23');

$query = http_build_query($data);

$url = 'http://localhost/get.php';//这里一定要写完整的服务页面地址,否则php程序不会运行

$result = file_get_contents($url.'?'.$query);

2、【POST请求】$data = array('user'=>'jiaxiaozi','passwd'=>'123456');

$requestBody = http_build_query($data);

$context = stream_context_create(['http' => ['method' => 'POST', 'header' => "Content-Type: application/x-www-form-urlencodedrn"."Content-Length: " . mb_strlen($requestBody), 'content' => $requestBody]]);

$response = file_get_contents('http://server.test.net/login', false, $context);

PHP获取当天是几号、周几echo date('y').'';    //当前年份

echo date('m').'';    //当前月份

echo date('d').'';    //当前日

echo date('s').'';    //当前秒

echo date('w').'';    //当前周几

打印结果显示为:20

07

24

50

5

PHP给第三方接口POST或GET方式传输数据并得到返回值function Post($url, $post = null)

{

$context = array();

if (is_array($post))

{

ksort($post);

$context['http'] = array

(

'timeout'=>60,

'method' => 'POST',

'content' => http_build_query($post, '', '&'),

);

}

return file_get_contents($url, false, stream_context_create($context));

}

$data = array

(

'name' => 'test',

'email' => 'test@gmail.com',

'submit' => 'submit',

);

echo Post('http://www.baidu.com', $data);

同一页面24小时之内之只能执行一次define('TIME_OUT', 86400); //定义重复操作最短的允许时间,单位秒

@session_start();

$time = time();

if( isset($_SESSION['time']) ){

if( $time - $_SESSION['time'] <= TIME_OUT ){

echo '';

exit();

}

}

$_SESSION['time'] = $time;

echo "正常执行!";

PHP连接远程MSSQL函数:

已在如上环境安装后测试通过!function mssql_user($username){

$host="远程服务器IP,MSSQL端口";

$dbname="数据库名称";

$user="数据库用户名";

$pass="数据库密码";

try {

$dbh = new PDO("sqlsrv:Server=$host;Database=$dbname", $user, $pass);

} catch(PDOException $e) {

echo $e->getMessage();

exit;

}

$stmt = $dbh->prepare("SELECT XXX FROM XXX WHERE XXX = ".$username);

$stmt->execute();

while ($row = $stmt->fetch()) {

echo $row[0];//多个查询结果输出

//return $row[0]; 单一的结果可以直接用return

}

unset($dbh); unset($stmt);

}

PHP时间戳和日期相互转换

获取当前日期时间的时间戳echo time();

获取当前日期时间echo date("Y/m/d H:i:s");

日期转换为时间戳echo strtotime(date("Y/m/d"));

时间戳转换为日期echo date('Y-m-d',time());

打印明天此时的时间戳echo strtotime("+1 day");

当前时间:echo date("Y-m-d H:i:s",time()) ;

指定时间:echo date("Y-m-d H:i:s",strtotime("+1 day")) ;

下个星期此时的时间戳echo strtotime("+1 week");

指定下星期几的PHP时间戳echo strtotime("next Thursday");

指定下星期几的时间:echo date("Y-m-d H:i:s",strtotime("next Thursday"));

指定上星期几的时间戳echo strtotime("last Thursday");

指定本年的最后一个星期几的时间:echo date("Y-m-d H:i:s",strtotime("last Thursday"));

截取指定两个字符之间的字符串

方法一function cut($begin,$end,$str){

$b = mb_strpos($str,$begin) + mb_strlen($begin);

$e = mb_strpos($str,$end) - $b;

return mb_substr($str,$b,$e);

}

方法二function get_between($input, $start, $end) {

$substr = substr($input, strlen($start)+strpos($input, $start),(strlen($input) - strpos($input, $end))*(-1));

return $substr;

}

方法一当截取的是值为串的时候,会出现截取不到的情况用方法二尝试。

方法三:preg_match_all函数preg_match_all('/(.*)/', $result, $matches);

//print_r($matches);

$resultapp = $matches[1][1];

方法一及方法二在截取长段字符串时,碰到过无法截取到的情况,用方法三解决。

调用SOHU API获取IP地址//通过API获取IP地址

function getIP(){

$str = file_get_contents('https://pv.sohu.com/cityjson?ie=utf-8');

$ip = cut('cip": "','", "cid',$str);

if($ip){

return $ip;

}

}

注:需配合上面 截取指定两个字符之间的字符串 函数一起使用

获取访问客户端的IP地址function get_client_ip(){

static $realip;

if (isset($_SERVER)){

if (isset($_SERVER["HTTP_X_FORWARDED_FOR"])){

$realip = $_SERVER["HTTP_X_FORWARDED_FOR"];

} else if (isset($_SERVER["HTTP_CLIENT_IP"])) {

$realip = $_SERVER["HTTP_CLIENT_IP"];

} else {

$realip = $_SERVER["REMOTE_ADDR"];

}

} else {

if (getenv("HTTP_X_FORWARDED_FOR")){

$realip = getenv("HTTP_X_FORWARDED_FOR");

} else if (getenv("HTTP_CLIENT_IP")) {

$realip = getenv("HTTP_CLIENT_IP");

} else {

$realip = getenv("REMOTE_ADDR");

}

}

return $realip;

}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值