程式設計師找工作必備 PHP 基礎面試題 - 第十四天

viphper發表於2020-03-26

“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學習網一起努力學習

相關文章