php session variable expiring - php

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.

Related

What is wrong with php configuration?

This is simple php code with sessions:
<?php
session_start();
function testSession() {
//global $_SESSION;
var_dump($_SESSION['test']);
}
if (!isset($_SESSION['test'])) {
echo " Nope";
$_SESSION['test'] = " Yeap";
} else {
testSession();
}
?>
The problem is that "$_SESSION" is not a superglobal. "$_SESSION" is undefined in testSession function scope, it is visible only in the main scope. If I uncomment "global $_SESSION" than all will work.
upd:
The error is "Undefined variable: _SESSION" at line var_dump($_SESSION['test']);
upd:
if you write this code:
<?php
session_start();
if (!isset($_SESSION['test'])) {
echo " Nope";
$_SESSION['test'] = " Yeap";
} else {
var_dump($_SESSION['test']);
}
?>
all will work correctly.
Always put your session_start(); at the start of the page.
Try using:
<?php
session_start();
echo "Session test ";
function testSession() {
//global $_SESSION;
var_dump($_SESSION['test']);
}
if (!isset($_SESSION['test'])) {
echo " Nope";
$_SESSION['test'] = " Yeap";
} else {
testSession();
}
?>
Make sure session_start(); is called before any sessions are being called. So a safe bet would be to put it at the beginning of your page, immediately after the opening <?php tag before anything else. Also ensure there are no whitespaces/tabs before the opening <?php tag.
After the header redirect, end the current script using exit(); (Others have also suggested session_write_close(); and session_regenerate_id(true), you can try those as well, but I'd use exit();).
Make sure cookies are enabled in the browser you are using to test it on.
Ensure register_globals is off, you can check this on the php.ini file and also using phpinfo(). Refer to this as to how to turn it off.
Make sure you didn't delete or empty the session.
Make sure the key in your $_SESSION superglobal array is not overwritten anywhere.
Make sure you redirect to the same domain. So redirecting from a www.yourdomain.com to yourdomain.com doesn't carry the session forward.
Make sure your file extension is .php (it happens!).
Session variables not working php

How to destroy Session after page load?

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();
}

Opera Mini & PHP Sessions

I have tried passing sensitive information from 1 page to the next using Cookies and PHP Sessions but still no luck. This works fine in all browsers I have tested except Opera Mini. I also found this: http://caniuse.com/#search=cookie
This is how I have it currently setup.
page1.php:
<?php
session_start();
$time = time();
$key = '';
$hash = md5($key . $time);
$_SESSION['inno'] = '';
header("Location: page2.php". $hash);
exit;
?>
page2.php:
<?php
session_start();
if (isset( $_SESSION['inno'])) {
include("../global/header.php");
include("../global/content.php");
}
session_destroy();
?>
The content of the page is the sensitive information, So that is why it goes from page1.php to page2.php.
Is there some kind of workaround for this if passing information this way isn't supported in Opera Mini?
Use classes for reusable resources, they are less messy.
Looks like you forgot to assign data, so
Here is the promising structure:
index.php
<?php
include_once 'session.php';
$my_session = new session_helper;
//SET YOUR SESSION VARIABLES
if($my_session->get_session_data('username') == null){
$sessionData = array(
'username'=>'me',
'logged'=>true,
'password'=>md5('password')
);
$my_session->set_session_data($sessionData);
}
?>
View my session
view_session.php
<?php
include_once 'session.php';
$my_session = new session_helper;
?>
<!--GET YOUR SESSION VARIABLES-->
<p>Username: <?php echo $my_session->get_session_data('username'); ?></p>
<p>Logged: <?php echo $my_session->get_session_data('logged'); ?></p>
<p>Password: <?php echo $my_session->get_session_data('password'); ?></p>
<p> </p>
<?php $my_session->debug_session(); ?>
session.php to get rid of headaches
<?php
//MANAGE YOUR SESSION
class session_helper
{
//Start session when class instance created
function __construct()
{
session_start();
}
//Session data set using an array to easily add multiple values e.g. on login page
function set_session_data($sessionData)
{
forEach($sessionData as $key => $value){//Go through each key
$_SESSION[$key] = $value;//And assign it to session
}
}
function get_session_data($session_data_key)
{
return $_SESSION[$session_data_key];//Call this to get your values
}
function debug_session()
{
print_r($_SESSION); //Check what session contains if something is wrong
}
function close_session()
{
session_destroy(); //Good to use for fresh start/logout.
}
}
?>
session.php manages sessions. Accessing directly will output an empty page.
index.php uses a condition to set the variables. You don't need to rework data everytime when you have it.
view_session.php has all the info regarding the session.
Best practice of retrieving info is storing secure/encrypted primary keys of the database, then use them to retrieve everything else from the database, e.g. by user ID get the e-mail, profile creation time, name, surname etc.

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';

Categories