I have two pages of php on the same folder , Im wondering what can I do to use that variable in another page like page2.php
page1.php
$variable = "Variable";
page2.php
//get $variable value from page1.php here
Any suggestions? Thank You
Use Sessions.
On page1.php:
session_start();
$_SESSION['var1'] = 'some text';
On Page2.php:
if(!isset($_SESSION)){
session_start();
}
echo $_SESSION['var1'];
You will get some test as output.
Use session variables.
Just write session_start() at the beginning of files to start a session and assign the session variables.
eg:
File-1
session_start();
$_SESSION['var'] = "var";
File-2
session_start();
if(isset($_SESSION['var']))
echo $_SESSION['var']; // will print "var"
You can use this throughout till the session destroyed. To destroy the session (may be on logout)-
if(isset($_SESSION['var']))
unset($_SESSION['var']);
To completely destroy the session-
session_destroy();
Or you can try constants like so:
constants.php
<?php
define("VARIABLE_1", "Hotdog");
?>
page2.php
<?php
include_once("constants.php");
echo VARIABLE_1;
// output == Hotdog
?>
Just include the constants php script in the pages you want to use the variables in.
Check PHP Sessions:
page1.php
session_start();
$_SESSION['variable'] = "Variable";
page2.php
session_start();
echo $_SESSION['variable']
You can require_once("page2.php") in page1.php
That will allow you to use variables in page 2 across page 1.
Alternatively, you can also use sessions.
session_start() starts a new session or resumes an existing session. Include this on the first line of both pages.
Session parameters are set using $_SESSION["parameter-name"] = "something";
You can use the variables anywhere in the script.
To terminate sessions, use session_destroy()
You can use Sessions to achieve this , also you should know about QueryStrings.
For example:
Navigate to another page say "location2.php?Variable=123" here ?Variable is your var and 123 is value.
And on location2.php you can get this by simply $_GET["Variable"];
This can be achieved in many ways. You can use php session, you can include a file where you create that variable, or you can define it as a consant.
All of that depends how complex is that problem and which suits you best in this case.
Related
Below are two files first.php and second.php I want to access the $name variable in the second file.
I used global but it just access values inside the file. I do not want to use POST and GET method because I used POST already for redirecting to home.php
first.php
<?php
$name = 'New York';
?>
second.php
<?php
// Access variable here from the above first.php file
?>
INCLUDE
first.php
$name = "New York";
second.php
include "path/to/first.php";
echo $name; //echo "New York"
Here is the manual for PHP: Include
SESSIONS
If you don't want everything from first.php on second.php, you should use sessions.
first.php
session_start(); //start sessions, so you can use session variables
$_SESSION['name'] = "New York"; //set session variable called "name" to "New York"
second.php
session_start(); //start session so you can use session variables
echo $_SESSION['name']; //echo "New York"
Session variables work basically the same as regular variables, but you access them like an array. You have to start the session on every page to access them. I usually just start sessions in my header file so it's always included.
More info about PHP sessions
COOKIES
You could also use cookies, though I recommend using SESSIONS instead in most cases. Cookies are good for when that variable needs to last through multiple log in sessions or for a really long time, I usually use these for users settings themes in my application and such things that don't change often.
first.php
$name = "New York"; //set variable
setcookie("name", $name, time() + (86400 * 30), '/'); //set cookie that expires in 1 day
seconds.php
echo $_COOKIE['name']; //echo New York
More information on cookies
Try to use $_SESSION variable.
<?php //first.php
session_start();
$_SESSION['name'] = 'New York';
?>
<?php //second.php
session_start();
echo $_SESSION['name'];
?>
The best and safe way so far is using include_once(). If you want to include many files then you may use __autoload() (if it is object orient approach).
Check the documentation
$_REQUEST['var'];
This adapt with both post and get. Doesn't Metter what you are using.
Or with include 'file.php'
Or with setcookie('name', 'value'); $_COOKIE['name'];
Or with session storage
I've got a class, in which i start a Session.
Now I would need the Session ID from this Session in an other file - my header.php file.
Im quite new to PHP, so I would appreciate any help.
Thanks a lot Rohan
If you wish to use the Session in other files, you can simply use it using the $_SESSION variable.
A simple example::
test1.php
<?php
session_start();
$_SESSION['Test_Value'] = "100"; //set session variable.
?>
test2.php
<?php
session_start();
echo $_SESSION["Test_Value"]; //get session variable.
?>
Now when you open test1.php and then open test2.php you will see output of 100 which was set from test1.php
So if I start a session on one page, then send information via $_SESSION should I put session_start() at the top of the new page?
For Example:
page1.php:
session_start();
# code makes $foo
$_SESSION["var"] = $foo;
header("LOCATION: page2.php");
die;
page2b.php:
$foo = $_SESSION["var"];
# code uses $foo somehow
Does page2.php need session_start.php at the beggining of it? Especially if the only way to access it was through a page that starts a session?
Yes... in every page you should start a session
I want to store some information like user_name, country_name etc and need to be available through out the session.
what will be the best way to achieve this?
I have the following code to $_session variable but have no result.
page1.php
session_start;
$_SESSION['country_no']=1;
page2.php
session_start;
echo "here is session variable=".$_SESSION['country_no'];
exit;
session_start() is a function, use it like this:
session_start();
$_SESSION['country_no']=1;
You missed the ()
session_start();
session_start(); //needed before you create any session variables.
$_SESSION['mysessionvariablename'] = $myVar;
Read up on the session_start() function.
put session_start(); add the top of you page.
try session_start(); instead of session_start;
You're using a method :)
i want to retain the session variable while browsing from one page to the other
could some one help me that i could i achieve that??
thanks
Ok in the most basic fashion
<?php
//index.php
session_start();
$_SESSION['name'] = "Fred";
header("Location:displayname.php");
?>
<?php
//displayname.php
session_start();
echo $_SESSION['name'];
?>
Its made for it. I think you are missing the session_start() part of your script.
Then you can register a var as following: $_SESSION['var_name'] = 'value'; and you are done, if the session doesn't expires, that variable will be available everywhere on your domain.
Remember that session, by default, expires after 24 minutes of inactivity.
Basically the code should look like this:
session_start();
$_SESSION['var'] = 'var';
and you should be able to get the variable in another page:
$var = $_SESSION['var'];
Here it is. Just don't forget the session_start() at the top of each page where you need to use sessions.
This way,
<?php
session_start();
$_SESSION['views'] = 1;
echo "Pageviews = ". $_SESSION['views'];
?>
The data in _SESSION will be persisted across multiple pages until you invoke session_destroy() function.