前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >程序员面试必备PHP基础面试题 – 第十四天

程序员面试必备PHP基础面试题 – 第十四天

作者头像
PHP学习网
发布2022-08-03 14:22:54
3200
发布2022-08-03 14:22:54
举报
文章被收录于专栏:PHP学习网PHP学习网

一、设已知目录/data1/somedir, 写一个函数, 遍历取得该目录下包含子目录所在后缀为txt的文件.

代码语言:javascript
复制
function get_dir($dir){
        if(!is_dir($dir) || !file_exists($dir)){
            exit(‘不是目录或目录不存在’);
}
$dd=opendir($dir);
readdir($dd);
readdir($dd);
while($f=readdir($dd)){
    $file=rtrim($dir,’/’).’/’.$f;
    if(pathinfo($file,PATHINFO_EXTENSION)==’txt’){
        $str.=$file.’,’;
}
if(is_dir($file)){
    $str.=$file.’,’;
    $str.=get_dir($file);
}
}
close($dd);
return $str;
}
$str=rtrim(get_dir(‘/data1/somedir’),’,’);
print_r(explode(‘,’,$str));

二、写一个函数, 算出两个文件的相对路径, 如$a = ‘/a/b/c/d/e.php’; $b= ‘/a/b/12/34/c.php’; 计算出$b 相对于$a 的相对路径 应该是../../c/d 将()添加上

代码语言:javascript
复制
 $a = '/a/b/c/d/e.php';
  $b = '/a/b/12/34/c.php';
  getpathinfo($a, $b);
  function getpathinfo( $a, $b ) {
      $a2array = explode('/', $a);
      $b2array = explode('/', $b);
      $pathinfo = '';
      for( $i = 1; $i <= count($b2array); $i++ ) {
$pathinfo.=$a2array[$i] == $b2array[$i] ? '../' : $b2array[$i].'/';
      }
      print_R($pathinfo);
  }

三、假设某论坛 url http://test.com/login.php 为注册用户入口地址, 请用程序实现摸拟注册用户的过程, 成功之后到http://test.com/thread.php?id=100的版面发一篇帖子, 需要考虑有图形验证码的情况,验证码如:9679

答:采用curl模拟登陆操作

代码语言:javascript
复制
第一:分析登陆字段
第二:登陆后保留COOKIE
第三:读取COOKIE并跳转到相关页
第四:抓取数据
代码语言:javascript
复制
<?php
$bbs_url = 'http://test.com/ ';//论坛地址
$login_url = $bbs_url .' login.php ';//登录页地址
$post_fields = array();
//以下两项不需要修改
$post_fields['loginfield'] = 'username';
$post_fields['loginsubmit'] = 'true';
//用户名和密码,必须填写
$post_fields['username'] = 'tianxin';
$post_fields['password'] = '111111';
//安全提问
$post_fields['questionid'] = 0;
$post_fields['answer'] = '';
//@todo验证码
$post_fields['seccodeverify'] = '';

//获取表单FORMHASH
$ch = curl_init($login_url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$contents = curl_exec($ch);
curl_close($ch);
preg_match('/<input\s*type="hidden"\s*name="formhash"\s*value="(.*?)"\s*\/>/i', $contents, $matches);
if(!empty($matches)) {
    $formhash = $matches[1];
} else {
    die('Not found the forumhash.');
}

//POST数据,获取COOKIE,cookie文件放在网站的temp目录下
$cookie_file = tempnam('./temp','cookie');

$ch = curl_init($login_url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_fields);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file);
curl_exec($ch);
curl_close($ch);

//取到了关键的cookie文件就可以带着cookie文件去模拟发帖.
$send_url = $bbs_url." thread.php?id=100";
$ch = curl_init($send_url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file);
$contents = curl_exec($ch);
curl_close($ch);

