Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Variable persistence in 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!

like image 334
Arthur Avatar asked Dec 14 '22 03:12

Arthur


1 Answers

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

like image 83
benlumley Avatar answered Dec 26 '22 22:12

benlumley