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';
Related
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
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();
}
How can I pass two session variables to loan_officer1.php in below code
function redirect_to($location = NULL){
if($location != NULL){
header("Location:{$location}");
exit;
}
}
if(mysql_query($query,$connection)){
//sucess
redirect_to('loan_officer1.php');
}else{
confirm_query($query);
}
When you use sessions in php then you don't need to pass session variables explicitly.
You can declare session variables suppose on your index page as
<?php
session_start();
$_Session['loan_officer_name'] = 'ABC DEF';
$_Session['loan_officer_post'] = 'Officer';
?>
and then just call your session variables wherever on any page you need to call like
<?php
session_start();
echo $_Session['loan_officer_name'];
?>
Remember to call session_start(); on page you want to access session variables.
You do not need to pass the..just use this in your loan_officer1.php
$_SESSION['name'] and it will give you the value stored in it
Just give the value to $_SESSION
For ex,like $_SESSION['location']
Im using two pages first page s getting values from url and its displaying some content. I included first page in second page but the first page should not be displayed but i have to access the values in second page which is used in first page..
The coding for first page
first.php
In utl the value is passed as first.php?Logid=7773&shiftdate=2013-01-04&shiftid=146&pshift=1&tsid=1&dctype=timebased
<?php
$Logid=$_GET['Logid'];
$ShiftDate=$_GET['shiftdate'];
$ShiftID=$_GET['shiftid'];
$PShift=$_GET['pshift'];
$TsID=$_GET['tsid'];
$DcType=$_GET['dctype'];
// below this some process is carried out
sec.php
<?php
ob_start();
include('first.php');
ob_end_clean();
echo $Logid;
echo $ShiftDate;
echo $ShiftID;
echo $PShift;
echo $TsID;
echo $DcType;
?>
The value is not displayed in second page..
Say how i can access the values in second page .
Pls help me
Thank u !!!
The best way to access data in PHP "generally" (except in small, insubstantial snippets) is through encapsulation. You could put those values into an object. Then, you will be able to access them on sec.php:
first.php:
<?php
class pageData {
public $Logid;
public $ShiftDate;
public $ShiftID;
public $PShift;
public $TsID;
public $DcType;
public function __construct() {
$this->Logid = $_GET['Logid'];
$this->ShiftDate = $_GET['shiftdate'];
$this->ShiftID = $_GET['shiftid'];
$this->PShift = $_GET['pshift'];
$this->TsID = $_GET['tsid'];
$this->DcType = $_GET['dctype'];
}
}
$pageData = new pageData();
?>
sec.php:
<?php
include('first.php');
echo $pageData->Logid;
// ...
echo $pageData->DcType;
?>
Remove ob_end_clean(); and see that will solve it.
ob_end_clean — Clean (erase) the output buffer and turn off output buffering
More
sec.php
<?php
error_reporting(E_ALL);
ini_set("display_errors", 1);
include("first.php");
?>
try above code and see if its return any error.
You are trying to pass the values which are set by GET in the page to the second page, am I right? How about trying to use sessions instead.
You can start a session and define values which will be stored as long as the browser is open and the session is still alive. So:
first.php
<?php
// Starting the session
session_start();
$_SESSION['Logid'] = $_GET['Logid'];
$_SESSION['ShiftDate'] = $_GET['shiftdate'];
$_SESSION['ShiftID'] = $_GET['shiftid'];
$_SESSION['PShift'] = $_GET['pshift'];
$_SESSION['TsID'] = $_GET['tsid'];
$_SESSION['DcType'] = $_GET['dctype'];
?>
sec.php
<?php
echo $_SESSION['Logid'];
echo $_SESSION['ShiftDate'];
echo $_SESSION['ShiftID'];
echo $_SESSION['PShift'];
echo $_SESSION['TsID'];
echo $_SESSION['DcType'];
?>
and use
session_unset();
session_destroy();
to kill the session and destroy the data in the global variable ($_SESSION). If you want to be extra cautious you can use:
session_unset();
session_destroy();
session_write_close();
setcookie(session_name(),'',0,'/');
session_regenerate_id(true);
to make sure everything is really destroyed. A bit of an overkill if you'd ask me but use if necessary.
Hope it helps!
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.