【总结】PHP常见面试题汇总(三)
1、php如何在文章列表中任意位置或固定位置插入新的文章?比如:3、6位置
2、php如何删除两个数组中有交集的元素?
3、php如何在数组头部和尾部及任意位置插入元素?
4、php如何将二位数组按某一个或多个字段值(升序/降序)排序?数字索引被重置,关联索引保持不变
5、php如何实现APP版本号的比对?
6、php如何获取视频封面图?
7、php中的六种加密解密算法
8、php如何方式SQL注入?
9、php如何将模板标签替换为指定内容?
10、php如何获取当前页面的完整url?
11、php如何强制下载文件?
12、php截取字符串长度(含中文)
13、php如何获取客户端真实IP?
14、php如何记录日志信息到文件中?
15、php如何防止重复提交表单?令牌方式
1、如何在文章列表中任意位置或固定位置插入新的文章?比如:3、6位置
- <?php
- /**
- *需求如下:
- *1、在文章列表中的第3、6位置插入新的文章
- *2、插入的新文章不能出现在文章列表中的头部和尾部
- *3、文章列表中轮询显示最新发布的前10篇新文章(即:插入的新文章),每次显示2篇
- *4、如果新文章数量不小于10篇,那么则轮询显示;反之则只显示一遍
- *
- **/
- $pageNumber=$this->input->get_post("pageNumber",true);//上拉、下拉次数
- $contentList=$this->article_model->getArticleByCategory2($cateId,0,$offset);//$offset=8
- $contentList=$this->_getContentList($contentList);//文章列表
- $cache_num=10;//缓存最新的10条新文章到mc
- $size=2;//每次从mc获取2篇新文章
- $max_times=$cache_num/$size;//5次取完
- if($pageNumber<=$max_times){//下拉上拉次数小于等于5的情况,比如:"1、2、3、4、5、..."
- $offset_1=($pageNumber-1)*$size;//查询的开始位置,比如:"0、2、4、6、8"
- }else{
- if($pageNumber%$max_times){//下拉上拉次数大于5并且不能被5整除的情况,比如:"6、7、8、9、11、..."
- $num=$this->article_model->getPublishCountArticleByCategory($cateId);//获取mc中数据量
- if($num>=$cache_num){//mc中新文章大于等于10篇的情况
- $offset_1=($pageNumber%$max_times-1)*$size;//轮询
- }else{//mc中新文章小于10篇的情况
- $offset_1=$max_times*2;//不轮询
- }
- }else{//下拉上拉次数大于5并且正好能被5整除的情况,比如:"10、15、20、25、30、..."
- $num=$this->article_model->getPublishCountArticleByCategory($cateId);//获取mc中数据量
- if($num>=$cache_num){//mc中新文章大于等于10篇的情况
- $offset_1=$cache_num-$size;//轮询
- }else{//mc中新文章小于10篇的情况
- $offset_1=$max_times*2;//不轮询
- }
- }
- }
- $publishList=$this->article_model->getPublishArticleByCategory($cateId,$offset_1,$size,$cache_num);//随机获取两条新文章
- $publishList=$this->_getPublishList($publishList);//从mc中获取2篇新文章插入到文章列表中
- $content_count=count($contentlist);
- $publish_count=count($publishlist);
- if(!empty($publishList)){//3、6位置是预留位置
- if(($content_count>=3)&&($publish_count>=1)){
- $publishList_new[0]=$publishList[0];//组装成一个二维数组
- array_splice($contentList,3-1,0,$publishList_new);//第3的位置(索引为3-1)插入第1篇新文章
- }
- if(($content_count>=6)&&($publish_count>=2)){
- $publishList_new[0]=$publishList[1];//组装成一个二维数组
- array_splice($contentList,6-1,0,$publishList_new);//第6的位置(索引为6-1)插入第2篇新文章
- }
- }
- ?>
2、如何删除两个数组中有交集的元素?
- foreach($content_list_temp_recommendas$k=>$v){
- $kk=array_search($v[‘aid‘],$aid_arr_temp);//$v[‘aid‘]必定是$aid_arr_temp数组内元素之一的情况
- $msg.=$aid_arr_temp[$kk].",";
- if($kk!==false){//只要不是false就是找到了
- unset($aid_arr_temp[$kk]);//删除后,索引键保持不变
- }
- }
- $aid_arr=array_values($aid_arr_temp);//经过array_values()函数处理过后,索引键重新分配。
3、如何在数组头部和尾部及任意位置插入元素?
- ①插入元素
- array_unshift();//在数组头部插入一个或多个元素
- array_push();//在数组尾部插入一个或多个元素
- array_splice($arr,$start,0,$arr1);//在数组的第$start+1个位置插入新元素(指的是头部和中部任意位置,但不包括尾部),注意:参数3一定要是0
- ②删除元素
- array_shift();//删除数组中首个元素,并返回删除后的值
- array_pop();//删除数组的最后一个元素(出栈),并返回删除后的值
4、如何将二位数组按某一个或多个字段值(升序/降序)排序?数字索引被重置,关联索引保持不变
- $arr=array(
- array(‘id‘=>1,‘name‘=>‘will‘,‘age‘=>23),
- array(‘id‘=>2,‘name‘=>‘myth‘,‘age‘=>32),
- array(‘id‘=>3,‘name‘=>‘allen‘,‘age‘=>27),
- array(‘id‘=>4,‘name‘=>‘martin‘,‘age‘=>23)
- );
- foreach($arras$k=>$v){
- $tag1[]=$v[‘age‘];//age排序字段
- $tag2[]=$v[‘id‘];//id排序字段
- }
- //相当于select*from$arrorderby$tag1DESC,$tag2ASC;//特点:$tag1、$tag2、$arr数组的元素个数必须要一致
- array_multisort($tag1,SORT_DESC,$tag2,SORT_ASC,$arr);//根据年龄从大到小排列,年龄相同则按id升序排列
- echo"<pre>";print_r($arr);exit;
- ?>
- //php二维数组如何按照指定列进行排序?
- functionarrSortByField(&$list,$field,$call_func=NULL,$sort_type=SORT_ASC){//引用传值
- $sort_filed=array();
- foreach($listas$val){
- if(!isset($val[$field]))returnfalse;
- $sort_filed[]=is_null($call_func)?$val[$field]:call_user_func($call_func,$val[$field]);
- }
- returnarray_multisort($sort_field,$sort_type,$list);//$list顺序会随$sort_field顺序变化而变化
- }
- $list=array(
- array(‘id‘=>3,‘name‘=>‘asdfsdf‘),
- array(‘id‘=>1,‘name‘=>‘12‘),
- array(‘id‘=>4,‘name‘=>‘10sdf‘),
- array(‘id‘=>2,‘name‘=>‘ada‘),
- array(‘id‘=>5,‘name‘=>‘aasdfbc‘)
- );
- arrSortByField($list,‘name‘,‘strlen‘);//按照"name"列的值长度进行排序
- echo"<pre>";print_r($list);
- arrSortByField($list,‘id‘);//按照"id"列的值大小进行排序
- echo"<pre>";print_r($list);
- ?>
5、APP版本号的比
- <?php
- header("content-type:text/html;charset=utf-8");
- date_default_timezone_set(‘Asia/Shanghai‘);
- function_diffVersion($current,$update){
- if($current=="null"){
- returnfalse;
- }
- $currentVersion=getVersion($current);
- $updateVersion=getVersion($update);
- if($currentVersion[‘mainVersion‘]<$updateVersion[‘mainVersion‘]){
- returntrue;
- }elseif($currentVersion[‘mainVersion‘]==$updateVersion[‘mainVersion‘]){
- if($currentVersion[‘minVersion‘]<$updateVersion[‘minVersion‘]){
- returntrue;
- }elseif($currentVersion[‘minVersion‘]>$updateVersion[‘minVersion‘]){
- returnfalse;
- }
- if($currentVersion[‘fixVersion‘]<$updateVersion[‘fixVersion‘]){
- returntrue;
- }
- }
- returnfalse;
- }
- functiongetVersion($version){
- $result=array();
- if(strstr($version,".")){
- $data=explode(".",$version);
- $result[‘mainVersion‘]=$data[0];
- if(isset($data[1])){
- $result[‘minVersion‘]=$data[1];
- }else{
- $result[‘minVersion‘]=0;
- }
- if(isset($data[2])){
- $result[‘fixVersion‘]=$data[2];
- }else{
- $result[‘fixVersion‘]=0;
- }
- }
- return$result;
- }
- echo"<pre>";print_r(_diffVersion("2.0.0","2.0.01"));//true-需要升级false-不升级
- ?>
6、获取视频封面图
- <?php
- header("content-type:text/html;charset=utf-8");
- date_default_timezone_set(‘Asia/Shanghai‘);
- functiongetCoverImages($fileUrl){
- $result=array();
- if(!empty($fileUrl)){
- $filePath=str_replace("http://img.baidu.cn/","/data/images/",$fileUrl);
- if(is_file($filePath)){
- $result=execCommandLine($filePath);
- }
- }
- returnjson_encode($result);
- }
- functionexecCommandLine($file){
- $result=array();
- $pathParts=pathinfo($file);
- $filename=$pathParts[‘dirname‘]."/".$pathParts[‘filename‘]."_";
- $times=array(8,15,25);
- foreach($timesas$k=>$v){
- $destFilePath=$filename.$v.".jpg";
- $command="/usr/bin/ffmpeg-i{$file}-y-fimage2-ss{$v}-vframes1-s640x360{$destFilePath}";
- exec($command);
- //chmod($filename.$v."jpg",0644);
- $destUrlPath=str_replace("/data/images/","http://img.baidu.cn/",$destFilePath);
- $selected=$k==0?"1":"0";//默认将第一张图片作为封面图
- array_push($result,array($destUrlPath,$selected));
- }
- return$result;
- }
- $fileUrl="http://img.baidu.cn/14221916FLVSDT1.mp4"
- getCoverImages($fileUrl);//截取第8、15、25秒为封面图
- ?>
7、php加密解密:php加密和解密函数通常可以用来加密一些有用的字符串存放在数据库里或作为各个子系统间同步登陆的令牌,并且通过解密算法解密字符串,该函数使用了base64和MD5加密和解密。
①第一种加密解密算法
- <?php
- functionencryptDecrypt($key,$string,$decrypt){
- if($decrypt){
- $decrypted=rtrim(mcrypt_decrypt(MCRYPT_RIJNDAEL_256,md5($key),base64_decode($string),MCRYPT_MODE_CBC,md5(md5($key))),"12");
- return$decrypted;
- }else{
- $encrypted=base64_encode(mcrypt_encrypt(MCRYPT_RIJNDAEL_256,md5($key),$string,MCRYPT_MODE_CBC,md5(md5($key))));
- return$encrypted;
- }
- }
- //加密:"z0JAx4qMwcF+db5TNbp/xwdUM84snRsXvvpXuaCa4Bk="
- echoencryptDecrypt(‘password‘,‘Helloweba欢迎您‘,0);
- //解密:"Helloweba欢迎您"
- echoencryptDecrypt(‘password‘,‘z0JAx4qMwcF+db5TNbp/xwdUM84snRsXvvpXuaCa4Bk=‘,1);
- ?>
②第二种加密解密算法:
- <?php
- //加密函数
- functionlock_url($txt,$key=‘www.zhuoyuexiazai.com‘){
- $chars="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-=+";
- $nh=rand(0,64);
- $ch=$chars[$nh];
- $mdKey=md5($key.$ch);
- $mdKey=substr($mdKey,$nh%8,$nh%8+7);
- $txt=base64_encode($txt);
- $tmp=‘‘;
- $i=0;$j=0;$k=0;
- for($i=0;$i<strlen($txt);$i++){
- $k=$k==strlen($mdKey)?0:$k;
- $j=($nh+strpos($chars,$txt[$i])+ord($mdKey[$k++]))%64;
- $tmp.=$chars[$j];
- }
- returnurlencode($ch.$tmp);
- }
- //解密函数
- functionunlock_url($txt,$key=‘www.zhuoyuexiazai.com‘){
- $txt=urldecode($txt);
- $chars="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-=+";
- $ch=$txt[0];
- $nh=strpos($chars,$ch);
- $mdKey=md5($key.$ch);
- $mdKey=substr($mdKey,$nh%8,$nh%8+7);
- $txt=substr($txt,1);
- $tmp=‘‘;
- $i=0;$j=0;$k=0;
- for($i=0;$i<strlen($txt);$i++){
- $k=$k==strlen($mdKey)?0:$k;
- $j=strpos($chars,$txt[$i])-$nh-ord($mdKey[$k++]);
- while($j<0)$j+=64;
- $tmp.=$chars[$j];
- }
- returnbase64_decode($tmp);
- }
- ?>
- <?php
- //改进后的算法
- //加密函数
- functionlock_url($txt,$key=‘zhuoyuexiazai‘){
- $txt=$txt.$key;
- $chars="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-=+";
- $nh=rand(0,64);
- $ch=$chars[$nh];
- $mdKey=md5($key.$ch);
- $mdKey=substr($mdKey,$nh%8,$nh%8+7);
- $txt=base64_encode($txt);
- $tmp=‘‘;
- $i=0;$j=0;$k=0;
- for($i=0;$i<strlen($txt);$i++){
- $k=$k==strlen($mdKey)?0:$k;
- $j=($nh+strpos($chars,$txt[$i])+ord($mdKey[$k++]))%64;
- $tmp.=$chars[$j];
- }
- returnurlencode(base64_encode($ch.$tmp));
- }
- //解密函数
- functionunlock_url($txt,$key=‘zhuoyuexiazai‘){
- $txt=base64_decode(urldecode($txt));
- $chars="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-=+";
- $ch=$txt[0];
- $nh=strpos($chars,$ch);
- $mdKey=md5($key.$ch);
- $mdKey=substr($mdKey,$nh%8,$nh%8+7);
- $txt=substr($txt,1);
- $tmp=‘‘;
- $i=0;$j=0;$k=0;
- for($i=0;$i<strlen($txt);$i++){
- $k=$k==strlen($mdKey)?0:$k;
- $j=strpos($chars,$txt[$i])-$nh-ord($mdKey[$k++]);
- while($j<0)$j+=64;
- $tmp.=$chars[$j];
- }
- returntrim(base64_decode($tmp),$key);
- }
- ?>
- <?php
- functionpassport_encrypt($txt,$key=‘www.zhuoyuexiazai.com‘){
- srand((double)microtime()*1000000);
- $encrypt_key=md5(rand(0,32000));
- $ctr=0;
- $tmp=‘‘;
- for($i=0;$i<strlen($txt);$i++){
- $ctr=$ctr==strlen($encrypt_key)?0:$ctr;
- $tmp.=$encrypt_key[$ctr].($txt[$i]^$encrypt_key[$ctr++]);
- }
- returnurlencode(base64_encode(passport_key($tmp,$key)));
- }
- functionpassport_decrypt($txt,$key=‘www.zhuoyuexiazai.com‘){
- $txt=passport_key(base64_decode(urldecode($txt)),$key);
- $tmp=‘‘;
- for($i=0;$i<strlen($txt);$i++){
- $md5=$txt[$i];
- $tmp.=$txt[++$i]^$md5;
- }
- return$tmp;
- }
- functionpassport_key($txt,$encrypt_key){
- $encrypt_key=md5($encrypt_key);
- $ctr=0;
- $tmp=‘‘;
- for($i=0;$i<strlen($txt);$i++){
- $ctr=$ctr==strlen($encrypt_key)?0:$ctr;
- $tmp.=$txt[$i]^$encrypt_key[$ctr++];
- }
- return$tmp;
- }
- $txt="1";
- $key="testkey";
- $encrypt=passport_encrypt($txt,$key);
- $decrypt=passport_decrypt($encrypt,$key);
- echo$encrypt."<br>";
- echo$decrypt."<br>";
- ?>
项目中有时我们需要使用PHP将特定的信息进行加密,也就是通过加密算法生成一个加密字符串,这个加密后的字符串可以通过解密算法进行解密,便于程序对解密后的信息进行处理。最常见的应用在用户登录以及一些API数据交换的场景。最常见的应用在用户登录以及一些API数据交换的场景。加密解密原理一般都是通过一定的加密解密算法,将密钥加入到算法中,最终得到加密解密结果。
- <?php
- //非常给力的authcode加密函数,Discuz!经典代码(带详解)
- //函数authcode($string,$operation,$key,$expiry)中的$string:字符串,明文或密文;$operation:DECODE表示解密,其它表示加密;$key:密匙;$expiry:密文有效期。
- functionauthcode($string,$operation=‘DECODE‘,$key=‘‘,$expiry=0){
- //动态密匙长度,相同的明文会生成不同密文就是依靠动态密匙
- $ckey_length=4;
- &nbs
知识推荐
- Mac系统下搭建PHP环境
- 06_Flume_interceptor_时间戳+Host
- httpd: Could not reliably determine the server's fully
- js实现选项卡功能
- 第96天:CSS3 ?背景详解
- Apache Maven 入门篇
- ECharts.js学习(三)交互组件
- Asp.Net Core中使用Newtonsoft.Json进行序列化处理解决返回值首字母小写
- web.xml is missing and <failOnMissingWebXml> is set to true
- JS基本语法
- [.Net跨平台]部署DTCMS到Jexus遇到的问题及解决思路---部署
- Vue.js 第2篇学习笔记
- 使用git上传本地项目到GitHub上和更新
- 网站的robots.txt文件
- 为什么利用多个域名来存储网站资源会更有效?
- nodejs基础: 如何升级Noejs版本
- jQuery清空表单方法
- Cannot retrieve metalink for repository: epel 错误解决办法