首页 > 编程语言 >PHP经验集锦

PHP经验集锦

时间:2023-09-04 17:07:40浏览次数:45  
标签:经验 Agent application 集锦 sql elseif PHP os eregi


作者:snowai 

 


最近刚刚完成手中的项目,比较闲。来这儿转转,把积累的一些技巧分享给大家!
1、关于PHP重定向
方法一:header("Location: index.php");
方法二:echo "<script>window.location =/"$PHP_SELF/";</script>";
方法三:echo "<META HTTP-EQUIV=/"Refresh/" CONTENT=/"0; URL=index.php/">";

2、获取访问者浏览器

function browse_infor() 
{ 
$browser="";$browserver=""; 
$Browsers =array("Lynx","MOSAIC","AOL","Opera","JAVA","MacWeb","WebExplorer","OmniWeb"); 
$Agent = $GLOBALS["HTTP_USER_AGENT"]; 
for ($i=0; $i<=7; $i++) 
{ 
if (strpos($Agent,$Browsers[$i])) 
{ 
$browser = $Browsers[$i]; 
$browserver =""; 
} 
} 
if (ereg("Mozilla",$Agent) && !ereg("MSIE",$Agent)) 
{ 
$temp =explode("(", $Agent); $Part=$temp[0]; 
$temp =explode("/", $Part); $browserver=$temp[1]; 
$temp =explode(" ",$browserver); $browserver=$temp[0]; 
$browserver =preg_replace("/([/d/.]+)/","//1",$browserver); 
$browserver = " $browserver"; 
$browser = "Netscape Navigator"; 
} 
if (ereg("Mozilla",$Agent) && ereg("Opera",$Agent)) 
{ 
$temp =explode("(", $Agent); $Part=$temp[1]; 
$temp =explode(")", $Part); $browserver=$temp[1]; 
$temp =explode(" ",$browserver);$browserver=$temp[2]; 
$browserver =preg_replace("/([/d/.]+)/","//1",$browserver); 
$browserver = " $browserver"; 
$browser = "Opera"; 
} 
if (ereg("Mozilla",$Agent) && ereg("MSIE",$Agent)) 
{ 
$temp = explode("(", $Agent); $Part=$temp[1]; 
$temp = explode(";",$Part); $Part=$temp[1]; 
$temp = explode(" ",$Part);$browserver=$temp[2]; 
$browserver =preg_replace("/([/d/.]+)/","//1",$browserver); 
$browserver = " $browserver"; 
$browser = "Internet Explorer"; 
} 
if ($browser!="") 
{ 
$browseinfo = "$browser$browserver"; 
} 
else 
{ 
$browseinfo = "Unknown"; 
} 
return $browseinfo; 
} 
//调用方法$browser=browseinfo() ;直接返回结果



3、获取访问者操作系统

function osinfo() { 
$os=""; 
$Agent = $GLOBALS["HTTP_USER_AGENT"]; 
if (eregi('win',$Agent) && strpos($Agent, '95')) { 
$os="Windows 95"; 
} 
elseif (eregi('win 9x',$Agent) && strpos($Agent, '4.90')) { 
$os="Windows ME"; 
} 
elseif (eregi('win',$Agent) && ereg('98',$Agent)) { 
$os="Windows 98"; 
} 
elseif (eregi('win',$Agent) && eregi('nt 5/.0',$Agent)) { 
$os="Windows 2000"; 
} 
elseif (eregi('win',$Agent) && eregi('nt',$Agent)) { 
$os="Windows NT"; 
} 
elseif (eregi('win',$Agent) && eregi('nt 5/.1',$Agent)) { 
$os="Windows XP"; 
} 
elseif (eregi('win',$Agent) && ereg('32',$Agent)) { 
$os="Windows 32"; 
} 
elseif (eregi('linux',$Agent)) { 
$os="Linux"; 
} 
elseif (eregi('unix',$Agent)) { 
$os="Unix"; 
} 
elseif (eregi('sun',$Agent) && eregi('os',$Agent)) { 
$os="SunOS"; 
} 
elseif (eregi('ibm',$Agent) && eregi('os',$Agent)) { 
$os="IBM OS/2"; 
} 
elseif (eregi('Mac',$Agent) && eregi('PC',$Agent)) { 
$os="Macintosh"; 
} 
elseif (eregi('PowerPC',$Agent)) { 
$os="PowerPC"; 
} 
elseif (eregi('AIX',$Agent)) { 
$os="AIX"; 
} 
elseif (eregi('HPUX',$Agent)) { 
$os="HPUX"; 
} 
elseif (eregi('NetBSD',$Agent)) { 
$os="NetBSD"; 
} 
elseif (eregi('BSD',$Agent)) { 
$os="BSD"; 
} 
elseif (ereg('OSF1',$Agent)) { 
$os="OSF1"; 
} 
elseif (ereg('IRIX',$Agent)) { 
$os="IRIX"; 
} 
elseif (eregi('FreeBSD',$Agent)) { 
$os="FreeBSD"; 
} 
if ($os=='') $os = "Unknown"; 
return $os; 
} 
//调用方法$os=os_infor() ;



