php - php包含或需要变量的内容,而不是文件
问题描述
我正在寻找一种方法来包含或需要变量的内容,而不是文件。
通常,可以要求/包含具有以下任一功能的 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;
}
}
?>
还有其他选择吗?
解决方案
不,没有其他选择。
include()
就安全性而言,您是文件还是内容没有区别eval()
。这取决于上下文。只要您只运行自己的代码,就没有什么“危险”的。
推荐阅读
- javascript - Javascript 调试 - 找出正在运行的脚本
- android - 在传递图像 URL 时无法在 android 的 onActivityResult() 中获取数据
- python - 幂法不适用于对称矩阵
- javascript - 角按钮单击
- python - 从 Python 中执行的批处理状态(使用子进程)
- javascript - Javascript - Chrome:子节点最大长度
- c++ - 是什么阻止了 Boost.Format 表单使用我的流运算符重载作为可选 int?
- outlook - 如何在新的 Outlook 邮件中创建永久通知?
- python - Python:将二进制数组显示为图像
- c# - 如何通过双击.NET框架中的exe等文件来运行.NET核心应用程序