Home > DeveloperSection > Forums > How to Pass variable to next page in php?
Ankita Pandey
Ankita Pandey

Total Post:183

Posted on    October-30-2014 12:58 AM

 PHP PHP  Session  Session Variable 

 1 Reply(s)
 684  View(s)
Rate this:
it seems pretty simple but I can't find a good way to do it.

Say in the first page I create a variable

$myVariable = "Some text";

And the form's action for that page is "Page2.php". So in Page2.php, how can I have access to that variable? I know I can do it with sessions but I think it's too much for a simple string, and I do only need to pass a simple string (a file name).

lois waisbrooker
lois waisbrooker

Total Post:70

Posted on    October-30-2014 12:59 AM

HTML / HTTP is stateless, in other words, what you did / saw on the previous page, is completely disconnected with the current page. Except if you use something like sessions, cookies or GET / POST variables. Sessions and cookies are quite easy to use, with session being by far more secure than cookies. More secure, but not completely secure.


//page 1
$_SESSION['varname'] = $var_value;

//page 2
$var_value = $_SESSION['varname'];

Remember to run the session_start() statement on both these pages before you try to access the $_SESSION array, and also before any output is sent to the browser.

Don't want to miss updates? Please click the below button!

Follow MindStick