分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 技术分享

PHP常见面试题

发布时间:2023-09-06 01:27责任编辑:沈小雨关键词:PHP面试题

【总结】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位置

  1. <?php
  2. /**
  3. *需求如下:
  4. *1、在文章列表中的第3、6位置插入新的文章
  5. *2、插入的新文章不能出现在文章列表中的头部和尾部
  6. *3、文章列表中轮询显示最新发布的前10篇新文章(即:插入的新文章),每次显示2篇
  7. *4、如果新文章数量不小于10篇,那么则轮询显示;反之则只显示一遍
  8. *
  9. **/
  10. $pageNumber=$this->input->get_post("pageNumber",true);//上拉、下拉次数
  11. $contentList=$this->article_model->getArticleByCategory2($cateId,0,$offset);//$offset=8
  12. $contentList=$this->_getContentList($contentList);//文章列表
  13. $cache_num=10;//缓存最新的10条新文章到mc
  14. $size=2;//每次从mc获取2篇新文章
  15. $max_times=$cache_num/$size;//5次取完
  16. if($pageNumber<=$max_times){//下拉上拉次数小于等于5的情况,比如:"1、2、3、4、5、..."
  17. $offset_1=($pageNumber-1)*$size;//查询的开始位置,比如:"0、2、4、6、8"
  18. }else{
  19. if($pageNumber%$max_times){//下拉上拉次数大于5并且不能被5整除的情况,比如:"6、7、8、9、11、..."
  20. $num=$this->article_model->getPublishCountArticleByCategory($cateId);//获取mc中数据量
  21. if($num>=$cache_num){//mc中新文章大于等于10篇的情况
  22. $offset_1=($pageNumber%$max_times-1)*$size;//轮询
  23. }else{//mc中新文章小于10篇的情况
  24. $offset_1=$max_times*2;//不轮询
  25. }
  26. }else{//下拉上拉次数大于5并且正好能被5整除的情况,比如:"10、15、20、25、30、..."
  27. $num=$this->article_model->getPublishCountArticleByCategory($cateId);//获取mc中数据量
  28. if($num>=$cache_num){//mc中新文章大于等于10篇的情况
  29. $offset_1=$cache_num-$size;//轮询
  30. }else{//mc中新文章小于10篇的情况
  31. $offset_1=$max_times*2;//不轮询
  32. }
  33. }
  34. }
  35. $publishList=$this->article_model->getPublishArticleByCategory($cateId,$offset_1,$size,$cache_num);//随机获取两条新文章
  36. $publishList=$this->_getPublishList($publishList);//从mc中获取2篇新文章插入到文章列表中
  37. $content_count=count($contentlist);
  38. $publish_count=count($publishlist);
  39. if(!empty($publishList)){//3、6位置是预留位置
  40. if(($content_count>=3)&&($publish_count>=1)){
  41. $publishList_new[0]=$publishList[0];//组装成一个二维数组
  42. array_splice($contentList,3-1,0,$publishList_new);//第3的位置(索引为3-1)插入第1篇新文章
  43. }
  44. if(($content_count>=6)&&($publish_count>=2)){
  45. $publishList_new[0]=$publishList[1];//组装成一个二维数组
  46. array_splice($contentList,6-1,0,$publishList_new);//第6的位置(索引为6-1)插入第2篇新文章
  47. }
  48. }
  49. ?>

2、如何删除两个数组中有交集的元素?

  1. foreach($content_list_temp_recommendas$k=>$v){
  2. $kk=array_search($v[‘aid‘],$aid_arr_temp);//$v[‘aid‘]必定是$aid_arr_temp数组内元素之一的情况
  3. $msg.=$aid_arr_temp[$kk].",";
  4. if($kk!==false){//只要不是false就是找到了
  5. unset($aid_arr_temp[$kk]);//删除后,索引键保持不变
  6. }
  7. }
  8. $aid_arr=array_values($aid_arr_temp);//经过array_values()函数处理过后,索引键重新分配。

3、如何在数组头部和尾部及任意位置插入元素?

  1. ①插入元素
  2. array_unshift();//在数组头部插入一个或多个元素
  3. array_push();//在数组尾部插入一个或多个元素
  4. array_splice($arr,$start,0,$arr1);//在数组的第$start+1个位置插入新元素(指的是头部和中部任意位置,但不包括尾部),注意:参数3一定要是0
  5. ②删除元素
  6. array_shift();//删除数组中首个元素,并返回删除后的值
  7. array_pop();//删除数组的最后一个元素(出栈),并返回删除后的值