4、文件格式类

5、php生成excel文档

$mime_types = array( 
'gif' => 'image/gif', 
'jpg' => 'image/jpeg', 
'jpeg' => 'image/jpeg', 
'jpe' => 'image/jpeg', 
'bmp' => 'image/bmp', 
'png' => 'image/png', 
'tif' => 'image/tiff', 
'tiff' => 'image/tiff', 
'pict' => 'image/x-pict', 
'pic' => 'image/x-pict', 
'pct' => 'image/x-pict', 
'tif' => 'image/tiff', 
'tiff' => 'image/tiff', 
'psd' => 'image/x-photoshop', 

'swf' => 'application/x-shockwave-flash', 
'js' => 'application/x-javascript', 
'pdf' => 'application/pdf', 
'ps' => 'application/postscript', 
'eps' => 'application/postscript', 
'ai' => 'application/postscript', 
'wmf' => 'application/x-msmetafile', 

'css' => 'text/css', 
'htm' => 'text/html', 
'html' => 'text/html', 
'txt' => 'text/plain', 
'xml' => 'text/xml', 
'wml' => 'text/wml', 
'wbmp' => 'image/vnd.wap.wbmp', 

'mid' => 'audio/midi', 
'wav' => 'audio/wav', 
'mp3' => 'audio/mpeg', 
'mp2' => 'audio/mpeg', 

'avi' => 'video/x-msvideo', 
'mpeg' => 'video/mpeg', 
'mpg' => 'video/mpeg', 
'qt' => 'video/quicktime', 
'mov' => 'video/quicktime', 

'lha' => 'application/x-lha', 
'lzh' => 'application/x-lha', 
'z' => 'application/x-compress', 
'gtar' => 'application/x-gtar', 
'gz' => 'application/x-gzip', 
'gzip' => 'application/x-gzip', 
'tgz' => 'application/x-gzip', 
'tar' => 'application/x-tar', 
'bz2' => 'application/bzip2', 
'zip' => 'application/zip', 
'arj' => 'application/x-arj', 
'rar' => 'application/x-rar-compressed', 

'hqx' => 'application/mac-binhex40', 
'sit' => 'application/x-stuffit', 
'bin' => 'application/x-macbinary', 

'uu' => 'text/x-uuencode', 
'uue' => 'text/x-uuencode', 

'latex'=> 'application/x-latex', 
'ltx' => 'application/x-latex', 
'tcl' => 'application/x-tcl', 

'pgp' => 'application/pgp', 
'asc' => 'application/pgp', 
'exe' => 'application/x-msdownload', 
'doc' => 'application/msword', 
'rtf' => 'application/rtf', 
'xls' => 'application/vnd.ms-excel', 
'ppt' => 'application/vnd.ms-powerpoint', 
'mdb' => 'application/x-msaccess', 
'wri' => 'application/x-mswrite', 
);
<? 
header("Content-type:application/vnd.ms-excel"); 
header("Content-Disposition:filename=test.xls"); 
echo "test1/t"; 
echo "test2/t/n"; 
echo "test1/t"; 
echo "test2/t/n"; 
echo "test1/t"; 
echo "test2/t/n"; 
echo "test1/t"; 
echo "test2/t/n"; 
echo "test1/t"; 
echo "test2/t/n"; 
echo "test1/t"; 
echo "test2/t/n"; 
?>


//改动相应文件头就可以输出.doc .xls等文件格式了

6、时间比较问题
举一个简单例子说明:比如一个论坛对当天发表的贴子用new图片标记一下。
方法一:

