PHP网站开发的一些经典应用整理

发布时间:2025-02-17 点击:29
php的做为web编程语应用是非常广的,做为一个php程序员要知道并总结一些应用写法方式以方便日常的开发。
1、自动判断当前http协议类型,这个应用还是我在支付宝sdk中发现的,是一个不错的写法。
$protocol = (!empty($_server['https']) && $_server['https'] !== 'off' || $_server['server_port'] == 443) ? "https://" : "http://";
2、php判断浏览器终端pc、平板、手机的实现方法,经实测,非常好用和准备,可以在后端实现根据不同的终端调用不同的模块处理。
mobile_detect项目地址:https://github.com/serbanghita/mobile-detect/
require_once 'mobile_detect.php';
$detect = new mobile_detect;
$devicetype = ($detect->ismobile() ? ($detect->istablet() ? 'tablet' :'phone') :'computer');
3、php通过curl上传文件,特别注意的是下面的file路径,如果在windows环境下,在realpath后的路径前面加上@字符是可以正常发送的。如果在linux中是不能正常发送的。正确的写法是curl_file_create($fileurl)
$fileurl = realpath($fileobj->getrealfile());//获取文件的实际物理路径
$post_data = array(
'token' => '88e780d49ff812c644c89ff31e5de196',
"file" => curl_file_create($fileurl),
);
4、php生成二维码,使用的是phpqrcode。
5、php浮点数精度丢失问题解决方案。原则:在进行浮点数计算的时候一定要对浮点数进行格式化!再代入进行计算处理。
var_dump(intval(sprintf("%.2f", $f*100)));//string '58.00'//int 58
var_dump(intval(round($f*100, 1)));//float 58//int 58
var_dump(intval(number_format($f*100,2)));//string '58.00'//int 58
6、根据ip定位用户所在城市信息
使用第三方ip库来获取内容:http://www.ipip.net/download.html
7、随机排列数组。shuffle()函数把数组中的元素按随机顺序重新排列。常在数据库中查出数据后,用这个函数处理一下就可以生成随机排列的新数组
8、php获取上传文件大小函数
/**
* 文件大小格式化
*@param type $filename 文件路径
*/
function getfilesize($filename) {
$filename = $_server['document_root'] . . $filename;
$size=filesize($filename);
$mod = 1024;
$units = explode(' ','b kb mb gb tb pb');
for ($i = 0; $size > $mod; $i++) {
$size /= $mod;
}
return round($size, 2) . ' ' . $units[$i];
}



网站设计如何提高用户体验度?
制作手机网站需要在哪些方面下功夫
不同的网站有不同的风格 与产品相结合才能给用户更好的体验
北京网站制作提醒一个好的网站要多注意细节
网站商品价格的设计 如何不让用户产生困惑
从始至终制作网站费用不同都是因为这些
建设网站需要多少钱,常见的网站建设方式有哪些?
北京网站建设中的三感:空间感、立体感、神秘感