4、如何将二位数组按某一个或多个字段值(升序/降序)排序?数字索引被重置,关联索引保持不变

  1. $arr=array(
  2. array(‘id‘=>1,‘name‘=>‘will‘,‘age‘=>23),
  3. array(‘id‘=>2,‘name‘=>‘myth‘,‘age‘=>32),
  4. array(‘id‘=>3,‘name‘=>‘allen‘,‘age‘=>27),
  5. array(‘id‘=>4,‘name‘=>‘martin‘,‘age‘=>23)
  6. );
  7. foreach($arras$k=>$v){
  8. $tag1[]=$v[‘age‘];//age排序字段
  9. $tag2[]=$v[‘id‘];//id排序字段
  10. }
  11. //相当于select*from$arrorderby$tag1DESC,$tag2ASC;//特点:$tag1、$tag2、$arr数组的元素个数必须要一致
  12. array_multisort($tag1,SORT_DESC,$tag2,SORT_ASC,$arr);//根据年龄从大到小排列,年龄相同则按id升序排列
  13. echo"<pre>";print_r($arr);exit;
  14. ?>
<?php
  1. //php二维数组如何按照指定列进行排序?
  2. functionarrSortByField(&$list,$field,$call_func=NULL,$sort_type=SORT_ASC){//引用传值
  3. $sort_filed=array();
  4. foreach($listas$val){
  5. if(!isset($val[$field]))returnfalse;
  6. $sort_filed[]=is_null($call_func)?$val[$field]:call_user_func($call_func,$val[$field]);
  7. }
  8. returnarray_multisort($sort_field,$sort_type,$list);//$list顺序会随$sort_field顺序变化而变化
  9. }
  10. $list=array(
  11. array(‘id‘=>3,‘name‘=>‘asdfsdf‘),
  12. array(‘id‘=>1,‘name‘=>‘12‘),
  13. array(‘id‘=>4,‘name‘=>‘10sdf‘),
  14. array(‘id‘=>2,‘name‘=>‘ada‘),
  15. array(‘id‘=>5,‘name‘=>‘aasdfbc‘)
  16. );
  17. arrSortByField($list,‘name‘,‘strlen‘);//按照"name"列的值长度进行排序
  18. echo"<pre>";print_r($list);
  19. arrSortByField($list,‘id‘);//按照"id"列的值大小进行排序
  20. echo"<pre>";print_r($list);
  21. ?>

5、APP版本号的比

  1. <?php
  2. header("content-type:text/html;charset=utf-8");
  3. date_default_timezone_set(‘Asia/Shanghai‘);
  4. function_diffVersion($current,$update){
  5. if($current=="null"){
  6. returnfalse;
  7. }
  8. $currentVersion=getVersion($current);
  9. $updateVersion=getVersion($update);
  10. if($currentVersion[‘mainVersion‘]<$updateVersion[‘mainVersion‘]){
  11. returntrue;
  12. }elseif($currentVersion[‘mainVersion‘]==$updateVersion[‘mainVersion‘]){
  13. if($currentVersion[‘minVersion‘]<$updateVersion[‘minVersion‘]){
  14. returntrue;
  15. }elseif($currentVersion[‘minVersion‘]>$updateVersion[‘minVersion‘]){
  16. returnfalse;
  17. }
  18. if($currentVersion[‘fixVersion‘]<$updateVersion[‘fixVersion‘]){
  19. returntrue;
  20. }
  21. }
  22. returnfalse;
  23. }
  24. functiongetVersion($version){
  25. $result=array();
  26. if(strstr($version,".")){
  27. $data=explode(".",$version);
  28. $result[‘mainVersion‘]=$data[0];
  29. if(isset($data[1])){
  30. $result[‘minVersion‘]=$data[1];
  31. }else{
  32. $result[‘minVersion‘]=0;
  33. }
  34. if(isset($data[2])){
  35. $result[‘fixVersion‘]=$data[2];
  36. }else{
  37. $result[‘fixVersion‘]=0;
  38. }
  39. }
  40. return$result;
  41. }
  42. echo"<pre>";print_r(_diffVersion("2.0.0","2.0.01"));//true-需要升级false-不升级
  43. ?>

