How to destroy Session after page load? - php

I want to show a session variable in a web page and if the web page get reloaded it will destroy the session means user can't see the same message if he reload the same webpage.
for that I have used the following procedure using PHP
<?php
if (!isset($_SESSION['message'])) {
echo $_SESSION['message'];
}
session_unset();
?>
But when i reload the same page again I saw the following Error:
Notice: Undefined index: message
Can anyone suggest me what can be the procedure so that I can ignore the notice

Code this should solve your query
if session is not destroyed 'hello' message will not be shown.
<?php
$_SESSION['message']='hello';
if(isset($_SESSION['message'])) {
echo $_SESSION['message'];
}
unset($_SESSION);
//test
echo $_SESSION['message'];
?>

Update if condition
<?php
if(isset($_SESSION['messsage'])) {
echo $_SESSION['message'];
}
session_unset();
?>

as you can know that the code runs line to line so if you have any condition where you want to destroy the session, put session_destroy(); there, otherwise you can put it at the last line of your code. it is the last point where your goes.
session_destroy();

Please Try This :
$_SESSION['message'] = "Hello";
if(isset($_SESSION['message'])){
echo $_SESSION['message'];
session_destroy();
}

Related

use php to display log in and log out link according to active session

This is the code i have at the moment but will not work, displays log out button when logged in only on one page then logs user out automatically ?
<?php
if(!session_is_registered(myusername))
{
echo '<b>Log In</b>';
}
else
{
echo '<b>Log Out</b>';
}
?>
You had forgotten to do session_start() out of many things, and please make sure to share that, on every one of your pages, where you want to enable session protection.
<?php
session_start();
if(!isset($_SESSION['username']) && empty($_SESSION['username']))
{
echo '<b>Log In</b>';
}
else
{
echo '<b>Log Out</b>';
}
?>
session_is_registered is deprecated. Try using $_SESSION instead
if ($_SESSION["isLoggedIn"]) {
// Log out HTML goes here
} else {
// Log in HTML goes here
}
You'll need to include session_start() at the top of all of your files and you can set $_SESSION["isLoggedIn"] just like any other variable: $_SESSION["isLoggedIn"] = TRUE

getting undefined variables when using session

I get an undefiend variable error when i call another $_SESSION but when i remove this piece of code it shows no errors, I am using $_SESSION in order to pass one variable to another php script, can anyone enlighten me on what may be causing this issue?
I know the variable is defiened but for some reason calling a $_SESSION causes this error?
<div>
<?
if(empty($item_details['trucks'])) {
include_once ('trucks.php');
$_SESSION['runmapapi'] = 'start';
//$runmapapi == true;
echo '<p> Success</p></div>';
} else {
echo '<p>failed</p></div>';
}
?>
<div>
<p>Cars</p>
</div>
</div>
</div></body>
<? } ?>
<? } ?>
<?= $print_footer; ?>
when you are using session variable, you have to initialize the session by using
session_start();
so wherever you use any of the session variable in your page, make sure you first declare the session_start()
so in your page it should be
//initialize session to use session variable
session_start();
include_once ('trucks.php');
$_SESSION['runmapapi'] = 'start';

Start session after destroy php

I have a logout file for my web application. I would like to create a session after the logout so that I can echo a logout success message. My logout code works but I am unable to create a new session after destroying the previous one.
What is the best way to do this, see code:
//LOGOUT.PHP
session_start();
//// unset all $_SESSION variables
session_regenerate_id();
session_unset();
session_destroy();
$_SESSION['logoutsuccess'] = 'You have successfully logged out.';
header("Location: /login/");
exit;
//LOGIN.PHP (ECHO SUCCESS MESSAGE)
if(isset($_SESSION['logoutsuccess']))
{
echo '<div class="success">'.$_SESSION['logoutsuccess'].'</div>';
unset($_SESSION['logoutsuccess']);
}
I would like to avoid passing variables in the url if possible.
Call session_start() again after session_destroy()?
Just start a new session:
session_start();
session_destroy();
session_start();
$_SESSION['food'] = 'pizza';
Instead of trying to store it as a session variable, you could check the referer and check for /logout/
if(strpos($_SERVER['HTTP_REFERER'], 'logout') !== false) {
echo 'You have successfully logged out.';
}
session_id($session_id_to_destroy);
session_start();
session_destroy();
session_commit();

Session doesn't get cleared

I have a google chrome extension talking to a PHP script via XHR. I have a flag in the PHP script to check if a button is clicked in a popup if proper POST data is sent. If this button is clicked, the current session needs to be destroyed and when new data is entered by clicking the add button it shouldn't be appending data to an existing session but start a new one instead. It's currently just re-appending the data to the array. It's as if the clear session command is being ignored, however the IF statement is being called correctly to the console. How do I get the clear session statement to clear the session?
PHP script:
<?php $json = $_POST['data'];
if($json['button'] == "clear"){
session_unset();
session_destroy();
echo 'session destroyed!'; //being called to console
exit();
}
if (!isset ($_COOKIE[ini_get($_SESSION['data'])])) {
session_start();
}
if(!isset($_SESSION['data'])){
$_SESSION['data'] = $json;
} elseif (isset($_SESSION['data'])){
array_push($_SESSION['data'], $json);
}
print_r($_SESSION['data']);
?>
You must call session_start() first:
if($json['button'] == "clear")
{
session_start(); // <-- first
session_unset();
session_destroy();
echo 'session destroyed!'; //being called to console
exit();
}

php session variable expiring

i am using a session variable to authenticate, acc to my knowledge the session variable is supposed to be stored at the server even when new pages are loaded.
I am using the following code:
<?php
session_start();
echo $_POST['path'];
if($_POST['path']=="index")
{
$_SESSION['rightPath']=1;
if(isset($_SESSION['rightPath']))
echo "it is set";
?>
<script type="text/javascript">parent.location='UI.php'</script>
<?php
}
else
{?>
<script type="text/javascript">parent.location='index.php'</script>
<?php
}
?>
here this isset function tells me that the variable is set but in the next page ui.php is it not giving me the same result.
<?php
if(!isset($_SESSION['rightPath']))
{
echo "it not is set";?>
<?php }
?>
this is the ui.php page snippet. here the if statement is executing.
what am i doing wrong ?
you need to start session here is well
<?php
session_start();
if(!isset($_SESSION['rightPath']))
{
echo "it not is set";?>
<?php }
?>
You are not starting the session in UI.php. The code should be like this, with session_start at the top:
<?php
session_start();
if(!isset($_SESSION['rightPath']))
{
echo "it not is set";?>
}
?>
The session_start() creates a session or resumes the current one. So, while you are creating the session earlier, it is NOT resumed unless you do a session_start() again on every page where you intend to use the session variables.

Categories