PHP 将变量传递到下一页
看起来很简单,但我找不到好的方法.
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";
该页面的表单操作是Page2.php".那么在 Page2.php 中,我如何才能访问该变量?我知道我可以用会话来做,但我认为这对于一个简单的字符串来说太多了,我只需要传递一个简单的字符串(文件名).
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).
我怎样才能做到这一点?
How can I achieve this?
谢谢!
HTML/HTTP 是无状态的,换句话说,您在上一页所做的/看到的,与当前页面完全无关.除非,如果您使用诸如会话、cookie 或 GET/POST 变量之类的东西.会话和 cookie 非常易于使用,会话比 cookie 安全得多.更安全,但不完全安全.
HTML / HTTP is stateless, in other words, what you did / saw on the previous page, is completely unconnected 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.
会话:
//On page 1
$_SESSION['varname'] = $var_value;
//On page 2
$var_value = $_SESSION['varname'];
记住在尝试访问 $_SESSION
数组之前,以及在任何输出发送到浏览器之前,在这两个页面上运行 session_start();
语句.
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.
Cookie:
//One page 1
$_COOKIE['varname'] = $var_value;
//On page 2
$var_value = $_COOKIE['varname'];
会话和 cookie 之间的最大区别在于,如果您使用会话,变量的值将存储在服务器上,如果您使用 cookie,则变量的值将存储在客户端上.我想不出使用 cookie 代替会话的任何充分理由,除非您希望数据在会话之间保持不变,但即便如此,最好还是将其存储在数据库中,并根据用户名或 ID 检索它.
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.
GET 和 POST
您可以在下一页的链接中添加变量:
You can add the variable in the link to the next page:
<a href="page2.php?varname=<?php echo $var_value ?>">Page2</a>
这将创建一个 GET 变量.
This will create a GET variable.
另一种方法是在提交到第二页的表单中包含一个隐藏字段:
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>
然后在第二页:
//Using GET
$var_value = $_GET['varname'];
//Using POST
$var_value = $_POST['varname'];
//Using GET, POST or COOKIE.
$var_value = $_REQUEST['varname'];
如果你想通过 post 来做,只需将表单的方法更改为 post
.两者都同样不安全,尽管 GET 更容易被破解.
Just change the method for the form to post
if you want to do it via post. Both are equally insecure, although GET is easier to hack.
当我第一次开始用 PHP 编码时,每个新请求都是一个全新的脚本实例,除了会话数据,这一事实让我印象深刻.一旦你习惯了它,它就会很简单.
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.