0

I have a variable that I want to persist between pages. I've tried setting it using POST, and when that didn't work, I used SESSION. Yet in either case, the variable is lost when I go to the new page.

Here is the code when the variable is set:

$_SESSION['filename'] = $boardName;
$debug->alert_code_info($_SESSION['filename']);

And just as a debugging test, I used this line to check if it persisted:

$debug->alert_code_info($_SESSION['filename']);

You can substitute POST for SESSION in the above lines - I've tried that as well, and it doesn't work either.

Why are these variables not persisting from page to page?

Eric LaRue
  • 47
  • 10

2 Answers2

0

Make sure you have session_start(); on both the pages on the top.

Yes, $_POST would work. In that case, please go through this article. Post data in PHP

$_POST is an array that stores data received from an HTTP POST request to a particular page. It is not like $_SESSION variable which persists across the pages.

hsuk
  • 6,321
  • 12
  • 45
  • 78
  • That explains why it wouldn't work using SESSION - is there any reason why it wouldn't work using POST? – Eric LaRue Jun 13 '17 at 19:31
  • answer is correct. you can use $GLOBALS or $_SESSION for persist data among your php server – Himesh Suthar Jun 13 '17 at 19:37
  • 1
    @HimeshSuthar - `$GLOBALS` does not persist, it is simply a globally-available "superglobal" array that generally should be avoided. `$_SESSION` and `$_COOKIE` do persist, in different ways of course. – Jared Farrish Jun 13 '17 at 20:04
0

PHP has "page scope". What that means, is that when a script is run, all variables are created, and when the script completes, all variables are disposed of. It has no persistence. Without output buffering (a whole subject unto itself) for all intents and purposes, as soon as the page is accessed and output generated, the script will be complete. This model is very close to the way HTTP is designed.

As noted, you need some other form of persistence to carry variables between pages. Sessions, databases & datastores, cache, cookies and shared memory are all routinely used in php applications.

Which one is appropriate requires some further understanding of why you need the persistence.

You can also pass variables from one page/script to another using the standard web mechanics of url parameters (automatically placed in the $_GET superglobal), POST variables (automatically placed in the $_POST superglobal) or cookies (automatically placed in the $_COOKIE superglobal).

Some of these are connected, in that php sessions by default utilize a cookie used by the server to identify a returning client.

In regards to your specific question about POST variables, so long as a form is POSTed targetting your script, the POST variables will be available in the $_POST. A much used technique is to either utilize hidden form fields or to set the value of form fields when there is a multi form process, or in the case of error handling.

To be clear, again PHP has no persistence, which is different from some other languages which run under application servers (such as Java with a J2EE server). In J2EE Objects can be created and will live inside the Application server across any number of page requests. PHP in some implementations has some minor persistence capabilities as in the case of database pooling, but nothing intrinsic to the language.

Once you are clear about page scope, and that essentially nothing lives beyond one HTTP request/response, your persistence options should be clearer.

gview
  • 13,306
  • 2
  • 38
  • 45