Reputation: 709
when I use this:
require("diggstyle_code.php?page=$page_no");
the warning is :failed to open stream: No error in C:\xampp\htdocs\4ajax\gallery_core.php on line 198
and the error is:
Failed opening required 'diggstyle_code.php?page=1' (include_path='.;C:\xampp\php\PEAR') in C:\xampp\htdocs\4ajax\gallery_core.php on line 198
value of the variable $page_no
is collected beforehand.
But if I omit the '?page=$page_no part'
from the argument of the require function, then no error or warning is shown.
I need to pass the variable when I use the require() function.
Upvotes: 55
Views: 84199
Reputation: 401022
require()
and include()
will open the file corresponding to the path/name they receive.
Which means that, with your code, you would have to have a file called diggstyle_code.php?page=1
on your disk. That's obviously not the case, so it fails.
Quoting the Variable scope page of the PHP Manual:
The scope of a variable is the context within which it is defined. For the most part all PHP variables only have a single scope. This single scope spans included and required files as well.
In your case, you don't need to pass the variable. If you have a variable in your current script, it will also exist in the script you include, outside of functions, which have their own scope.
In your main script, you should have:
$page_no = 10;
require 'diggstyle_code.php';
And in diggstyle_code.php
:
echo $page_no;
// Or work with $page_no the way you have to
Remember that including/requiring a file is exactly the same as copy-pasting its content at the line it's required.
Upvotes: 83
Reputation: 39456
Though this question is old there's another option that I use which is missing from this thread. You can return a function from the required file which accepts the arguments you want to pass along:
return function(array $something) {
print_r($something);
}
And call it with the arguments when you require
it:
require('file.php')(['some', 'data']);
// or:
$context = require('file.php');
$context(['some', 'data']);
Upvotes: 6
Reputation: 9335
require()
does not make an HTTP call. All it does is open the file from disk and include the code in the position of the call. So simple local variables are enough.
Upvotes: 1
Reputation: 801
if I've correctly understood, what you need is to call the file diggstyle_code.php
passing an argument, so that no one can call that file and make it work, rather than your main file. Am I right?
Thus supposing that your "main.php" has the lines
require("diggstyle_code.php?page=$page_no");
it means that:
if anyone calls "main.php" gets diggstyle_code.php
running.
But if anybody in any manner calls directly diggstyle_code.php
he/she shoudl get nothing.
If I am right on my understanding, a way to achieve this, is to include into the main file a variable or a constant, that will be scoped by diggstyle_code.php
Thus for instance: 'main.php'
<?php
define("_VERIFICATION_", "y");
require("diggstyle_code.php");
?>
and now diggstyle_code.php
<?php
if ( _VERIFICATION_ == "y" ) {
//Here the code should be executed
} else {
// Something else
}
?>
Upvotes: 2
Reputation: 1
I had this problem and I noticed if you use http:// in your url then it doesn't work
Upvotes: -4
Reputation: 43508
require
doesn't pull the file from the web server - it should refer to a file on the filesystem instead.
Calling include
or require
just tells PHP to paste the contents of the given file in your code at this place, nothing more than that.
Upvotes: 3
Reputation: 1788
If your variable is global, there's no need to "pass"it, it is there already: PHP variable scope.
The answer then is, don't do anything, if $page_no exists in the file in which you call require(), it will be available in the included file.
Upvotes: 1
Reputation: 26597
require, require_once, include and include_once try to include files from the filesystem in the current file.
Since there's no files named diggstyle_code.php?page=1, it's totally logical that PHP can't find it.
You can't pass values that way, however, any variable declared in the current file will be accessible in the included files.
Upvotes: -1
Reputation: 265241
this should work, but it's quite a dirty hack:
$_GET['page'] = $page_no;
require('diggstyle_code.php');
you probably want to refactor your code to use functions and/or objects and call them from your files instead of including them (spaghetti code alert)
Upvotes: 8