//$db->rows[$i][date]中为数据库中datetime字段值. 
$today=time(); 
$theDay=date("Y-m-d H:i:s",$today-24*3600); 
$newTag=$db->rows[$i][date]>=$theDay?"<img src='../image/newinfor.gif'>":""; 
方法二: 
$newTag=$db->rows[$i][date]>=date("Y-m-d 00:00:00")?"<img src='../image/newinfor.gif'>":""; 
7.数据库封装例子<?php
//------------------------------------------------------------------------------------------ 
// ※Database()                   构造函数,数据库初始参数 
// ※Select()                     查询
// ※GetRows()                    返回查询的记录总数
// ※Insert()                     插入记录
// ※Update()                     更新
// ※Delete()                     删除
// ※Halt()                       中断并显示错误信息*/
//------------------------------------------------------------------------------------------ 
 define("DATABASETYPE","1");       //定义数据库类型:1为MySql;2为SQL Server;3为Oracle;4为Odbc
 define("SERVER","localhost");     //Host name or IP address of the database server
 define("DATABASE","dbName");   //要连接的数据库名
 define("USER","tableName");     //用于连接数据库的用户名
 define("PASSWORD","paswd");    //用于连接数据库的密码  
 
class Database
{
 var $dbLink;                      //连接句柄 
 var $result;                      //查询句柄 
 var $insId;                       //Insert()成功返回AUTO_INCREMENT列的值
 var $rows;                        //返回数据数组
 var $numRows;                     //返回数据数目
 var $dbHost, $dbUser, $userPassword, $database;
 var $dbType=DATABASETYPE;
 var $msgFlag = "yes" ;            //yes:show the Mysql message ; no: die by show "Halted."
 function Database($dbHost=SERVER,$dbUser=USER,$userPassword=PASSWORD,$database=DATABASE)
 {
   switch($this->dbType)
   {
     case 1:
      $this->dbLink=@mysql_pconnect($dbHost,$dbUser,$userPassword);// or die("Can't Connect to Remote Host!");
      @mysql_select_db($database,$this->dbLink);// or die ("Can't Connect to Remote Host!");
      break;
    case 2:
      break;    
  }
   return true;
 }
 
 /*SQL:Select() 返回为false无结果*/
 function Select($table,$columns,$condition=1)
 {
  $sql="select $columns from $table where $condition ";  
  //echo $sql."<br>";   
  $this->result=@mysql_query($sql,$this->dbLink);  
  unset($this->rows);
  if($this->result)
  {
   $i=0;
   if(!($this->rows=array("$i"=>@mysql_fetch_array($this->result))))
    return false;
   if(($this->numRows=@mysql_num_rows($this->result))==0)
    return false;
   while($tempRows=@mysql_fetch_array($this->result))
   { 
    array_push($this->rows,$tempRows); 
   }      
  }
  else
  {
   $this->Halt($sql);
   return false;
  }  
  return true;
 }
 
 /*SQL:GetRows() 返回查询的记录总数*/
 function GetRows($table,$condition=1)
 {
  $sql="select count(1) as count from $table where $condition";  
  //echo $sql."<br>";      
  $this->result=@mysql_query($sql,$this->dbLink);    
  if($this->result)
  {
    $temp=@mysql_fetch_array($this->result);
   $this->numRows=$temp[count];
  }
  else
  {
   $this->Halt($sql);
   return false;
  }  
  return $this->numRows;
 }
  
 /*SQL:Insert()*/
  
 function Insert($table,$columns,$values)
 {
  $sql="insert into $table ($columns) values ($values)";
  //echo $sql;
  $this->result=@mysql_query($sql,$this->dbLink);
  if ($this->result)
    $this->insId=@mysql_insert_id($this->dbLink);
   else
  {
   $this->Halt($sql);
   return false;
  }
  return true;
 } /*SQL:Update()*/
  
 function Update($table,$setings,$condition)
 {
  $sql="update $table set $setings where $condition";
  //echo $sql;
  $this->result=@mysql_query($sql,$this->dbLink);
  if ($this->result)
    $this->numRows=@mysql_affected_rows($this->result);
   else
  {
   $this->Halt($sql);
   return false;
  }
  return true;
 } /*SQL:Delete*/
 
 function Delete($table,$condition)
 {
  $sql="delete from $table where $condition";
  $this->result=@mysql_query($sql,$this->dbLink);
  if ($this->result)
     $this->numRows=@mysql_affected_rows($this->result);
   else
  {
   $this->Halt($sql);
   return false;
  }  
    
  return true;
 }
 
 /*Halt():error message */
 
