Php include with arguments

Include Local PHP File and Pass Parameters

I’d like to avoid doing any needless HTTP requests on my site to improve performance. So, I’d like to include a local PHP on my server without using cURL. The problem is that the PHP I’m calling expects some parameters to be passed via HTTP GET. I’d like to keep it that way since I also want to be able to access those PHP files from other places. What is the best way of doing this? Can I access the output of a local PHP file while giving it GET parameters?

@Sarfraz Even if he does, that command would not include the script it would include the output of the script. Also, you would need allow allow_url_include to be enabled as well in order for it work. Plus you should never include stuff via HTTP anyway. Ever.

3 Answers 3

I’d like to avoid doing any needless HTTP requests on my site to improve performance

include also makes an HTTP request when you supply an HTTP URI to it, so instead of the client’s asynchronous include (for example using AJAX or an iframe) you do it synchronously on the server, therefore making the page load even slower.

So while the include may work as intended (you want to include the output, right?), it will definitely not speed up your site.

the PHP I’m calling expects some parameters to be passed via HTTP GET. I’d like to keep it that way since I also want to be able to access those PHP files from other places.

Then alter those files and set the appropriate variables before including them. Or even better, refactor it into functions. For example, if the file you want to include looks like this:

$username = $_GET['username']; print "User: $username"; 

Then refactor into a function and store in a separate file:

function PrintUsername($username)

And call it appropriately:

include('printusername.php'); PrintUsername($_GET['username']); 

(You might want to throw in an isset() here and there.) Now you can alter your code that also needs this output:

include('printusername.php'); PrintUsername($someOtherVariable); 

Now you don’t have to rely on URL or $_GET or include magic, but simply use all functions as they’re meant to.

Источник

PHP, include with parameters

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

5 Answers 5

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.

this is kind of what i was going to suggest. create a function that takes arguments. when you are processing a form, use GET vars as the function call’s arguments, otherwise, set the arguments to whatever you want.

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.

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)

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.

Источник

PHP — include a php file and also send query parameters

I have to show a page from my php script based on certain conditions. I have an if condition and am doing an «include» if the condition is satisfied.

Now the problem is the server has a file «myFile.php» but I want to make a call to this file with an argument (id) and the value of «id» will change with each call. Can someone please tell me how to achieve this? Thanks.

What does myFile.php actually do in your case? Unless you request the include over HTTP, you can’t add parameters to the filename, but you could influence its behaviour via a global variable of some sort.

13 Answers 13

Imagine the include as what it is: A copy & paste of the contents of the included PHP file which will then be interpreted. There is no scope change at all, so you can still access $someVar in the included file directly (even though you might consider a class based structure where you pass $someVar as a parameter or refer to a few global variables).

Also note: $_GET will be the same for all included files. So if that is where your query parameters are stored, it will still see them. Also note: it is not the function, in a class.

You could do something like this to achieve the effect you are after:

$_GET['id']=$somevar; include('myFile.php'); 

However, it sounds like you are using this include like some kind of function call (you mention calling it repeatedly with different arguments).

In this case, why not turn it into a regular function, included once and called multiple times?

An include is just like a code insertion. You get in your included code the exact same variables you have in your base code. So you can do this in your main file :

The simplest way to do this is like this

You can share variables since you are including 2 files by using «include»

If you are going to write this include manually in the PHP file — the answer of Daff is perfect.

Anyway, if you need to do what was the initial question, here is a small simple function to achieve that:

 > // now it's time to include the real php file // all necessary variables are already defined and will be in the same scope of included file include($phpinclude); > 

I’m using this variable variable construction very often.

I know this has been a while, however, Iam wondering whether the best way to handle this would be to utilize the be session variable(s)

In your myFile.php you’d have

Would this circumvent the «suggested» need to Functionize the whole process?

You want to make it a function so you don’t initiate a bunch of requests. The problem isn’t how to get the information to the included PHP — OP’s method already achieves that.

In the file you include, wrap the html in a function.

In the file where you want it to be included, include the file and then call the function with the parameters you want.

Please add some explanation to your answer such that others can learn from it. How does wrapping markup in a function relate to the given problem?

I have ran into this when doing ajax forms where I include multiple field sets. Taking for example an employment application. I start out with one professional reference set and I have a button that says «Add More». This does an ajax call with a $count parameter to include the input set again (name, contact, phone.. etc) This works fine on first page call as I do something like:

User presses a button that makes an ajax call ajax(‘references.php?count=1’); Then inside the references.php file I have something like:

I also have other dynamic includes like this throughout the site that pass parameters. The problem happens when the user presses submit and there is a form error. So now to not duplicate code to include those extra field sets that where dynamically included, i created a function that will setup the include with the appropriate GET params.

The function checks for query params, and automatically adds them to the $_GET variable. This has worked pretty good for my use cases.

Here is an example on the form page when called:

Just another example of including GET params dynamically to accommodate certain use cases. Hope this helps. Please note this code isn’t in its complete state but this should be enough to get anyone started pretty good for their use case.

Источник

Читайте также:  Настройка apache php скрипты
Оцените статью