程序员找工作必备 PHP 基础面试题 - 第十四天

“PHP学习网” 公众号会每天分享一些面试题,正在找工作的小伙伴们可以来看看哦。

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

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 将()添加上

 $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模拟登陆操作

第一:分析登陆字段
第二:登陆后保留COOKIE
第三:读取COOKIE并跳转到相关页
第四:抓取数据
<?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. 如果用户没有退出 下次登录自动显示用户名和最后一次登录的信息
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 地址等常用方法.

<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>

最后各位可以扫下方二维码关注我公众号,目前我正在更新基础面试题,之后会更新中高级、redis、liunx面试题

本作品采用《CC 协议》,转载必须注明作者和本文链接
和PHP学习网一起努力学习
讨论数量: 0
(= ̄ω ̄=)··· 暂无内容!

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!