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>
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>
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.