This question already has answers here:
Passing PHP variables to an included file?
(4 answers)
Closed 8 years ago.
In my script, I choose which page to include based on a variable.
Does the included page receive the variables that are defined in the main page? Or so I have to redefine them?
If so, what's the best way to pass the variables to the included page?
I tried include("page.php?var=".$var)
But it seems that actually tries to include a file with that string name.
Advice?
If you define you variable before include page, you don't need any query string. Your variable will be accessed in the included page with just name. for example
$name = "Awais"
include("page.php");
then in page.php
echo $name; //will print Awais
Variables that are already in scope in the first page are already defined in the second.
You are better off setting the variables themselves in the main page. include tries to include a local file, not a HTTP GET request, but just set the variables anyway and you can use them.
If you define $var = 1 and after that include("page.php"); the variable will be accessible in that file, since it's nothing more then an extension of what you already got.
This ... "include("page.php?var=".$var)" won't work
Instead try the following:
page1.php
<?php
$dog_name = "scruff";
include("otherpage.php");
?>
otherpage.php
<?php
echo $dog_name;
?>
This will output on page1.php:
scruff
As midnightlightning said: "Variables that are already in scope in the first page are already defined in the second."
Does the included page receive the variables that are defined in the main page?
Yes, the code you include is within the same scope. That is also the documented behaviour, see include.
$var = 'value';
include('page.php'); # has $var defined now.
unset($var);
include('page.php'); # has $var undefined now.
So as you can see, there is no need to redfine them.
But you might want to separate that because it has side-effects, see:
Is include()/require() with “side effects” a bad practice?
Related
So basically I have a file, lets say file1.php and it includes another file2.php.
file1.php contains a variable called $dateString that I have performed some operations on.
file2.php also has the same variable called $dateString however this variable is not being referenced within this include file even though $dateString was initialized prior to including file2.php.
So this is currently how file1.php looks like:
$dateString = 'some expression';
include 'file2.php';
file2.php is basically trying to echo out the variable that was initialized in file1.php
echo $dateString;
Everything else from file2.php is being called properly except that one variable.
Hopefully this makes sense. Is this possible in PHP? I have read in other places that you need to make the variables global and others have mentioned how you cant reference a variable from a parent file within a child file..any thoughts? Thanks in advance!
there is nothing magically happening when you use PHP's include(). the included file's code is basically just inserted at the point where you are including it - resulting in one bigger PHP file.
it doesn't really matter if the "parent" or in the "child" file - it's the position that matters since PHP will parse the single resulting file from top to bottom.
for example: if you set the variable in file A before including file B, file B will know about the variable. but if you set the variable in file A after including file B - file B doesn't know it.
With coding you should always try and test your own things before asking other people, butch like what #bub suggested in the comments.
Variables are global pretty much, you are able to include another file and use variables that are in file1.php in file2.php.
Otherwise, if you are using them in a function, I'd suggest using:
global $var;
the above should only be used in a function.
This question already has answers here:
PHP include() with GET attributes (include file.php?q=1)
(7 answers)
Closed 8 years ago.
What are the security implications of passing a get variable through an include?
Example:
index.php:
$lastname = $pulleddatabasevalue;
include "../includes/header?lastname=$lastname";
header.php:
echo $_GET["lastname"];
As the variable is dynamic, I have struggled to make include() or sessions work to assign the variable $lastname with the database value within the php include. However, $_GET here has worked fine. It doesn't show up on the browser address bar, thus can't be manipulated in a hostile manner there. Is there another way someone with malicious intent could work this code? Assume that the include directory is locked and I'm only referring to index.php.
Sorry, no way to pass get parameters to included file... See:
PHP include() with GET attributes (include file.php?q=1).
Include is a strict let's name it "Physical function". To make a get request, you must make a request. Include just read the file from the server.
BTW. I'm curious, how it is possible, you made it work. I think there is some misunderstood in your code.
You should think about include, as a COPY PASTE function.
In that case:
$var = true;
include ('include.php');
include.php:
var_dump($var);
should echo bool(true).
Hope it helps.
When talking about security issues, as far as I'm concerned, include in the way I describe, should not create any new security holes. But you should check all the permissions of included files, to be 100% sure.
The security implications of outputting user supplied input is the same no matter how it is done: ESCAPING AND VALIDATION IS ESSENTIAL! Otherwise you are implementing big security holes.
Apart from that, there isn't any difference whether you directly access $_GET, or first stuff that value into another variable and access that inside your include.
The only difference is of general software maintenance: The former usually is considered bad because it is access to a global variable, while the latter might be part of a function call and might encapsulate the variable name better.
Your code, however, is wrong. You cannot pass query parameters as part of the filename. It works because $_GET is available as an array everywhere without any further code (read "superglobal variable" in the PHP documentation).
Keep it simple and don't confuse...
index.php
$lastname = $pulleddatabasevalue;
include "../includes/header.php";
header.php
echo $lastname;
External refs. and recommended read:
http://www.php.net/manual/en/function.include.php
http://www.php.net/manual/en/reserved.variables.get.php
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
passing variable between pages
I need to create a variable that I can use in one PHP page to assign a category to this variable. When I've assigned a value, how I can pass that value to another PHP page?
Do I have to use a global variable?
You can pass variables from page to page using:
Sessions.
A common config file that is included throughout all of your pages.
Forms (hidden fields etc).
Cookies.
Each approach has its own use, so you'll need to choose according to the circumstance. If the variable in question will stay the same throughout your app, I'd suggest placing it in a config file. If its a user-specific variable, I'd suggest using sessions.
Its depend on you what you want to use for passing value from one php page to another.
You can use Session, Cookie or Forms by using $_GET and $_POST method:
Example : By using Session
//page1.php
session_start();
$_SESSION['name'] = $name;
//you can access this in page2.php
session_start();
echo $_SESSION['name'];
Example : By using Cookies
//page1.php
setcookie(name, value, expire, path, domain);
//you can access it in page2.php like this
echo $_COOKIE["name"];
Example : By using GET and POST
You can use GET with anchor tag and for POST you need form submission.
Read more about GET and POST :
http://php.net/manual/en/reserved.variables.post.php
http://php.net/manual/en/reserved.variables.get.php
Hope this information will help you
If you use POST to send FORM data to a PHP page, are the POST values available to use in all the PHP pages that are INCLUDED in the POST TO with the PHP INCLUDE?
Example in the POSTED TO PAGE:
<?php include 'otherpage.php'; ?>
Would I be able to use the POST value that was sent to the 'POSTED TO Page' in 'otherpage.php'?
Yes
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.
PHP.net
When a file is included, the code it contains inherits the variable scope of the line on which the include occurs. So if you can access POST at the same line of your include then yes.
Yes, actually you should be able to access these variables.
Hi I've an issue while dealing with a php code inside Wordpress;
I've my aaa.php file wich contains code:
<?php
require_once("lang_file.php");
echo $GLOBALS['general']['username'];
?>
My lang_file.php contains:
<?php
$language['general']['username'] = 'User';
?>
And my Wordpress page contains this:
<?php
include("aaa.php");
?>
If i access directly aaa.php through browser i get the "User" message from the echo on aaa.php.
If i access the Wordpress page with include code it doesn't show nothing. I've already read this answer: Does WordPress clear $GLOBALS?
And i tried to define the variables on lang_file.php as $GLOBALS but this still don't work.
You'd need to use
$GLOBALS['language']['general']['username']
instead.
In PHP, $GLOBALS is an array of all variables defined globally. The first element of the array is the global variable name.
Therefore, to access the global variable $language via $GLOBALS, you would need to use $GLOBALS['language']. You can then append any array structure after that which you want to reference from $language.
You can also access it directly via the name $language if you prefer, by adding global $language; to the code prior to where you want to use it.