Using a query-string in require_once in PHP

拟墨画扇 提交于 2019-12-23 07:47:27

问题


On one of my pages I have a require_once('../path/to/url/page.php'); which works with no problems. The moment I add a query string require_once('../path/to/url/page.php?var=test'); it won't include the file anymore. It's just blank. Anyone have any ideas of why? Can you not use a query-string in a require?

Thanks, Ryan


回答1:


By using require_once('../path/to/url/page.php?var=test');, php will not make a new request to page.php, it will actually search for the file named page.php?var=test and include it, because in unix, you are allowed to have such a filename. If you want to pass a variable to that script, just define it: $var="test" and it will be available for use in that script.




回答2:


require loads a File (from a file path) to include. It does not request that file through apache (or other webserver), therefore you cannot pass query strings in this way.

If you need to pass data into the file, you can simply define a standard php variable.

Example

<?php $a_variable = "data"; require_once('../path/to/url/page.php'); ?>

Note, the variable must be set before the include/require is called, otherwise it won't be available.




回答3:


All answes true. But most importantly: since $_GET is a global, it's present' in all included files as well, so there's absolutely no use in passing those parameters with the include.




回答4:


require only accepts paths it would be pointless to add any request since it doesn't make any it simple includes the required code into the current one



来源:https://stackoverflow.com/questions/4485845/using-a-query-string-in-require-once-in-php

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