Using a query-string in require_once in PHP - php

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

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.

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.

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.

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

Related

Passing arguments via url parameters inside include function in php

Suppose, I have an index.php file like this:
<?php
$id=2;
include('php/config.php');
require_once "php/variables.php";
?>
Is there any way to pass the variable $id via url parameter to config.php and variables.php files so that I can perform some tasks based on the variable after getting it
by $_GET['id']?
You can use $id in both file, because you are already including those two file in your current file so you can able to access $id, try to print echo $id; in both files and you can see the value.
Yes, You can directly access $id in both files as saied by Karthik.
Or you can use global variables
include('php/config.php');
require_once "php/variables.php";
The include and require_once functions, take a filesystem path by default; not a URL. Consequently "URL parameters" (and the corresponding $_GET superglobal) are irrelevant here, because you are not passing a URL.
(Yes, you can pass a URL to these functions if you have the appropriate fopen wrappers set and this will trigger an HTTP request - but this is most probably not what you want to do here.)
The include and require_once functions include the referenced document in-place and therefore inherit the current scope. Consequently the $id variable will be available as-is to these included scripts (as the other answers have already pointed out).

include not working for some reason

In my php code there is a section which needs to be retrieved from another php file.
I tried accomplishing this by:
include "teams.php?id=".$matchid;
and in the teams.php
$matchid = $_GET['id'];
echo "MATCH ID IS ".$matchid;
The problem is when i open teams.php?id=".$matchid directly it displays the match id fine
however the include doesn't work - i checked the source code of the original page - no code is being inserted. Is there a way to do what i want? I need to get php code from another file whilst passing 2 variables onto that file
Problem
The problem is related to what $_GET really contains:
An associative array of variables passed to the current script via the URL parameters. (...)
And you do not pass $matchid to the script through URL... And you should not in this case.
Solution
But there is a way. PHP does not separate global variables among files, so variables available in one file are available also in the one included:
in file no. 1:
// Assume $matchid is defined
include "teams.php";
in file no. 2 (the one included):
// No need to redefine $matchid - it was defined in the first file
echo "MATCH ID IS ".$matchid;
Also make sure you add <?php at the beginning of the file (the closing tag is not required). Otherwise it will be treated as text and not executed.
The problem is that you aren't loading that page in the browser when you call the include function, so the variable you are appending is not being processed as a URL query param. You are really just taking everything inside teams.php and dumping it into the other file (the one including teams.php) at that position.
If you want to effectively pass something into teams.php then just declare a variable before the include.
Check out http://php.net/manual/en/function.include.php and all will become clear.
That's not how include's work. When you include a file, it's code is executed as if it was written in the same file it's being included in. So teams.php will have the variable $matchid when the including file has that variable, no need to pass it in. Also the way you called the include was incorrect. Doing what you did causes php to look for a file in the current directory called 'teams.php?id=0' (replace 0 with the $matchid value). This file does not exist, 'teams.php' exists, not the URI. It is possible to specify a url by prefixing it with 'http://' however the result would work with the code in teams.php as it would expect to be able to read the php source.
You can do like this include "include "teams.php?id={$matchid}"; and
$matchid = filter_input(INPUT_GET, 'id', FILTER_SANITIZE_STRING);
echo "MATCH ID IS {$matchid}";
If You will use this code, you will be safe from Injects too!
I hope you will apreciate this.

php: include with URL parameter

I'm trying to include db.php and pass a variable to it.
This is what I have:
include "https://imprss.herokuapp.com/db.php?id=6626068";
Looks perfect to me but it simply won't work.
To use a protocol such as https in your include directive, you should make sure that allow_url_fopen is enabled in your php.ini file.
https://imprss.herokuapp.com/db.php?id=6626068 is not returning PHP code, it outputs HTML code to be interpreted by the browser, if you are interested in getting the HTML code of that page then dont use include() instead you should use cURL functions or the fopen url handlers .
I would use a relative path, but only thing you are missing are braces around the quotes which define url.
It is not possible to include a php file on another server, because this would be a security vulnerability. You can, however, use file_get_contents to get the output of a php file.
It's asked before and answered. just go to link bottom:
How to pass parameters with include?

Include GET Variable in include(path)

I am trying to do an include(dirname(FILE), '/../file.php?variable=VAR'), but it is unable to find the location when I use the variable.
The directory is located one above the current one where the file is. Is it possible to pass the variables through the include?
You can't, though you won't need this at all, because $_GET's scope is global [i.e. accessible from every script you run].
Just to be clear:
include($path) searches the filesystem for a resource, then processes it.
http://somesite.com/index.php?key=value is a URL, whose part after the ? sign is called query.
No.
By using include, you include the source code from the file as it is. I guess you want to define a function inside the included file and call it to get the result.

Storing the HTML output from a local PHP file into a string using file_get_contents

There is a header.php file and it contains some php codes that return HTML.
I know I can use require, include to echo the results, but what I want to do is to store its processed output string into a variable.
In a page, I used:
$headerHTML=file_get_contents('header.php');
Then I got the PHP code output rather than the processed HTML output.
I know adding http:// would help.
But I prefer to keep using relative path, how can I tell the function to treat the php file correctly?
Note: I would like to continue to use this statement file_get_contents rather than using ob_start() if possible.
I'd rather use require() wrapped inside ob_start() and ob_get_clean(). I am sure there is nothing wrong with this approach.
Don't use eval() - it's evil!
Use the relative local path an automatically map it to a absolute URL.
If URL wrappers are enabled and you want the output of header.php (and you don't want to keep session state) you could use $headerHTML=file_get_contents('http://yourdomain.tld/path/to/header.php');, though why you would want to do such a thing eludes me. Are you sure you're not trying to do something that could easily be solved by using templates and caching?
You can check http://in2.php.net/manual/en/function.eval.php#56641, hope it helps.

Categories