php 将表单数据存储为会话变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3791414/
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
Storing Form Data as a Session Variable
提问by Kevin Johnson
So I was wondering if it would be possible to store data coming in from a form as a session variable.
所以我想知道是否可以将来自表单的数据存储为会话变量。
Heres what I have so far, but I don't know what to put for the Form Action.
到目前为止,这是我所拥有的,但我不知道该为 Form Action 放置什么。
Thanks for looking!
感谢您的关注!
<strong>Test Form</strong>
<form action="" method"post">
<input type="text" name="picturenum"/>
<input type="submit" name="Submit" value="Submit!" />
</form>
<?
if (isset($_POST['Submit'])) {
$_session['picturenum'] = $_POST['picturenum'];
}
?>
<strong><? echo $_session['picturenum'];?></strong>
回答by Fopa Léon Constantin
To use session variables it's necessary to start the session by using the session_start
function, this will allowed you to store your data in the global variable $_SESSION
in a persistent way.
要使用会话变量,必须使用该session_start
函数启动会话,这将允许您以$_SESSION
持久方式将数据存储在全局变量中。
so your code will finally looks like this :
所以你的代码最终会是这样的:
<strong>Test Form</strong>
<form action="" method"post">
<input type="text" name="picturenum"/>
<input type="submit" name="Submit" value="Submit!" />
</form>
<?php
// starting the session
session_start();
if (isset($_POST['Submit'])) {
$_SESSION['picturenum'] = $_POST['picturenum'];
}
?>
<strong><?php echo $_SESSION['picturenum'];?></strong>
to make it easy to use and to avoid forgetting it again, you can create a session_file.php
which will be include in all your codes and will start the session for you
为了使其易于使用并避免再次忘记它,您可以创建一个session_file.php
将包含在您的所有代码中并为您启动会话的
session_start.php
session_start.php
<?php
session_start();
?>
and then include it wherever you like :
然后将其包含在您喜欢的任何位置:
<strong>Test Form</strong>
<form action="" method"post">
<input type="text" name="picturenum"/>
<input type="submit" name="Submit" value="Submit!" />
</form>
<?php
// including the session file
require_once("session_start.php");
if (isset($_POST['Submit'])) {
$_SESSION['picturenum'] = $_POST['picturenum'];
}
?>
that's is the more portable and easy way to maintain in the future.
这是将来维护起来更便携、更简单的方法。
other remarks
其他备注
if you are using Apache version 2 or more, be carefull instead of
<?
to open php's tags, use<?php
, otherwise your code will not be interpretedvariables names in php are case-sensitives instead of write $_session, write $_SESSION in capital letters
如果您使用的是 Apache 2 或更高版本,请小心而不是
<?
打开 php 的标签,使用<?php
,否则您的代码将不会被解释php中的变量名区分大小写而不是写$_session,用大写字母写$_SESSION
good work !
干得好!
回答by kjones1876
That's perfectly fine and will work. But to use sessions you have to put session_start();
on the first line of the php code. So basically
这完全没问题,而且会起作用。但是要使用会话,您必须放在session_start();
php 代码的第一行。所以基本上
<?php
session_start();
//rest of stuff
?>
回答by Todd Moses
Yes this is possible. kizzie is correct with the session_start();
having to go first.
是的,这是可能的。kizzie 是正确的session_start();
,必须先走。
another observation I made is that you need to filter your form data using:
我所做的另一个观察是您需要使用以下方法过滤表单数据:
strip_tags($value);
and/or
和/或
stripslashes($value);
回答by giuseppe
You can solve this problem using this code:
您可以使用以下代码解决此问题:
if(!empty($_GET['variable from which you get']))
{
$_SESSION['something']= $_GET['variable from which you get'];
}
So you get the variable from a GET form, you will store in the $_SESSION['whatever']
variable just once when $_GET['variable from which you get']
is set and if it is empty $_SESSION['something']
will store the old parameter
因此,您从 GET 表单中获取变量,您将在设置$_SESSION['whatever']
时仅在变量中存储一次$_GET['variable from which you get']
,如果为空,$_SESSION['something']
则将存储旧参数