6、获取视频封面图

  1. <?php
  2. header("content-type:text/html;charset=utf-8");
  3. date_default_timezone_set(‘Asia/Shanghai‘);
  4. functiongetCoverImages($fileUrl){
  5. $result=array();
  6. if(!empty($fileUrl)){
  7. $filePath=str_replace("http://img.baidu.cn/","/data/images/",$fileUrl);
  8. if(is_file($filePath)){
  9. $result=execCommandLine($filePath);
  10. }
  11. }
  12. returnjson_encode($result);
  13. }
  14. functionexecCommandLine($file){
  15. $result=array();
  16. $pathParts=pathinfo($file);
  17. $filename=$pathParts[‘dirname‘]."/".$pathParts[‘filename‘]."_";
  18. $times=array(8,15,25);
  19. foreach($timesas$k=>$v){
  20. $destFilePath=$filename.$v.".jpg";
  21. $command="/usr/bin/ffmpeg-i{$file}-y-fimage2-ss{$v}-vframes1-s640x360{$destFilePath}";
  22. exec($command);
  23. //chmod($filename.$v."jpg",0644);
  24. $destUrlPath=str_replace("/data/images/","http://img.baidu.cn/",$destFilePath);
  25. $selected=$k==0?"1":"0";//默认将第一张图片作为封面图
  26. array_push($result,array($destUrlPath,$selected));
  27. }
  28. return$result;
  29. }
  30. $fileUrl="http://img.baidu.cn/14221916FLVSDT1.mp4"
  31. getCoverImages($fileUrl);//截取第8、15、25秒为封面图
  32. ?>

7、php加密解密:php加密和解密函数通常可以用来加密一些有用的字符串存放在数据库里或作为各个子系统间同步登陆的令牌,并且通过解密算法解密字符串,该函数使用了base64和MD5加密和解密。

①第一种加密解密算法

  1. <?php
  2. functionencryptDecrypt($key,$string,$decrypt){
  3. if($decrypt){
  4. $decrypted=rtrim(mcrypt_decrypt(MCRYPT_RIJNDAEL_256,md5($key),base64_decode($string),MCRYPT_MODE_CBC,md5(md5($key))),"12");
  5. return$decrypted;
  6. }else{
  7. $encrypted=base64_encode(mcrypt_encrypt(MCRYPT_RIJNDAEL_256,md5($key),$string,MCRYPT_MODE_CBC,md5(md5($key))));
  8. return$encrypted;
  9. }
  10. }
  11. //加密:"z0JAx4qMwcF+db5TNbp/xwdUM84snRsXvvpXuaCa4Bk="
  12. echoencryptDecrypt(‘password‘,‘Helloweba欢迎您‘,0);
  13. //解密:"Helloweba欢迎您"
  14. echoencryptDecrypt(‘password‘,‘z0JAx4qMwcF+db5TNbp/xwdUM84snRsXvvpXuaCa4Bk=‘,1);
  15. ?>

②第二种加密解密算法:

  1. <?php
  2. //加密函数
  3. functionlock_url($txt,$key=‘www.zhuoyuexiazai.com‘){
  4. $chars="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-=+";
  5. $nh=rand(0,64);
  6. $ch=$chars[$nh];
  7. $mdKey=md5($key.$ch);
  8. $mdKey=substr($mdKey,$nh%8,$nh%8+7);
  9. $txt=base64_encode($txt);
  10. $tmp=‘‘;
  11. $i=0;$j=0;$k=0;
  12. for($i=0;$i<strlen($txt);$i++){
  13. $k=$k==strlen($mdKey)?0:$k;
  14. $j=($nh+strpos($chars,$txt[$i])+ord($mdKey[$k++]))%64;
  15. $tmp.=$chars[$j];
  16. }
  17. returnurlencode($ch.$tmp);
  18. }
  19. //解密函数
  20. functionunlock_url($txt,$key=‘www.zhuoyuexiazai.com‘){
  21. $txt=urldecode($txt);
  22. $chars="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-=+";
  23. $ch=$txt[0];
  24. $nh=strpos($chars,$ch);
  25. $mdKey=md5($key.$ch);
  26. $mdKey=substr($mdKey,$nh%8,$nh%8+7);
  27. $txt=substr($txt,1);
  28. $tmp=‘‘;
  29. $i=0;$j=0;$k=0;
  30. for($i=0;$i<strlen($txt);$i++){
  31. $k=$k==strlen($mdKey)?0:$k;
  32. $j=strpos($chars,$txt[$i])-$nh-ord($mdKey[$k++]);
  33. while($j<0)$j+=64;
  34. $tmp.=$chars[$j];
  35. }
  36. returnbase64_decode($tmp);
  37. }
  38. ?>
