卓越飞翔博客卓越飞翔博客

卓越飞翔 - 您值得收藏的技术分享站
技术文章11201本站已运行3223

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 < 24 ) {
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 < $qcontcount; $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' => '[email protected]',
'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 'alert("在24小时内就可以继续执行一次!");';
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简单Token使用与验证实例
下一篇: Wordpress外链转内链修改方法
留言与评论(共有 0 条评论)
   
验证码:
隐藏边栏