 function Halt($msg)
 {
  if($this->msgFlag=="yes")
  {
   printf("<b>Database Query Error:</b> %s<br>/n", $msg);
   printf("<b>MySql Error:</b> %s<br>/n",mysql_error());
  }
  else
   echo "<META HTTP-EQUIV=REFRESH CONTENT='0;URL=../include/error.htm'>";//自定一个出错提示文件
    return false;
 }
} 
  switch($db->dbType)
 {
   case 1:
     @mysql_close();     
    break;
  case 2:
      
    break;    
  } 
  $db = new Database(); 
?>

标签:经验,Agent,application,集锦,sql,elseif,PHP,os,eregi
From: https://blog.51cto.com/u_16245757/7352563

相关文章

  • 页面效果集锦
     1. 走马灯效果(仅仅IE支持):<marqueedirection="up"height="70"Scrollamount="1"onMouseOver="this.stop();"onMouseOut="this.start();">要滚动的内容</marquee>   其中,direction表示移动方向,scrollamount表示移动速度,后面两个事件实......
  • 泛微E-Office init.php文件上传漏洞
    漏洞简介泛微E-Officeinit.php文件存在任意文件上传漏洞,攻击者可以通过该漏洞直接获取网站权限。漏洞复现fofa语法:app="泛微-EOffice"登录页面如下:POC:POST/E-mobile/App/init.phpHTTP/1.1Host:User-Agent:Mozilla/5.0(WindowsNT10.0;Win64;x64)AppleWebKit/53......
  • 泛微E-Office UploadFile.php任意文件上传漏洞 CNVD-2021-49104
    漏洞描述在/general/index/UploadFile.php中上传文件过滤不严格导致允许无限制地上传文件,攻击者可以通过该漏洞直接获取网站权限漏洞复现fofa语法:app="泛微-EOffice"登录页面如下:POC:POST/general/index/UploadFile.php?m=uploadPicture&uploadType=eoffice_logo&userId=H......
  • Linux日志管理经验总结(crontab+logrotate)
    Linux系统-部署-运维系列导航 日志管理目标日志的管理,一般包括两大部分日志内容,合理的日志内容(日志锚点,内容格式,等)可以为应用服务的执行记录、问题排查提供最有力的帮助日志存档规则,包括日志分割方式(按日期、按文件大小,等),日志存档数量,如只保存最近一个月,等对于自行开发的......
  • PHP实现RESTful风格的API实例
    Request.php:包含一个Request类,即数据操作类。接收到URL的数据后,根据请求URL的方式(GET|POST|PUT|PATCH|DELETE)对数据进行相应的增删改查操作,并返回操作后的结果:<?php/***数据操作类*/classRequest{//允许的请求方式privatestatic$method_type=array('get',......
  • PHP 中 array_walk 与array_map的区别
    array_map函数来对数组中的每个元素应用回调函数。该函数与array_walk类似**,但是它返回一个新的数组,而不是直接修改原始数组。**在PHP中,可以使用array_walk函数来遍历数组并执行自定义的操作。该函数接受三个参数:要遍历的数组、回调函数和数组元素的索引。回调函数应该接受......
  • PHP 中 array_walk 与array_map的区别
    PHP中array_walk与array_map的区别array_map函数来对数组中的每个元素应用回调函数。该函数与array_walk类似,但是它返回一个新的数组,而不是直接修改原始数组。在PHP中,可以使用array_walk函数来遍历数组并执行自定义的操作。该函数接受三个参数:要遍历的数组、回调函数......
  • PHP extract() 函数
    PHPextract()Function定义和用法extract()函数从数组中将变量导入到当前的符号表。该函数使用数组键名作为变量名,使用数组键值作为变量值。针对数组中的每个元素,将在当前符号表中创建对应的一个变量。第二个参数type用于指定当某个变量已经存在,而数组中又有同名元素时,ext......
  • ⏱️TimeHelper——一个简单快捷的PHP日期时间助手类库
    TimeHelper是一个简单易用的PHP时间日期助手类库,可以快速实现常用的时间日期操作,比如获取指定时间的秒数,获取友好的时间格式,判断时间范围,计算两个时间相差值,返回N小时/天/星期/月/年前或者后的时间戳等等......
  • win2016搭建frp内网穿透的FTP服务器可用phpstorm
    操作系统:WindowsServer2016StandardFTP服务器:ser-U7.0.0.1之前用FileZillaServer,但phpstorm怎么连接不上FTP,最后安装Ser—U使用了SSH模式成功连通。下载安装Ser-U创建域域名信息随便写,没有意义SSH的SFTP一定要打开添加用户frpc开通内网穿透端口frpc相关配......