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;

}

php精准函数,PHP常用函数大全相关推荐

  1. matlab常用函数与常用指令大全

    matlab常用函数与常用指令大全 matlab常用函数- - 1.特殊变量与常数 ans 计算结果的变量名 computer 确定运行的计算机 eps 浮点相对精度 Inf 无穷大 I 虚数单位 i ...

  2. oracle共享函数,oracle常用函数及示例分享

    oracle很多常用的函数如果了解的话可以加速开发,原本想总结下自己工作中使用oracle函数的一些场景,后发现川哥哥的博客总结的很好,为了方便查询函数就转摘过来. 总结的很不错,简单易懂,没什么事就 ...

  3. PHP 常用函数 - 其他常用函数

    PHP 常用函数 PHP 常用函数 - 字符串函数 PHP 常用函数 - 数组函数 PHP 常用函数 - 数学函数 PHP 常用函数 - 目录.文件函数 PHP 常用函数 - 其他常用函数 文章目录 ...

  4. datastage 函数_DataStage常用函数大全

    1 / 38 DataStage 常用函数大全 DATASTAGE 常用函数大全 . ......................................................... ...

  5. iif函数 vfp_VFP常用函数大全

    VFP 常用函数大全 一 . 字符及字符串处理函数 : 字符及字符串处理函数的处理对象均为字符型数据 , 但其返回值类型各异 . 1. 取子串函数 : 格式 :substr(c,n1,n2) 功能 : ...

  6. datastage 函数_DataStage常用函数大全.doc

    DataStage常用函数大全 DataStage常用函数大全 DataStage常用函数大全1 一.类型转换函数4 1.Char4 2.DateToString4 3.DateToDecimal4 ...

  7. matlab doc函数,matlab常用函数.doc

    matlab常用函数.doc MatLab 常用函数 1. 特殊变量与常数 ans 计算结果的变量名 computer 确定运行的计算机 eps 浮点相对精度 Inf 无穷大 I 虚数单位 name ...

  8. python常用函数-python常用函数精讲

    原标题:python常用函数精讲 返回值为bool类型的函数 bool是Boolean的缩写,只有真(True)和假(False)两种取值 bool函数只有一个参数,并根据这个参数的值返回真或者假. ...

  9. python常用函数-python常用函数与用法示例

    本文实例讲述了python常用函数与用法.分享给大家供大家参考,具体如下: 自定义函数实例 # 定义一个函数 def printme( str ): "打印任何传入的字符串" pr ...

  10. 如果你也会C#,那不妨了解下F#(4):了解函数及常用函数

    函数式编程其实就是按照数学上的函数运算思想来实现计算机上的运算.虽然我们不需要深入了解数学函数的知识,但应该清楚函数式编程的基础是来自于数学. 例如数学函数f(x) = x^2+x,并没有指定返回值的 ...

最新文章

  1. Scrum敏捷研发管理平台-Leangoo看板
  2. GIF发明者感染新冠后去世,没有他就没有表情包
  3. Postgresql使用笔记
  4. 【Git】Git 版本管理 ( 补充提交版本 git commit --amend | 版本库提取文件 git checkout -- filename | 删除文件 git rm )
  5. python 类特殊方法_python中用特殊方法定制类
  6. Python 在字符串中处理html 和xml
  7. 移动app部分机型无法唤起h5支付宝支付_谜之wxs,uni-app如何用它大幅提升性能
  8. 对话猿辅导:阿里云远程办公零信任落地创新安全
  9. 是、大于等于_血压高,是不是等于患上高血压?吃药之前,先搞清楚
  10. 《Linux编程》学习笔记 ·001【基本操作、常用命令】
  11. 代码整洁之道读书笔记----第三章---函数--第三节-函数结构的优化
  12. php设计鸡兔同笼问题解法,鸡兔同笼问题口诀及解题方法(含经典应用题及答案)...
  13. UVa12235 Help Bubu
  14. K8S学习笔记之将Google的gcr.io、k8s.gcr.io 换为国内镜像
  15. Python计算圆的周长与面积
  16. redis IO模型的演进
  17. 银河麒麟服务器操作系统V10SP2安装搭建OpenVP
  18. OpenCV学习笔记(六)—— OpenCV for Android打开相机
  19. Iterator patten 读书笔记
  20. 微型torch去马赛克setup.py运行笔记

热门文章

  1. Android的DatePicker和TimePicker-android学习之旅(三十八)
  2. JVM垃圾回收机制之引用类型
  3. AFNetworking2.0源代码解析
  4. Android View之用户界面...
  5. ftp文件传输协议的匿名用户、系统用户的实现详解过程
  6. Tomcat version 5.5 only supports J2EE 1.2, 1.3, and 1.4 Web modules
  7. “打工皇帝”唐骏借收购重返IT
  8. LCP 19. 秋叶收藏集
  9. socket编程(六)
  10. 为什么root下不能使用passwd命令_基于centos7下安装部署openldap+phpldapadmin