What is the output of this PHP code during the first and second requests?

73 views Asked by At

This is most likely a PHP noobie question.

To test some server caching configuration, I added the following code to my test suite:

<?php

if (array_key_exists('visited', $GLOBALS))
{
   print_r("We have already met");
} else {
   print_r("Hello ShimmerCat");
}

$GLOBALS['visited']=1;
?>

I'm expecting this code to take differents path of the branch during a first and second request, but it is returning the second message always. How can I achieve what I want?

1

There are 1 answers

3
Hussein Duvigneau On BEST ANSWER

PHP itself is stateless, meaning every time a user visits a PHP page, the whole operation is done from scratch with each variable being defined as in the script.

If you want to store data between views, the basic way is with cookies. If you want the details of what's remembered to be secure, use session cookies.