PHP Require and Include GET

半世苍凉 提交于 2019-11-30 05:40:52

问题


I would like to require a file but also pass GET variables through the url, but when I write:

<?php
   require_once("myfile.php?name=savagewood");
?>

I get a fatal error. How would I accomplish this functionality in a different way, such that I don't get a fatal error?


回答1:


variables will be available as normal you do not have to pass like this.

$name='savagewood';
require_once("myfile.php");

$name will be available in myfile.php




回答2:


<?php
$getVarsArray = $_GET;
$postVarsArray = $_POST;
/* n number of variables and lines of code*/
include('script-a.php');
?>

Now in script-a.php has access to $getVarsArray and $postVarsArray and if in any case you are in doubt you can use $GLOBALS to access any variable throughout the life cycle of a script. But using global variables is a sin. :)




回答3:


I think I have got a perfect solution to your problem. You can use implode function of PHP. But I would strongly recommend doing Shakti Singh's code.

SOLUTION CODE

echo implode(file('http://path-to-your-site/your-dir/myfile.php?name=savagewood'));


来源:https://stackoverflow.com/questions/5771937/php-require-and-include-get

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!