简体   繁体   中英

php include or require contents of a variable, not a file

I'm looking for a way to include or require the content of a variable, instead of a file.

Normally, one can require/include a php function file with either of these:

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

Suppose I wanted to do something like this:

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

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

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

Now, in the above example, I have the actual function files which I am loading into variables. In the real world scenario I'm actually dealing with, the php code in the function files are not stored in files. They're in variables. So I'm looking for a way to treat those variables as include/require treats the function files.

I'm new to php so please forgive these questions if you find them foolish. What I'm attempting to do here does not appear to be possible. What I ended up doing was using eval which I'm told is very dangerous and should be avoided:

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

Content of $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;  
  }

}

?>


Are there any other alternatives?

No there are no other alternatives.

In terms of security there is no difference if you include() a file or eval() the content. It depends on the context. As long as you only run your own code there is nothing "dangerous".

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM