PHP 将变量传递到下一页
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/871858/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHP Pass variable to next page
提问by Carlo
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).
该页面的表单操作是“Page2.php”。那么在 Page2.php 中,我如何才能访问该变量?我知道我可以用会话来做,但我认为这对于一个简单的字符串来说太多了,我只需要传递一个简单的字符串(文件名)。
How can I achieve this?
我怎样才能做到这一点?
Thanks!
谢谢!
回答by Jrgns
HTML / HTTP is stateless, in other words, what you did / saw on the previous page, is completely unconnected with the current page. Exceptif 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.
HTML / HTTP 是无状态的,换句话说,您在上一页所做的/看到的,与当前页面完全无关。除非您使用诸如会话、cookie 或 GET / POST 变量之类的东西。会话和 cookie 非常易于使用,会话比 cookie 安全得多。更安全,但并不完全安全。
Session:
会议:
//On page 1
$_SESSION['varname'] = $var_value;
//On page 2
$var_value = $_SESSION['varname'];
Remember to run the session_start();statement on both these pages before you try to access the $_SESSIONarray, and also before any output is sent to the browser.
请记住session_start();在尝试访问$_SESSION数组之前以及在将任何输出发送到浏览器之前在这两个页面上运行该语句。
Cookie:
曲奇饼:
//One page 1
$_COOKIE['varname'] = $var_value;
//On page 2
$var_value = $_COOKIE['varname'];
The big difference between sessions and cookies is that the value of the variable will be stored on the server if you're using sessions, and on the client if you're using cookies. I can't think of any good reason to use cookies instead of sessions, except if you want data to persist between sessions, but even then it's perhaps better to store it in a DB, and retrieve it based on a username or id.
会话和 cookie 之间的最大区别在于,如果您使用会话,变量的值将存储在服务器上,如果您使用 cookie,则变量的值将存储在客户端。我想不出有什么好的理由使用 cookie 而不是会话,除非您希望数据在会话之间保持不变,但即便如此,最好还是将它存储在数据库中,并根据用户名或 ID 检索它。
GET and POST
获取和发布
You can add the variable in the link to the next page:
您可以在下一页的链接中添加变量:
<a href="page2.php?varname=<?php echo $var_value ?>">Page2</a>
This will create a GET variable.
这将创建一个 GET 变量。
Another way is to include a hidden field in a form that submits to page two:
另一种方法是在提交到第二页的表单中包含一个隐藏字段:
<form method="get" action="page2.php">
<input type="hidden" name="varname" value="var_value">
<input type="submit">
</form>
And then on page two:
然后在第二页:
//Using GET
$var_value = $_GET['varname'];
//Using POST
$var_value = $_POST['varname'];
//Using GET, POST or COOKIE.
$var_value = $_REQUEST['varname'];
Just change the method for the form to postif you want to do it via post. Both are equally insecure, although GET is easier to hack.
post如果您想通过邮寄方式完成,只需将表单的方法更改为。两者都同样不安全,尽管 GET 更容易被破解。
The fact that each new request is, except for session data, a totally new instance of the script caught me when I first started coding in PHP. Once you get used to it, it's quite simple though.
当我第一次开始用 PHP 编码时,每个新请求都是一个全新的脚本实例,除了会话数据,这一事实让我印象深刻。一旦你习惯了它,虽然它很简单。
回答by Frank
Thanks for the answers above. Here's how I did it, I hope it helps those who follow. I'm looking to pass a registration number from one page to another, hence regNameand regValue:
感谢上面的回答。以上是我的做法,希望对后面的朋友有所帮助。我希望将注册号从一个页面传递到另一个页面,因此regName和regValue:
Create your first page, call it set_reg.php:
创建您的第一个页面,将其命名为 set_reg.php:
<?php
session_start();
$_SESSION['regName'] = $regValue;
?>
<form method="get" action="get_reg.php">
<input type="text" name="regName" value="">
<input type="submit">
</form>
Create your second page, call it get_reg.php:
创建您的第二个页面,将其命名为 get_reg.php:
<?php
session_start();
$regValue = $_GET['regName'];
echo "Your registration is: ".$regValue.".";
?>
<p><a href="set_reg.php">Back to set_reg.php</a>
Although not as comprehensive as the answer above, for my purposes this illustrates in simple fashion the relationship between the various elements.
虽然不像上面的答案那么全面,但出于我的目的,这以简单的方式说明了各种元素之间的关系。
回答by Paul Dixon
Passing data in the request
在请求中传递数据
You could either embed it as a hidden field in your form, or add it your forms action URL
您可以将其作为隐藏字段嵌入表单中,也可以将其添加到表单操作 URL
echo '<input type="hidden" name="myVariable" value="'.
htmlentities($myVariable).'">';
or
或者
echo '<form method="POST" action="Page2.php?myVariable='.
urlencode($myVariable).'">";
Note this also illustrates the use of htmlentitiesand urlencodewhen passing data around.
请注意,这也说明了在传递数据时使用htmlentities和urlencode。
Passing data in the session
在会话中传递数据
If the data doesn't need to be passed to the client side, then sessions may be more appropriate. Simply call session_start()at the start of each page, and you can get and set data into the $_SESSION array.
如果数据不需要传递到客户端,那么会话可能更合适。只需在每个页面的开头调用session_start(),您就可以获取和设置数据到 $_SESSION 数组中。
Security
安全
Since you state your value is actually a filename, you need to be aware of the security ramifications. If the filename has arrived from the client side, assume the user has tampered with the value. Check it for validity! What happens when the user passes the path to an important system file, or a file under their control? Can your script be used to "probe" the server for files that do or do not exist?
由于您声明您的值实际上是一个文件名,因此您需要了解安全后果。如果文件名是从客户端到达的,则假定用户已篡改了该值。检查它的有效性!当用户将路径传递给重要的系统文件或他们控制的文件时会发生什么?你的脚本可以用来“探测”服务器上存在或不存在的文件吗?
As you are clearly just getting started here, its worth reminding that this goes for any data which arrives in $_GET, $_POST or $_COOKIE - assume your worst enemy crafted the contents of those arrays, and code accordingly!
由于您显然刚刚开始,因此值得提醒的是,这适用于到达 $_GET、$_POST 或 $_COOKIE 的任何数据 - 假设您最大的敌人制作了这些数组的内容,并相应地编写代码!
回答by ravi sharma
There are three method to pass value in php.
在 php 中有三种传递值的方法。
- By post
- By get
- By making session variable
- 邮寄
- 通过获取
- 通过使会话变量
These three method are used for different purpose.For example if we want to receive our value on next page then we can use 'post' ($_POST) method as:-
这三种方法用于不同的目的。例如,如果我们想在下一页接收我们的值,那么我们可以使用 'post' ($_POST) 方法作为:-
$a=$_POST['field-name'];
If we require the value of variable on more than one page than we can use session variable as:-
如果我们需要多个页面上的变量值,那么我们可以将会话变量用作:-
$a=$_SESSION['field-name];
Before using this Syntax for creating SESSION variable we first have to add this tag at the very beginning of our php page
在使用这个语法创建 SESSION 变量之前,我们首先必须在我们的 php 页面的最开始添加这个标签
session_start();
GET method are generally used to print data on same page which used to take input from user. Its syntax is as:
GET 方法通常用于在用于接收用户输入的同一页面上打印数据。其语法如下:
$a=$_GET['field-name'];
POST method are generally consume more secure than GET because when we use Get method than it can display the data in URL bar.If the data is more sensitive data like password then it can be inggeris.
POST 方法通常比 GET 消耗更安全,因为当我们使用 Get 方法时,它不能在 URL 栏中显示数据。如果数据是更敏感的数据,如密码,那么它可能是 inggeris。
回答by Alo
Sessions would be the only good way, you could also use GET/POST but that would be potentially insecure.
会话将是唯一的好方法,您也可以使用 GET/POST 但这可能不安全。
回答by Bhargav Chudasama
try this code
试试这个代码
using hidden field we can pass php varibale to another page
使用隐藏字段,我们可以将 php 变量传递到另一个页面
page1.php
页面1.php
<?php $myVariable = "Some text";?>
<form method="post" action="page2.php">
<input type="hidden" name="text" value="<?php echo $myVariable; ?>">
<button type="submit">Submit</button>
</form>
pass php variable to hidden field value so you can access this variable into another page
将 php 变量传递给隐藏字段值,以便您可以将此变量访问到另一个页面
page2.php
页面2.php
<?php
$text=$_POST['text'];
echo $text;
?>
回答by Kaushik Kothiya
Sessions would be the only good way, you could also use GET/POST but that would be potentially insecure.
会话将是唯一的好方法,您也可以使用 GET/POST 但这可能不安全。
Passing data in the session If the data doesn't need to be passed to the client-side, then sessions may be more appropriate. Simply call session_start() at the start of each page, and you can get and set data into the $_SESSION array.
在会话中传递数据 如果不需要将数据传递到客户端,那么会话可能更合适。只需在每个页面的开头调用 session_start(),您就可以获取和设置数据到 $_SESSION 数组中。
Security Since you state your value is actually a filename, you need to be aware of the security ramifications. If the filename has arrived from the client-side, assume the user has tampered with the value. Check it for validity! What happens when the user passes the path to an important system file or a file under their control? Can your script be used to "probe" the server for files that do or do not exist?
安全性 由于您声明您的值实际上是一个文件名,因此您需要了解安全性后果。如果文件名是从客户端到达的,则假定用户已篡改了该值。检查它的有效性!当用户将路径传递给重要的系统文件或他们控制的文件时会发生什么?你的脚本可以用来“探测”服务器上存在或不存在的文件吗?
As you are clearly just getting started here, it is worth reminding that this goes for any data which arrives in $_GET, $_POST or $_COOKIE - assume your worst enemy crafted the contents of those arrays, and code accordingly!
由于您显然刚刚开始,因此值得提醒的是,这适用于到达 $_GET、$_POST 或 $_COOKIE 的任何数据 - 假设您最大的敌人制作了这些数组的内容,并相应地编写代码!
回答by V Kash Singh
**page 1**
<form action="exapmple.php?variable_name=$value" method="POST">
<button>
<input type="hidden" name="x">
</button>
</form>`
page 2
第2页
if(isset($_POST['x'])) {
$new_value=$_GET['variable_name'];
}

