PHP - sending GET variables as a "Path in and inclusion / requirement" statement

I have a situation where I would like to use the php file "query.php" to see the value of " $_POST " or $_GET as a MySQL query. It looks like this:

 <?php //verify data has been sent via POST or GET and set strings //find supplied table name if(isset($_POST['tblName'])){ $strSuppliedTableName = $_POST['tblName']; } if(isset($_GET['tblName'])){ $strSuppliedTableName = $_GET['tblName']; } else{ $strSuppliedTableName = 'roles'; } //find supplied field name or default to all fields in the table if(isset($_POST['fieldName'])){ $strSuppliedFieldName = $_POST['fieldName']; } else if(isset($_GET['fieldName'])){ $strSuppliedFieldName = $_GET['fieldName']; } else{ $strSuppliedFieldName = '*'; } //query db $query = 'SELECT ' . $strSuppliedFieldName . ' FROM ' . $strSuppliedTableName; $results = mysql_query($query) or die(mysql_error()); ?> 

After that, I want to include this query.php file in another file that will manage the results. I am trying to make this as modular as possible.

 <?php require_once("query.php?tblName=classes"); ......... (while loop, yadi yadi 

However, I get the error message:

Warning: require_once (query.php? TblName = classes) [function.require-once]: could not open the stream: there is no such file or directory

Is it permissible to pass GET values ​​to your included file? PHP will not handle this?

+4
source share
4 answers

You do not need to pass the variables to get or POST, as when you include or request files, the variables are distributed between the files if the values ​​are set before the inclusion occurs.

ie:

file1.php is called file1.php? var2 = value2

 <?php $var1 = "value1"; $var2 = $_GET['value']; include "file2.php"; ?> 

file2.php:

 <?php echo $var1.' '.$var2; ?> 

will output:

value1 value2

+6
source

As a shortcut, you can use $_REQUEST inside, which is the amalgam of the superglobals _GET, _POST, _COOKIE and _ENVIRONMENT. Exactyl, which is included in it, is managed by the installation of request_order .ini.

Alternatively, a completely reliable way to check which METHOD you are working with is $_SERVER['REQUEST_METHOD'] . This value is always set when processing an HTTP request and will be GET, POST, HEAD, etc. Unlike checking for the presence of a form field, it is completely reliable - the form field may not be submitted (is the box unchecked?). It may be renamed to HTML, but you will forget to change the script, etc.

As for your require() , unless you specify an absolute url ( http://... ), PHP will interpret its argument as a request for a local file and will not pass it through the HTTP layer. If you do not have a file named query.php?tblName... , it will be "file not found" and the require () request will not work.

+1
source

the right way to do this is to define your data as variables in your mother file, and then use these variables in your child file.
in the code that you gave to the parser, it searches for the file "query.php? tblName = classes" and, obviously, it does not exist.

+1
source

include / require both values ​​to be accepted as a specification, not a URI. PHP does not parse it as a URI, so what you are trying will not work.

It is better to set up an object that can check the installed / required file.

0
source

All Articles