-1

I have a page that acts like a controller, more or less (I'm not using frameworks) named control.php This page has a array declared on top and if's that verifies the page that called.Like this:

<?php
$arr= array();
$reference=str_replace("/","",parse_url($_SERVER['HTTP_REFERER'],PHP_URL_PATH));
if($reference=="page1.php"){
    $value="pages";
    array_push($arr, $value);
    header("Location: page2.php");
}else($reference=="page2.php"){ print_r($arr);  }

The objective is that when page1 call control.php it adds to the array and when page2 call control.php it prints the array. The problem that i don't understand is that if i print the array right after adding it added, but when i do it when is the page2, the array is empty.Declaring the array on top of the page doesn't make it available to all control.php?

Mar10Bel
  • 41
  • 2
  • 6
  • `header("Location: page2.php");` this code will perform redirects so i dont think you will have pages item added in array – Dipesh Parmar Jun 11 '15 at 10:12
  • Its that why the array appears empty? But i need to redirect to page2 (that lists the content of the array) after adding – Mar10Bel Jun 11 '15 at 10:17

1 Answers1

0

Every time you execute a php script, no matter if it's the same script or two different scripts, the environment set up from scratch. There is no state preserved between different scripts automatically. Variables and their values are not preserved. Everything starts from scratch.

If you want to make some information preserved between two different scripts, you need to introduce some storage mechanism. You can use sessions or a database to achieve that.

Mic Jaw
  • 366
  • 2
  • 8