PHP中的变量持久性 [英] Variable persistence in PHP
问题描述
我有一个php页面, 在该页面上,我有文本框和一个提交按钮, 此按钮在以下部分中运行php:
if(isset($ _ POST ['Add'])){code}
这在这里很好用,并且在该部分中计算并使用了$ name,$ base,$ location等.但是该部分代码会生成另一个提交按钮,以驱动另一部分代码.
我希望在代码的第二部分中将数据添加到数据库中. 现在我已经知道如何执行此操作,问题是变量$ name等在此时具有NULL值..但是只能在第一个代码段运行并获得值之后调用它们. /p>
如何保持这些值直到添加它们?
资源:
该页面随时可以尝试:
位置必须为'DNN:NN:NN:NN'形式,其中D为"D",N为0-9整数
http://www.teamdelta.byethost12.com/postroute.php
php文件的代码作为文本文件!
http://www.teamdelta.byethost12.com/postroute.php
第116和149行是2个按钮运行部分的开始!
我认为您正在寻找PHP的会话处理内容...
例如,第一页:
session_start(); # start session handling.
$_SESSION['test']='hello world';
exit();
第二页:
session_start(); # start session handling again.
echo $_SESSION['test']; # prints out 'hello world'
在幕后,当您首次调用会话启动时,php在用户浏览器中设置了一个cookie,在执行结束时将$ _SESSION数组序列化到磁盘,然后在下一页请求中接收到该cookie时,它与序列化的数据匹配,并在调用session_start()时将其作为$ _SESSION数组加载回去.
有关会话处理内容的完整详细信息:
http://uk.php.net/manual/en/book. session.php
i have a php page, on that page i have text boxes and a submit button, this button runs php in a section:
if(isset($_POST['Add'])){code}
This works fine here and in that section $name,$base,$location etc are calculated and used. but that section of code generates another submit button that drives another section of code.
it is in this second section of code that i wish to add data to the DB. Now i already know how to do this, the problem is that the variables $name and so forth have a value of NULL at this point.. but they can only be called after the first code section has been run where they gain value.
How do i maintain these values until the point where i add them?
Resources:
the page feel free to try it out:
location mustbe of the form 'DNN:NN:NN:NN' where D is "D" and N is a 0-9 integer
http://www.teamdelta.byethost12.com/postroute.php
the code of the php file as a text file!
http://www.teamdelta.byethost12.com/postroute.php
lines 116 and 149 are the start of the 2 button run sections!
I think you are looking for PHP's session handling stuff ...
As an example, first page:
session_start(); # start session handling.
$_SESSION['test']='hello world';
exit();
second page:
session_start(); # start session handling again.
echo $_SESSION['test']; # prints out 'hello world'
Behind the scenes, php has set a cookie in the users browser when you first call session start, serialized the $_SESSION array to disk at the end of execution, and then when it receives the cookie back on the next page request, it matches the serialised data and loads it back as the $_SESSION array when you call session_start();
Full details on the session handling stuff:
http://uk.php.net/manual/en/book.session.php
这篇关于PHP中的变量持久性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!