When I include a page using it's full URL (like include 'http://mysite.tld/mypage.php'), I can't use the $GLOBALS in mypage.php, it returns Undefined index error.
But when I include it using it's relative path (like include 'mypage.php'), then it's OK.
The reason why am I using URL instead of relative path is that I want to include $_GET parameters to mypage.php
Is there any logical explanation of this strange behaviour?
Note that both files are on the same server, in the same directory.
Including files with a URL means the code is run as a separate process, which means it runs under a different variable scope. This is as opposed to if you include the file via a relative path, in which case it is pretty much equivalent to cut and pasting the code into the script.
Essentially this means that the only variables available from your starting script are those that you explicitly pass (as you are in this case using the $_GET variables). This includes the $_SESSION variables, since the caller is your own server rather than the client.
This behaviour is noted in the PHP manual's include page:
If the target server interprets the target file as PHP code, variables
may be passed to the included file using a URL request string as used
with HTTP GET. This is not strictly speaking the same thing as
including the file and having it inherit the parent file's variable
scope; the script is actually being run on the remote server and the
result is then being included into the local script.
Related
I have a webpage which holds the number of the page to display:
mydomain.net/index.php?page=42
This works alright. Now I want to display the page only when a particular cookie is set, and I moved most of the body to an include file, so that index.php only has
<?php
if ($cookie_ok):
include("http://mydomain.net/index_d6skrif9.php");
else:
include("http://mydomain.net/noaccess.inc");
endif
?>
and now the $_GET["page"] in the include file, which is supposed to retrieve the page number returns nothing.
I read that $_GET[] is a superglobal and that superglobals' scopes are across include files. So what's wrong here, and how can I use the page number in the include file?
$_GET works in included files, it does not work in included HTTP resources.
The PHP in index_d6skrif9.php will be executed by mydomain.net before it gets to the PHP program with the include statement in it.
Use a local file path, not an HTTP URL.
include("index_d6skrif9.php");
Alternatively, pass the value to the server you are pulling the include from:
include("http://mydomain.net/index_d6skrif9.php?page=" + urlencode($_GET['page']));
Note that the latter approach has far more opportunity for things to go wrong and is much less efficient than a local file included, so it isn't recommended if you can help it.
You'll need to include them locally, not over-the-web:
if ($cookie_ok)
include("index_d6skrif9.php");
else
include("noaccess.inc");
endif
By using URLs, you're making a web-request and the server executes the PHP in the file and returns the contents (making it have its own set of super-globals).
I have this code inside of my header
<?php
define('RELPATH','http://www.saint57records.com/');
include_once(RELPATH.'sidebar.php');
?>
and an example line of code in the sidebar
<img style="margin:10px;" src="<?php print RELPATH;?>images/logo.png" width="60px"/>
but when it gets to the page it includes the file correctly but all the links inside of the file just print RELPATH instead of the web url like this
<img style="margin:10px;" src="RELPATHimages/logo.png" width="60px"/>
It works fine on the other pages of my website, just not inside of Wordpress. Does anyone know what might be causing this issue?
The short answer is to provide a filesystem path to RELPATH, not a web URL.
The long answer is that when you use a web URL to include a PHP file, the PHP file will be treated like an external source. It will be called remotely, executed in a process of its own, and return the results. A constant defined previously can not have an effect in this remote resource.
If http://www.saint57records.com/ is on a different server, you'll have to pass RELPATH to it some other way, e.g. through a GET variable (which you'd have to sanitize with htmlentities() prior to use.) However, including content from a remote server in this way isn't good practice. It'll slow down your page as it'll make an expensive web request. If the target server is down, your page will time out.
I'm using require_once to call a script that will echo back HTML for a the footer of each page. I have each page submitting a variable in the URL for the PHP file to retrieve via $_GET, but because it doesn't see a file with the name of the full URL (including variable) it isn't working. Any work arounds here?
ERROR:
Warning: require_once(footer/footer.php?page=homepage) [function.require-once]: failed to open stream: No such file or directory in /home/content/05/10838405/html/index.php on line 434
If I remove the '?page=homepage' it works perfectly.
Just use a variable
$page = 'homepage';
require_once 'footer/footer.php';
Then in our footer.php you can use $page
GET parameters are a part of HTTP URLs – but the local file system of your server does “speak” HTTP.
So footer/footer.php?page=homepage would literally mean a file of that name (which does not exist, and the characters ? and = are “illegal” as parts of a file/directory name in many file systems).
Since you are asking for a workaround – two easy possibilities (in both cases, only requiring the file by it’s actual name, footer/footer.php, of course):
you re-write the code inside that file to use a “normal” variable to decide what it should output – $page. (Although that might lead to problems with the variable scope, if $_GET['page'] is used inside a function in that file.)
In your script that does the including, you set the GET parameter yourself – $_GET['page'] = 'homepage'; before including the file. This is perfectly legal in PHP, from a syntactic standpoint – semantically, one might see that differently; and also it might lead to problems if in other code further down the line that GET parameter gets evaluated as well, and the value homepage is not expected in that context.
require_once is a local include, its not going via a http request so things like query strings won't work.
do something like:
$page = 'homepage';
require 'footer.php'
$page will be available to footer.php
Edit: Beaten like a ginger stepchild
Additional Reading: PHP Manual: Require Once
The reason you are having this problem is because require_once will look for a file called 'footer.php?page=homepage' which doesn't exist on your filesystem. Romainberger's solution is good.
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.
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