PHP将变量传递到下一页 [英] PHP Pass variable to next page

查看:220
本文介绍了PHP将变量传递到下一页的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这似乎很简单,但我找不到一种很好的方法.

请在第一页中创建一个变量

$myVariable = "Some text";

该页面的表单操作是"Page2.php".因此,在Page2.php中,如何访问该变量?我知道我可以在会话中做到这一点,但是我认为对于一个简单的字符串来说这太多了,我只需要传递一个简单的字符串(文件名)即可.

我该如何实现?

谢谢!

解决方案

HTML/HTTP是无状态的,换句话说,您在上一页所做的/与当前页面完全无关. 除了,如果您使用会话,cookie或GET/POST变量之类的东西.会话和cookie十分易于使用,并且会话比cookie更为安全.更安全,但不完全安全.

会话:

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

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

在尝试访问$_SESSION数组之前,以及在将任何输出发送到浏览器之前,请记住在这两个页面上都运行session_start();语句.

Cookie:

//One page 1
$_COOKIE['varname'] = $var_value;

//On page 2
$var_value = $_COOKIE['varname'];

会话和cookie之间的最大区别在于,如果使用会话,则变量的值将存储在服务器上,如果使用cookie,则变量的值将存储在客户端上.我想不出使用cookie代替会话的任何好理由,除非您希望数据在会话之间持久存在,但是即使那样,最好将其存储在数据库中,并根据用户名或id检索它. /p>

获取并发布

您可以在指向下一页的链接中添加变量:

<a href="page2.php?varname=<?php echo $var_value ?>">Page2</a>

这将创建一个GET变量.

另一种方法是在提交到第二页的表单中包含一个隐藏字段:

<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.两者同样不安全,尽管GET更易于破解.

当我第一次开始用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";

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?

Thanks!

解决方案

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.

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 $_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'];

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 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.

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 post if you want to do it via post. Both are equally insecure, although GET is easier to hack.

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将变量传递到下一页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