简体   繁体   English

如何在PHP中的页面之间传输变量?

[英]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: 我正在尝试使用PHP创建一个登录表单,我希望从这样的表单中获取值:

$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. 技术上有效,但我不想要所有'file1.php',只是变量。 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. 会话是一种根据唯一会话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. 会话ID通常通过会话cookie发送到浏览器,ID用于检索现有会话数据。

You have to use session. 你必须使用会话。 Here is a simple demonstration: 这是一个简单的演示:


page1.php - Load page one to set the session variable page1.php - 加载第一页以设置会话变量

 <?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 page2.php - 加载第二页,它可以访问你在page1.php设置的会话变量

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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