【问题标题】:php include or require contents of a variable, not a filephp包含或需要变量的内容,而不是文件
【发布时间】:2019-12-30 02:16:16
【问题描述】:

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

通常,可以要求/包含具有以下任一功能的 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 代码不存储在文件中。它们在变量中。所以我正在寻找一种方法来处理这些变量,因为 include/require 处理函数文件。

我是 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;  
  }

}

?>


还有其他选择吗?

【问题讨论】:

  • 您能否进一步描述您的用例。为什么要执行未存储在文件中的 php? php哪里来的?
  • 请发布/tmp/my1stphpfunctionfile.php的内容
  • 嗯,eval()include() 几乎相同。并且“……有人告诉我这是非常危险的,应该避免”在没有理由和背景的情况下有点无用的建议。
  • @AndreaManzi 我已按要求将代码添加到我的帖子中。
  • @user2831586 我们需要知道 PHP 的来源。如果它来自外部源(如暴露的 HTTP POST 端点),那么你永远不应该使用 eval。为什么你把它放在一个变量中而不是通过include

标签: php require


【解决方案1】:

没有,没有其他选择。

在安全性方面,include() 文件或eval() 内容没有区别。这取决于上下文。只要您只运行自己的代码,就没有什么“危险”。

【讨论】:

    猜你喜欢
    • 2011-12-15
    • 2011-11-25
    • 2012-07-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-03
    • 1970-01-01
    相关资源
    最近更新 更多