有时基于某些特殊需求,需要在PHP中使用include()方法调用项目中某个独立提供服务的文件,如果被调用文件使用GET方式传入数据,则需要用到一些小技巧。
举例:假设在 index.php 中需要调用 inc.php?para=3 ,
inc.php
<?php
echo $_GET['para'];
?>
下面的写法是无法得到正确结果的:
index.php
<?php
include dirname(__FILE__).'/inc.php?para=3';
?>
稍微变通一下,把$_GET变量在include之前定义,则可以正常运行:
index.php
<?php
$_GET['para'] = 3
include dirname(__FILE__).'/inc.php;
?>
如果php.ini中开启了allow_url_include功能,则可以使用include url的方式:
index.php
<?php
include 'http://www.yoururl.com/inc.php?para=3';
?>
设置方法:php.ini中,找到如下行,改为On:
; Whether to allow include/require to open URLs (like http:// or ftp://) as files.
allow_url_include = On
不过为了保证安全性,大部分的服务器都将allow_url_include 功能关闭,那样就只能视情况而定了。
