php包含或要求变量的内容,而不是文件的内容

时间:2019-08-25 18:15:30

标签: php require

我正在寻找一种包含或要求包含变量而不是文件内容的方法。

通常,一个人可以要求/包括一个具有以下任一功能的php函数文件:

require_once('my1stphpfunctionfile.php')
include('my2ndphpfunctionfile.php');

假设我想做这样的事情:

$contentOf1stFFile = file_get_contents('/tmp/my1stphpfunctionfile.php');

$contentOf2ndFFile = file_get_contents('/tmp/my2ndphpfunctionfile.php');

require_once($contentOf1stFFile);
require_once($contentOf2ndFFile);

现在,在上面的示例中,我具有要加载到变量中的实际功能文件。在我实际处理的现实世界中,函数文件中的php代码未存储在文件中。它们在变量中。因此,我正在寻找一种将那些变量视为包含/要求对待函数文件的方法。

我是php的新手,所以如果您发现它们很愚蠢,请原谅这些问题。我在这里尝试做的事情似乎是不可能的。我最终所做的是使用eval,这是非常危险的,应避免使用:

eval("?>$contentOf1stFFile");
eval("?>$contentOf2ndFFile");

$ contentOf1stFFile的内容:

# class_lookup.php
<?php

class Lookup_whois {
  // Domain name which we want to lookup
  var $domain;
  // TLD for above domain, eg. 'com', 'net', etc...
  var $tld;
  // Array which contains information needed to parse the whois server response
  var $tld_params;
  // Sets to error code if something fails
  var $error_code;
  // Sets user-friendly error message if something goes wrong
  var $error_message;
  // For internal use mainly - raw response from the whois server
  var $whois_raw_output;

  function Lookup_whois($domain, $tld, $tld_params) {
    $this->domain     = $domain;
    $this->tld        = $tld;  
    $this->tld_params = $tld_params;
  }

  function check_domain_spelling() {
    if (preg_match("/^([A-Za-z0-9]+(\-?[A-za-z0-9]*)){2,63}$/", $this->domain)) {
                        return true;
                } else {
                        return false;
                }
  }

  function get_whois_output() {
    if (isset($this->tld_params[$this->tld]['parameter'])) {
      $query = $this->tld_params[$this->tld]['parameter'].$this->domain.'.'.$this->tld; 
    } else {
      $query = $this->domain.'.'.$this->tld; 
    }
    $server = $this->tld_params[$this->tld]['whois'];
    if (!$this->check_domain_spelling()) {
      $this->error_message = 'Domain name is not correct, check spelling. Only numbers, letters and hyphens are allowed';
      return false;
    }
    if (!$server) {
      $this->error_message = 'Whois server name is empty, please check the config file';
      return false;
    }
    $output = array(); 
    $fp = fsockopen($server, 43, $errno, $errstr, 30); 
    if(!$fp) {
      $this->error_code    = $errno;
      $this->error_message = $errstr;
      fclose($fp);
      return false; 
    } else { 
      sleep(2);
      fputs($fp, $query . "\n"); 
      while(!feof($fp)) { 
        $output[] = fgets($fp, 128); 
      }
      fclose($fp);
      $this->whois_raw_output = $output;
      return true; 
    } 
  }

  function parse_whois_data() {
    if (!is_array($this->whois_raw_output) && Count($this->whois_raw_output) < 1) {
      $this->error_message = 'No output to parse... Get data first';
      return false;
    }
    $wait_for = 0;
    $result = array();
    $result['domain'] = $this->domain.'.'.$this->tld;
    foreach ($this->whois_raw_output as $line) {
      #if (ereg($this->tld_params[$this->tld]['wait_for'], $line)) {
      if (preg_match($this->tld_params[$this->tld]['wait_for'],$line)) {
        $wait_for = 1;
      }
      if ($wait_for == 1) {
        foreach ($this->tld_params[$this->tld]['info'] as $key => $value) {
          $regs = '';
          if (ereg($value.'(.*)', $line, $regs)) {
            if (key_exists($key, $result)) {
              if (!is_array($result[$key])) {
                $result[$key] = array($result[$key]);
              }
              $result[$key][] = trim($regs[1]);
            } else {
              $result[$key] = trim($regs[1]);
              $i = 1;
            }
          }
        }
      }
    }
    return $result;  
  }

}

?>


还有其他选择吗?

1 个答案:

答案 0 :(得分:0)

没有其他选择。

就安全性而言,include()是文件还是eval()是没有区别的。这取决于上下文。只要您只运行自己的代码,就不会有“危险”的事情。

相关问题