1

Can someone tell how to get a PHP variable even when you reload the page? I have tried with the session but it seems that the value is changed when the page is refreshed.

0

3 Answers 3

1
<?php

session_start();

if( empty($_SESSION['test']) ) {
     $_SESSION['test'] = date( 'Y-m-d H:i:s' );
}

echo $_SESSION['test'];

The first time this script is executed the current datetime will be assigned to the test key (and echoed out). Next time you run this script the old date will be echoed.

Sign up to request clarification or add additional context in comments.

Comments

0

Session are what you want. Look at the following code:

<?php
// start session
session_start();


$var1 = "Hello World";
// save variable you want to have after the reload
$_SESSION["myvar"] = $var1;

// echo session value
echo $_SESSION["myvar"];
?>

Comments

0

You can try session with ajax.

  1. save it as a session variable and retrieve it over AJAX each time the page loads.
  2. save it as cookie (might as well do the javascript approach if you're going to cookie it.

Any PHP approach would be clunky as you'd have to first send the value of the variable to a PHP script over AJAX, then retrieve it over AJAX after reload.

Or you could save it as a property of the page's local storage.save it as cookie.and append the variable to the URL hash so it can access by location.hash after the browsers reload.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.