//这里的hash码和登陆窗口的hash码的正则不太一样,这里的hidden多了一个id属性
preg_match('/<input\s*type="hidden"\s*name="formhash"\s*id="formhash"\s*value="(.*?)"\s*\/>/i', $contents, $matches);
if(!empty($matches)) {
    $formhash = $matches[1];
} else {
    die('Not found the forumhash.');
}
$post_data = array();
//帖子标题
$post_data['subject'] = 'test2';
//帖子内容
$post_data['message'] = 'test2';
$post_data['topicsubmit'] = "yes";
$post_data['extra'] = '';
//帖子标签
$post_data['tags'] = 'test';
//帖子的hash码,这个非常关键!假如缺少这个hash码,会警告你来路的页面不正确
$post_data['formhash']=$formhash;
$ch = curl_init($send_url);
curl_setopt($ch, CURLOPT_REFERER, $send_url);       //伪装REFERER
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 0);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
$contents = curl_exec($ch);
curl_close($ch);
//清理cookie文件
unlink($cookie_file);

?>

四、设计一个类, 实现用户管理,需求如下(写出文体结构即可)

1.用文件存储用户 信息,用户注册输入用户 名,密码和电子邮件;2.注册后需要通过发送电子邮件来验证用户的信息真实和有效;3.密码需要加密.保证安全性4.用户可以登录,退出和注销,并将用户的这些操作行为记录到日志中5.如果用户没有退出 下次登录自动显示用户名和最后一次登录的信息

代码语言:javascript
复制
Class manage{
  public  function  login(){
  }
  public  function  logout(){
  }
  public  function  zhuxiao(){
  }
  private  function  log(){
  }
  private  function  info(){
  }
  private  function  mail(){
  }
  private  function  safe(){
  }
  private  function  my_cookie(){
  }
}

五、实现一个JS工具库, 分别实现判断字符串参数是否为数字. 是否为日期格式为(YYYY-mm-dd). 是否为邮件地址 是否为URL 地址等常用方法.

代码语言:javascript
复制
<html>
<head>
    <title></title>
</head>
<body>
    <script type="text/javascript">
        function tool(){
            this.mail=function(mail){
                var pre="\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*";
                var res=mail.exec(pre);
                if(!res){
                    return false;
                }else{
                    return ture;
                }
            }
            this.date=function(date){
                var pre="/^((((19|20)\d{2})-(0?(1|[3-9])|1[012])-(0?[1-9]|[12]\d|30))|(((19|20)\d{2})-(0?[13578]|1[02])-31)|(((19|20)\d{2})-0?2-(0?[1-9]|1\d|2[0-8]))|((((19|20)([13579][26]|[2468][048]|0[48]))|(2000))-0?2-29))$/";
                var res=date.exec(pre);
                if(!res){
                    return false;
                }else{
                    return ture;
                }
            }
            .
            .
            .
        }
    </script>
</body>
</html>
本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2020-03-22,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 PHP学习网 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 二、写一个函数, 算出两个文件的相对路径, 如$a = ‘/a/b/c/d/e.php’; $b= ‘/a/b/12/34/c.php’; 计算出$b 相对于$a 的相对路径 应该是../../c/d 将()添加上
  • 三、假设某论坛 url http://test.com/login.php 为注册用户入口地址, 请用程序实现摸拟注册用户的过程, 成功之后到http://test.com/thread.php?id=100的版面发一篇帖子, 需要考虑有图形验证码的情况,验证码如:9679
  • 四、设计一个类, 实现用户管理,需求如下(写出文体结构即可)
  • 五、实现一个JS工具库, 分别实现判断字符串参数是否为数字. 是否为日期格式为(YYYY-mm-dd). 是否为邮件地址 是否为URL 地址等常用方法.
相关产品与服务
验证码
腾讯云新一代行为验证码(Captcha),基于十道安全栅栏, 为网页、App、小程序开发者打造立体、全面的人机验证。最大程度保护注册登录、活动秒杀、点赞发帖、数据保护等各大场景下业务安全的同时,提供更精细化的用户体验。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档