0

Theres many pre-set globals within PHP, Theres $_SESSION, $_REQUEST, $_POST,$_GET,$_COOKIE... So on and so fourth, they each have different usages, but i'm wondering if it's possible to Create another global array which retains it's values after setting.. Example:

index.php:

include "Test.php"; 

print_r($Array);

Test.php

$Array = array();

Set.php:

include "Test.php";
$Array[] = "Test"; 

Procedure:

User navigates to Set.php -> set.php adds values to the array set on Test.php -> User Navigates to index.php & Values are retained.

As there is nothing manipulating the Array on test.php, the values are cleared. Is there any way to make this array retain it's values after refresh and no manipulations?

Sophie Mackeral
  • 897
  • 4
  • 12
  • 21
  • Please see [variable scope](http://be2.php.net/manual/en/language.variables.scope.php) ("The *global* keyword"). – Tomas Creemers Aug 14 '13 at 22:56
  • 2
    This is what $_SESSION is made for... – Macmade Aug 14 '13 at 22:57
  • @TomasCreemers `syntax error, unexpected '=', expecting ',' or ';' in C:\xampp\htdocs\daz\Test.php on line 3` and the line consists of: `global $Array = array();` – Sophie Mackeral Aug 14 '13 at 22:57
  • @SophieMackeral: you do not have to use `global` when creating the variable. You only 'have' to use `global` when trying to access the global variable from another scope (like inside a function). ('have' inside quotes because you can also use `$GLOBALS['variableName']` to access a global variable `$variableName`.) – Tomas Creemers Aug 14 '13 at 23:03
  • you might be interested in reading: http://php.net/features.sessions – hakre Aug 14 '13 at 23:03
  • http://stackoverflow.com/questions/6061499/has-anyone-created-a-php-session-like-class-in-user-code-not-native – Wrikken Aug 14 '13 at 23:07
  • possible duplicate of [PHP Pass variable to next page](http://stackoverflow.com/questions/871858/php-pass-variable-to-next-page) – HamZa Aug 14 '13 at 23:09

1 Answers1

5

retain it's values after refresh

That's what session state is for, among other things. Note that the values in $_SESSION are also "cleared" unless you invoke session_start(), which hooks into the web server itself to get the values for that session.

All of your other examples are also "cleared" with each page. In fact, all of these are supplied by the request itself when the page is requested:

  • $_REQUEST
  • $_POST
  • $_GET
  • $_COOKIE

The items in $_SESSION don't persist because it's some kind of special global. They persist because it's implemented to use the session state management of the web server. You could implement anything the same way, persisting state in any medium outside of the page. Such as:

  • A file
  • A database
  • etc.

Then you could write a function similar to session_start() which fetches the data from that external source and re-populates the global collection. You'd just have to call it at the start of each page, just like session_start().

David
  • 208,112
  • 36
  • 198
  • 279
  • 1
    Deleted my answer as yours is more thorough +1. – Mike Brant Aug 14 '13 at 23:01
  • Writing a function like session_start which will create a hooked super global, wouldn't I have to modify the Source code of PHP, to implement such a thing/ – Sophie Mackeral Aug 14 '13 at 23:01
  • @SophieMackeral Maybe you should tell us why you want to do this. Can't you simply use the session mechanism? – Macmade Aug 14 '13 at 23:02
  • @SophieMackeral: If you wanted that function to be available without having to include anything, probably. Since this would be a custom implementation then you'd also need to include the script which implements it. So technically it would be *two* lines to initialize it, instead of the one for the built-in `session_start()`. – David Aug 14 '13 at 23:03