③第三种加密解密算法:
  1. <?php
  2. //改进后的算法
  3. //加密函数
  4. functionlock_url($txt,$key=‘zhuoyuexiazai‘){
  5. $txt=$txt.$key;
  6. $chars="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-=+";
  7. $nh=rand(0,64);
  8. $ch=$chars[$nh];
  9. $mdKey=md5($key.$ch);
  10. $mdKey=substr($mdKey,$nh%8,$nh%8+7);
  11. $txt=base64_encode($txt);
  12. $tmp=‘‘;
  13. $i=0;$j=0;$k=0;
  14. for($i=0;$i<strlen($txt);$i++){
  15. $k=$k==strlen($mdKey)?0:$k;
  16. $j=($nh+strpos($chars,$txt[$i])+ord($mdKey[$k++]))%64;
  17. $tmp.=$chars[$j];
  18. }
  19. returnurlencode(base64_encode($ch.$tmp));
  20. }
  21. //解密函数
  22. functionunlock_url($txt,$key=‘zhuoyuexiazai‘){
  23. $txt=base64_decode(urldecode($txt));
  24. $chars="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-=+";
  25. $ch=$txt[0];
  26. $nh=strpos($chars,$ch);
  27. $mdKey=md5($key.$ch);
  28. $mdKey=substr($mdKey,$nh%8,$nh%8+7);
  29. $txt=substr($txt,1);
  30. $tmp=‘‘;
  31. $i=0;$j=0;$k=0;
  32. for($i=0;$i<strlen($txt);$i++){
  33. $k=$k==strlen($mdKey)?0:$k;
  34. $j=strpos($chars,$txt[$i])-$nh-ord($mdKey[$k++]);
  35. while($j<0)$j+=64;
  36. $tmp.=$chars[$j];
  37. }
  38. returntrim(base64_decode($tmp),$key);
  39. }
  40. ?>
④第四种加密解密算法:
  1. <?php
  2. functionpassport_encrypt($txt,$key=‘www.zhuoyuexiazai.com‘){
  3. srand((double)microtime()*1000000);
  4. $encrypt_key=md5(rand(0,32000));
  5. $ctr=0;
  6. $tmp=‘‘;
  7. for($i=0;$i<strlen($txt);$i++){
  8. $ctr=$ctr==strlen($encrypt_key)?0:$ctr;
  9. $tmp.=$encrypt_key[$ctr].($txt[$i]^$encrypt_key[$ctr++]);
  10. }
  11. returnurlencode(base64_encode(passport_key($tmp,$key)));
  12. }
  13. functionpassport_decrypt($txt,$key=‘www.zhuoyuexiazai.com‘){
  14. $txt=passport_key(base64_decode(urldecode($txt)),$key);
  15. $tmp=‘‘;
  16. for($i=0;$i<strlen($txt);$i++){
  17. $md5=$txt[$i];
  18. $tmp.=$txt[++$i]^$md5;
  19. }
  20. return$tmp;
  21. }
  22. functionpassport_key($txt,$encrypt_key){
  23. $encrypt_key=md5($encrypt_key);
  24. $ctr=0;
  25. $tmp=‘‘;
  26. for($i=0;$i<strlen($txt);$i++){
  27. $ctr=$ctr==strlen($encrypt_key)?0:$ctr;
  28. $tmp.=$txt[$i]^$encrypt_key[$ctr++];
  29. }
  30. return$tmp;
  31. }
  32. $txt="1";
  33. $key="testkey";
  34. $encrypt=passport_encrypt($txt,$key);
  35. $decrypt=passport_decrypt($encrypt,$key);
  36. echo$encrypt."<br>";
  37. echo$decrypt."<br>";
  38. ?>
⑤第五种加密解密算法:discuz中使用的加密解密算法

项目中有时我们需要使用PHP将特定的信息进行加密,也就是通过加密算法生成一个加密字符串,这个加密后的字符串可以通过解密算法进行解密,便于程序对解密后的信息进行处理。最常见的应用在用户登录以及一些API数据交换的场景。最常见的应用在用户登录以及一些API数据交换的场景。加密解密原理一般都是通过一定的加密解密算法,将密钥加入到算法中,最终得到加密解密结果。

  1. <?php
  2. //非常给力的authcode加密函数,Discuz!经典代码(带详解)
  3. //函数authcode($string,$operation,$key,$expiry)中的$string:字符串,明文或密文;$operation:DECODE表示解密,其它表示加密;$key:密匙;$expiry:密文有效期。
  4. functionauthcode($string,$operation=‘DECODE‘,$key=‘‘,$expiry=0){
  5. //动态密匙长度,相同的明文会生成不同密文就是依靠动态密匙
  6. $ckey_length=4;
  7. &nbs
我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved