0

I am trying to show a div content and remember it until browser close. How to call and set $_SESSION using php?

<div class="show">Show content</div>
userknowmore
  • 137
  • 2
  • 14
  • Try search on google, and you will get 100 examples of $_SESSION usages and how to. – Jesper Jul 04 '15 at 20:14
  • 2
    I'm voting to close this question because the answer is explained a thousand+ times on the internet and very easy to find with little googling. – Wouter J Jul 04 '15 at 22:03
  • Check out https://stackoverflow.com/questions/5489365/how-to-use-store-and-use-session-variables-across-pages – LinkBerest Jul 04 '15 at 22:23

1 Answers1

1

Try this:

<!--HTML PART-->
<form method="post">
 <input type="submit" name="setSession" value="Set Session" />
</form>

<!--PHP PART-->
<?php
if(isset($_POST['setSession'])) {
 session_start();
 $_SESSION['set'] = true;
}
?>

Above is the page that will set the session. Underneath is the content that you want to hide/show when the session is set or not set.

<!--YOUR PAGE-->
<?php
if($_SESSION['set']) {
?>
 <div class="show">Show content</div>
<?php
} elseif(!$_SESSION['set']) {
?>
 <div class="show">Set the session first</div>
<?php
}
?>

Remember, always put: session_start(); on the top of your PHP file.

PHP ONLY

<?php
session_start();
$_SESSION['fruit'] = 'apple';

if($_SESSION['fruit'] == 'apple') {
//the part you want to be displayed when there is a session
} else {
//the part you want to be displayed when there is NO session
}
?>

NOTE: The session will always be set, since it will be set when you access this page.

peer
  • 1,001
  • 4
  • 13
  • 29