php - how to access the variable to the page without using GET and POST methods and without using header function? -


below 2 files first.php , second.php want access $name variable in second file. used global access values inside file. not want use post , get method because used post redirecting home.php

first.php  <?php  $name = 'new york';  ?>    second.php  <?php   // access variable here above first.php file   ?> 

include

first.php

$name = "new york"; 

second.php

include "path/to/first.php"; echo $name; //echo "new york" 

here manual php: include


sessions

if don't want first.php on second.php, should use sessions.

first.php

session_start(); //start sessions, can use session variables $_session['name'] = "new york"; //set session variable called "name" "new york" 

second.php

session_start(); //start session can use session variables echo $_session['name']; //echo "new york" 

session variables work same regular variables, access them array. have start session on every page access them. start sessions in header file it's included.

more info php sessions


cookies

you use cookies, though recommend using sessions instead in cases. cookies when variable needs last through multiple log in sessions or long time, use these users settings themes in application , such things don't change often.

first.php

$name = "new york"; //set variable setcookie("name", $name, time() + (86400 * 30), '/'); //set cookie expires in 1 day 

seconds.php

echo $_cookie['name']; //echo new york 

more information on cookies


Comments