[php] Pass variables between two PHP pages without using a form or the URL of page

I want to pass a couple of variables from one PHP page to another. I am not using a form. The variables are some messages that the target page will display if something goes wrong. How can I pass these variables to the other PHP page while keeping them invisible?

e.g. let's say that I have these two variables:

//Original page
$message1 = "A message";
$message2 = "Another message";

and I want to pass them from page1.php to page2.php. I don't want to pass them through the URL.

//I don't want

Is there a way (maybe through $_POST?) to send the variables? If anyone is wondering why I want them to be invisible, I just don't want a big URL address with parameters like "&message=Problem while uploading your file. This is not a valid .zip file" and I don't have much time to change the redirections of my page to avoid this problem.

This question is related to php variables post

The answer is

Sessions would be good choice for you. Take a look at these two examples from PHP Manual:

Code of page1.php

// page1.php


echo 'Welcome to page #1';

$_SESSION['favcolor'] = 'green';
$_SESSION['animal']   = 'cat';
$_SESSION['time']     = time();

// Works if session cookie was accepted
echo '<br /><a href="page2.php">page 2</a>';

// Or pass along the session id, if needed
echo '<br /><a href="page2.php?' . SID . '">page 2</a>';

Code of page2.php

// page2.php


echo 'Welcome to page #2<br />';

echo $_SESSION['favcolor']; // green
echo $_SESSION['animal'];   // cat
echo date('Y m d H:i:s', $_SESSION['time']);

// You may want to use SID here, like we did in page1.php
echo '<br /><a href="page1.php">page 1</a>';

To clear up things - SID is PHP's predefined constant which contains session name and its id. Example SID value:


Similar questions with php tag:

Similar questions with variables tag:

Similar questions with post tag: