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).
Related
how to pass a variable in php include, i am new in php
This is what I was trying to do
include_once 'task_include.php?id=pot';
get an error like this
Warning: include_once(task_include.php?id=pot): failed to open stream: No such file or directory
There is no way to pass variable in the include_once.
There is no way to pass variable in the include_once.
Why don't u perform get and post methods.
use form action.
include loads a local php file an "pastes" the content into the actual scope....
it isn't that nice, but for your case you could do this
$id="pot";
include_once "task_include.php";
inside task_include.php there will be the global variable $id (not $_POST['id'] or something).
it isn't that nice, but i think, it would fit your needs
I know similar questions have been asked, but unfortunately I didn't manage to solve the problem after going through them.
Assuming this situation: In one.php I'm retrieving some data from an input field and saving it as a variable and later on I require two.php
one.php
$rejon = $_POST['rejon'];
require 'two.php';
two.php
--here I would like to use $rejon ---
When I try to use $rejon in two.php, it doesn't work (I try to insert it into a database to be exact). On the other hand, if I don't require the two.php but instead paste the code into one.php, it works.
I don't understand why this happens. W3schools claims that "The include (or require) statement takes all the text/code/markup that exists in the specified file and copies it into the file that uses the include statement." - http://www.w3schools.com/php/php_includes.asp
but it doesn't seem to work like a copy as manualy copying gives other results (variable $rejon is accesible).
1) What exactly does require do and what are its limitations?
2) Most importantly - how do I retrieve that $rejon variable in two.php?
Works for me:
$ cat one.php
<?php
$rejon = 'this value came from POST';
require 'two.php';
$ cat two.php
<?php
echo $rejon . PHP_EOL;
$ php one.php
this value came from POST
The W3Schools description is, unsurprisingly, misleading. Nothing is copied. The contents of the file are read, evaluated, and inserted at the point of the require. With respect to variables, the PHP manual says this:
When a file is included, the code it contains inherits the variable
scope of the line on which the include occurs. Any variables available
at that line in the calling file will be available within the called
file, from that point forward. However, all functions and classes
defined in the included file have the global scope.
Which is why my example above works as it does.
To diagnose this issue, continue to simplify your code to reduce it to the simplest possible example. If you're trying to use $rejon in a function inside of two, then you need to make the variable global. Like:
global $rejon;
$rejon = $_POST['rejon'];
// now you have $GLOBALS['rejon'] everywhere
Side note, $_SESSION should not be necessary unless you're crossing a page refresh boundary.
You can do this by the following two ways
1.Using Session
You can store the data in the session and than you can use it in the another file two.php
one.php
<?php
session_start();
$rejon = $_POST['rejon'];
$_SESSION['rejon'] = $rejon;
?>
two.php
<?php
session_start();
echo $_SESSION['rejon'];
?>
2. Including that file
I have found that you are including the file two.php in your script. If you are including this file than you can directly use the variable $rejon
two.php
<?php
echo $rejon;
?>
You can use sessions http://php.net/manual/en/book.session.php or a constant http://php.net/manual/en/language.constants.php.
Utkarsh already provided some examples for sessions.
To define a constant you use the define() function.
define('CONSTANT_NAME', CONSTANT_VALUE);
As long as your constant is defined and included (if in another file) before you use it you can then call CONSTANT_NAME in your code to get the value.
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.
I am in a situation .
My php folder structure is like
UI
user
login.php
logout.php
jquery
somejs
css
somecss
blah.php
blah.php
Now to import any css ,js or any php file i am using the file path like
localhost/UI/user/index.php // example
Now i am trying to define a global variable on any page like
<?php
$somevar = "localhost";
GLOBAL $somevar;
?>
So that i could import any css js like
<?php echo $somevar ;?>/UI/user/index.php // example
Problem : It is working on that page only where i declared the variable as GLOBAL
I want to use the variable on each page and don't want to use include
Is there any other alternative to define a variable for files folder in php ?
You do not declare variable with global. You just make it available within you method or function body even it was set (or using your terminology "declared") outside of it. So there's no way to have the variable unless it is declared. And there's no way to do that without running the PHP code (simplification, but it does not matter here). And code is not coming from nowhere, hence the need of include or require of said code that sets variable.
You may try to use php.ini's auto_include_file to have your variables auto-included, but still, the PHP code needs to be used for that.
But you generally doing it wrong. move all global variables into class, set autoloader and access i.e. statically. The code will be much cleaner.
Well, my experience says .. global is used for accesing variables inside the function those which defined outside the function.
There can be many different solutions to the same, one i will suggest is use sessions or cookies. Store the data in session / cookies and access it across wherever required.
You have to use define() function for this purpose
config.php
<?php
define("host", "localhost");
?>
now include this page wherever you want to access HOST variable.
you can access this variable like this echo host;
You can use the special PHP-defined $GLOBALS array. The $GLOBALS array is an associative array with the name of the global variable being the key and the contents of that variable being the value of the array element. Notice how $GLOBALS exists in any scope, this is because $GLOBALS is a superglobal
You may not need to define a global variable to apply the css, js or any static files viz Images etc to your application. you may use relative path to include js and css files. You can refer to this article to know more about relative and absolute path.
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