I am trying to add some variables at the end of an "include". Is this possible?
Here is the code that DOES NOT WORK:
include('login.php?' . "message=used");
You don't need to include a querystring. Includes/requires essentially put the included code in place. Just declare whatever variables the includes needs before you include the file:
$message = 'used';
include('login.php);
No it's not, and you really have no reason to do so, you see. Every variable you define before the include will be available from the include.
$var = "Hello include";
include("login.php");
//Include.php
echo $var;
Will output "Hello include"
No, the include() takes the parameter as a filename, so when you use login.php?message=used it's looking for login.php?message=used.php, which obviously doesn't exist.
You could alter the .ini file setting allow_url_include but this poses a potential security issue, but otherwise just declare your variables before including the file.
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.
I have file a.php with:
define('COLOR','blue');
and file b.php with:
include('/a.php');
echo COLOR;
... and "COLOR" is echoed. Is there something I need to do to carry the definition over?
File B is a WordPress functions file, which is possibly the cause - but I want to check first that the PHP is correct, having never used define() before... Thanks.
This should work just as you expect, however I think you have an issue on your include:
include('/a.php');
The leading slash anchors it to the root directory, which is almost certainly not what you want. I recommend using require instead so that you get an error if it fails:
require('a.php');
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.
I'm still kinda new to HTML/PHP and wanted to figure out how to streamline my pages a little bit more. I want to try to pass a variable from the page I include another PHP file on.
For example:
<?php include "quotes.php"; $name='tory'?>
I then want to use this variable name, $name='tory', in my quotes.php file. I'm unsure if I'm going about this the correct way because if I try to use the variable $name in my quotes.php file, it says that "name" is undefined.
Question has been answered. Needed to switch the statements. Thank you all!
Assign $name variable before including other files:
<?php
$name='tory';
include "quotes.php";
?>
Reverse it:
<?php $name='tory'; include "quotes.php"; ?>
You cannot use a variable before it was declared.
In your example, you're including quotes.php before the $name variable declaration, it will never work.
You can do
<?php $name='tory'; include "quotes.php"; ?>
Now, $name exists in "quotes.php"
What you're doing isn't necessarily the best way to go about it, but to solve your specific problem, simply define the variable before including the file. Then the variable will be globally scoped and available in the include file.
<?php
$name = 'tory';
include "quotes.php";
?>
You need to define $name first before including quotes.php.
You have to declare the variable $name before including the file.
<?php
$name = 'tory';
include 'quotes.php';
?>
This makes sense because the file you included will get parsed and executed and then move on with the rest.
The statements are executed in sequence. It is as though you had copy-and-pasted the contents of quotes.php at the point in your script where the include statement is found. So at the point where you execute quotes.php, the statement defining $name has not happened yet. Therefore to get the behaviour you want, you should reverse the order of the two statements.
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