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']
Related
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.
i have a problem calling a session variable from another script. can anybody help me on this matter.
Below is the script that i create the session and store the time in a session variable.
<?php
session_start();
$orgtimestamp = date("Y-m-d h:i:sa");
$_SESSION['orgtimestamp'] = $orgtimestamp;
?>
Here is the script that i try to access this session variable from a function of it. till now nothing worked
<?php
include '../../mydomain/myscript.php';
class survey_Tracksciprt
{
public static function timeofclick(){
session_start();
$time_org = $_SESSION['orgtimestamp'];
echo $time_org;
}
}
this hasnt worked upto now, nothing prints...can anybody give tips to sought this out and its compulsory to have this function timeofclick
You're not creating your class on your second file add:
//I don't know what this does but if it already starts a session remove the session start inside the class.
include '../../mydomain/myscript.php';
$survey = new survey_Tracksciprt();
$survey::timeofclick();
class survey_Tracksciprt
{
public static function timeofclick(){
session_start();
$time_org = $_SESSION['orgtimestamp'];
echo $time_org;
}
}
I also advice putting session_start at the top of your file.
<?php
session_start();
include '../../mydomain/myscript.php';
class survey_Tracksciprt
{
public static function timeofclick(){
$time_org = $_SESSION['orgtimestamp'];
echo $time_org;
}
}
Always use the session_start(); at the top line of the page.
First, you need an init-session.php file, containing:
if (session_status() == PHP_SESSION_NONE) {
session_start();
}
Second, you need to include this file at the start of your loader/layout (whatever you have there), so no operation will be executed before you initialize your session.
Third, you should initialize $orgtimestamp like this:
<?php
$orgtimestamp = date("Y-m-d h:i:sa");
$_SESSION['orgtimestamp'] = $orgtimestamp;
?>
Fourth, you need to call survey_Tracksciprt::timeofclick().
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 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';
What is the alternative to the deprecated function session_is_registered() in PHP 5?
Here's my code:
ob_start();
session_start();
if(!session_is_registered(myusername))
{
header("location:main_login.php");
}
ob_flush();
"You need to set and reference $_SESSION variables only." For example:
if( isset($_SESSION[$myusername]) )
From http://www.phpfreaks.com/forums/index.php?topic=263189.0
on a side note, best use $_SESSION['username'] = $myusername;. Using the $_SESSION[$myusername] as a variable may overwrite existing variables in the session.
But if you set anything in the session variable it will display you the protected page:
e.g.,
<?php
session_start();
if(isset($_SESSION[$myusername])){
echo "welcome to protected page.";
}
else {
header('location:login.php');
die;
}
?>
Use session_id()
if (!session_id()) {
// do stuff
}
if there is no session id nothing will be returned. (docs - session_id() )