I have a PHP-file which will be called from a form. So it gets parameters over $_GET.
Now I need the exact same functionality of this file, but not in such a form-call. I will include it in normal code with fixed parameters (which normally come from the form). So my file can work with the form AND without it.
I know only the way with include and setting the $_GET in front of it. But I am not sure this is the most elegant way (I dont like the idea of setting things like $_GET).
any other ways of doing this?
br, chris
You can convert the code to a function that takes an associative array as its parameter.
That way, you can just include it when necessary and call the function with either $_GET
or an array you build yourself.
You could first do an if
statement to see if the $_GET parameters are already set. If they are already set, assign them to variables that the script can use, and if they are not already set, just assign your fixed parameters to the variables.
If I understand you correctly, you could create an array full of default values. When this script is called it will check to see if $_GET
is empty. If $_GET
is empty, the default values are used, else, the default array is overwritten with $_GET
and those values are used.
$def = array(
'keyA' => 'valueA',
'keyB' => 'valueB'
);
if ( !empty( $_GET ) ) $def = $_GET;
// $def contains either your defaults, or the user-provided values
It's important to note that this logic can be broken up into two files.
/* index.php */
$def = array(
'keyA' => 'valueA',
'keyB' => 'valueB'
);
if ( !empty( $_GET ) ) $def = $_GET;
include( 'doStuff.php' );
/* doStuff.php */
if ( !isset( $def ) ) $def = $_GET;
echo $def['keyA'];
Note that in doStuff.php
we check to see if $def
is set. If this script is being included into index.php
, then we know $def
is set and those default values are present. If doStuff.php
is being called directly, then we know that $def
is most likely not set, and that we need to set it based upon $_GET
. Either way, when the script is ready, we will have a variable called $def
where we will get all of our values.
since you should be doing some kind of parameter sanitizing in your file you could parse parameters like this
$someConfigVar = isset($config['someConfigVar']) ?
$config['someConfigVar'] :
sanitize($_GET['someConfigVar'];
in this case you just need to fill the $config
array before including your file. this code snippet expects sanitize()
to be your sanitizing function (defense against xss and SQL injects etc)
Put your code that operate on the data of the form inside a separated file.
This file will use the context array variable $params, and not $_GET.
Then wherever you need to use the operations of this file, include it with the following function.
Then do includeElement('your_file', $_GET), if you want to use web parameters.
/**
* Allow a file inclusion with a set of parameters
* @param string $src File
* @param array $params $parameters
*/
function includeElement($src, $params = array())
{
include($src);
}