简体   繁体   中英

How can I transfer variables between pages in PHP?

I am trying to make a login form with PHP, and I want to get a value from a form like so:

$name = $_POST["name"];
$email = $_POST["email"];

then, when the user clicks "Submit", I want the new page to have access to these variables. Using the

include 'file1.php';

technically works, but I dont want ALL of 'file1.php', just the variables. Any Suggestions?

This is exactly what sessions are for.

Sessions are a simple way to store data for individual users against a unique session ID. This can be used to persist state information between page requests. Session IDs are normally sent to the browser via session cookies and the ID is used to retrieve existing session data.

You have to use session. Here is a simple demonstration:


page1.php - Load page one to set the session variable

 <?php // begin the session session_start(); // set the value of the session variable 'foo' $_SESSION['foo']='bar'; // echo a little message to say it is done echo 'Setting value of foo'; ?> 


page2.php - Load page two and it will have access to the session variable you set in page1.php

 <?php // begin our session session_start(); // echo the session variable echo 'The value of foo is '.$_SESSION['foo']; ?> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM