I have a controller/controller.php file that starts a session and sets some session variables. I then 'include' a model/model.php file and call a function to load it.
When I try and use the session variables from the controller.php file in the model.php file, I am getting many: Undefined variable: _SESSION errors.
Here is an example of setting a session variable in the controller.php file:
$_SESSION['userName'] = 'some data'
In the model.php file I am trying to retrieve it by this code:
print($_SESSION['userName']);
What am I doing wrong?
EDIT
my controller.php file code:
<?PHP
session_start();
if (isset($_POST['Login'])) {
$_SESSION['pointsForQuestion'] = '1';
$_SESSION['userName'] = $_POST['txtUsername'];
if(!is_file($_SESSION['userName'].".txt"))
{
writeUserDetails($_SESSION['userName'],"1","0");
$_SESSION['currentLevel'] = "1";
$_SESSION['score'] = "0";
} else {
readUserDetails($_SESSION['userName']);
}
print('logged in');
include('../model/model.php');
print page_load();
}
function writeUserDetails($username,$level,$score) {
$fh = fopen($username.".txt", 'w') or die("can't open file");
fwrite($fh, $level.",".$score);
fclose($fh);
}
function readUserDetails($username) {
$userDetails = explode(',', file_get_contents($username.".txt"));
$_SESSION['currentLevel'] = $userDetails[0];
$_SESSION['score'] = $userDetails[1];
}
?>
Start your session before defining the session variables on top ie session_start();
Edited
You have not set anything for these session variable that's why it is giving that error
$_SESSION['userName'];
$_SESSION['currentLevel'];
$_SESSION['score'];
You can delete these session variables if u dont want to set anything...
$_SESSION is a superglobal available in all scopes.
So, you must have forgotten session_start().
Related
I was having issues with the session file being locked, so I added session_write_close() once I was done with the session. The script worked properly before that, however, once I leave the sign-in page now, the session is blank.
Session is started at the top of index.php which includes the sign in page:
$result = 'token_valid';
$_SESSION['user'] = $email;
print_r($_SESSION);
session_write_close();
print_r($_SESSION);
The session data is returned properly both times on the sign-in page.
Array ( [user] => abc#gmail.com ) Array ( [user] => abc#gmail.com )
A link returns to the home page, which calls a function to check if logged in...
function user_is_signed_in() {
print_r($_SESSION);
session_write_close();
if($user == '') {
return False;
}
else {
return True;
}
}
The session no longer has any data.
Full index.php
<?php
session_start();
include_once('fnc/database.php');
include_once('fnc/user.php');
if(!user_is_signed_in()) {
include('sign-in.php');
}
else {
$url = parse_url($_SERVER['REQUEST_URI']);
if(!$url['query'])
{
include('home.php');
}
else {
if(isset($_GET['media']))
{
include($_GET['media'].'.php');
}
if(isset($_GET['user']))
{
include($_GET['user'].'.php');
}
}
}
.
.
Workaround (probably filthy)
Issue seems to be caused by the reading/writing of the actual session file. Used the session_id generated by PHP and just created a secondary session file. Do not save in same folder (or if you do, change the filename) - session_start seems to delete and regenerate the session file PHP manages and you'll lose any data written there.
session_start();
$sess = array();
$sess = $_SESSION;
$sess["id"] = session_id();
//print_r($sess);
session_write_close();
Create session_data in session folder
$session_details = "user|".$email;
$session_file = "/Programs/XAMPP/tmp/session_data/sess_".$sess["id"];
//echo $session_details;
$fh = fopen($session_file, 'w+');
fwrite($fh, $session_details);
fclose($fh);
Read session data from this file instead of the session
$session_path = "/Programs/XAMPP/tmp/session_data/sess_".$sess["id"];
$fh = fopen($session_path, 'r');
$session_file = fread($fh, filesize($session_path));
$exploded_session = explode("\n", $session_file);
$session_data = array();
foreach($exploded_session as $line)
{
$tmp = explode("|", $line);
$session_data[$tmp[0]] = $tmp[1];
}
return $session_data["user"];
fclose($fh);
I have three files - global.php, test.php, test1.php
Global.php
$filename;
$filename = "test";
test.php
$filename = "myfile.jpg";
echo $filename;
test1.php
echo $filename;
I can read this variable from both test and test1 files by
include 'global.php';
Now i want to set the value of $filename in test.php and the same value i want to read in test1.php.
I tried with session variables as well but due to two different files i am not able to capture the variable.
How to achieve this........
Thanks for help in advance.....
Use:
global.php
<?php
if(!session_id()) session_start();
$filename = "test";
if(!isset($_SESSION['filename'])) {
$_SESSION['filename'] = $filename;
}
?>
test.php
<?php
if(!session_id()) session_start();
//include("global.php");
$_SESSION['filename'] = "new value";
?>
test1.php
<?php
if(!session_id()) session_start();
$filename = $_SESSION['filename'];
echo $filename; //output new value
?>
I think the best way to understand this is as following:
You have one file index.php whit some variable defined.
$indexVar = 'sometext';
This variable is visible on all index.php file. But like a function, if you include other files, the variable will not be visible unless you specify that this variable has scope global.
You should redefine the scope of this variable in your new file, like this:
global $indexVar;
Then you will be able to acess directly by calling it as you were in your main file. You could use an "echo" in both files, index.php and any other file.
First you start session at the top of the page.
Assign your variable into your session.
Check this and Try it your self
test.php
<?php
session_start(); // session start
include("global.php");
$filename = "myfile.jpg";
$_SESSION['samplename']=$filename ; // Session Set
?>
test1.php
<?php
session_start(); // session start
$getvalue = $_SESSION['samplename']; // session get
echo $getvalue;
?>
How to pass two value from one page to another in PHP using session.
$account=$_SESSION["account_no"];
$account1=$_SESSION["account_no"];
Session will be available through out the application (in all pages) until you destroy it.
To set a session,
<?php
session_start();
$_SESSION['variable_name_1'] = "value_1"; // or $_POST['accountno_1'];
$_SESSION['variable_name_2'] = "value_2"; // or $_POST['accountno_2'];
?>
In the other page, to get the values
<?php
session_start();
echo $_SESSION['variable_name_1'];
echo $_SESSION['variable_name_2'];
?>
FILE-1: WHERE YOU NEED TO SAVE THE ACCOUNT TO SESSION
<?php // NOTICE THAT THERE IS NO SPACE BEFORE <?php [THIS IS IMPORTANT!!!]
// FILE-NAME: file_1.php WHERE YOU HAVE TO SET THE SESSION VARIABLE
//FIRST CHECK IF SESSION EXIST BEFORE STARTING IT:
if (session_status() == PHP_SESSION_NONE || session_id() == '') {
session_start();
}
$_SESSION["account_no"] = $account;
FILE-2: WHERE YOU NEED TO GET THE ACCOUNT FROM SESSION
<?php // NOTICE THAT THERE IS NO SPACE BEFORE <?php [THIS IS IMPORTANT!!!]
// FILE-NAME: file_2.php WHERE YOU NEED TO READ THE SESSION VARIABLE
//FIRST CHECK IF SESSION EXIST BEFORE STARTING IT:
if (session_status() == PHP_SESSION_NONE || session_id() == '') {
session_start();
}
// READ THE ACCOUNT NUMBER FROM SESSION DATA...
$account = $_SESSION["account_no"];
On the first page:
session_start();
$_SESSION['value1'] = 'First value';
$_SESSION['value2'] = 'Second value';
On the second page:
session_start();
$value1 = $_SESSION['value1'];
$value2 = $_SESSION['value2'];
File:1 where data will be store Session
<?php
session_start(); //before HTML tag
$_SESSION['one'] = $account_no1;
$_SESSION['two'] = $account_no2;
?>
File2: where you like to retrieve session
<?php
session_start();
echo $_SESSION['one'];
echo $_SESSION['two'];
?>
Sessions is a global variable in PHP.
Just create two session variables as use anywhere
<?php
session_start(); // should be at top of page or before any output to browser
$_SESSION['account'] = $account;
$_SESSION['account1'] = $account1;
Now access these session variables anywhere in any page but should start session before use, like:
<?php
session_start();
echo $_SESSION['account'];
I am starting a session in controller but i am unable to call that session variable in view.
When i am working on localhost its working perfectly but on server there is nothing in that session variable. I am writting my code:=
Controller
if($_REQUEST['username'] == $result['users'][$i]->username && $_REQUEST['pass'] == $result['users'][$i]->password)
{
session_start();
$_SESSION['username'] = $result['users'][$i]->username;
$_SESSION['profilename'] = $result['users'][$i]->profilename;
die;
$_SESSION['password'] = $result['users'][$i]->password;
$_SESSION['id'] = $result['users'][$i]->ID;
echo $_SESSION['id'];
die;
print_r($result['users']);
$url=strtok($_SERVER["REQUEST_URI"],'?');
redirect("$referal?user=profile");
echo "<script>location.href='$referal?user=profile'</script>";
return true;
die;
}
View :
if(isset($_SESSION['username']) && $_GET['action']!='logout'){
?>
<script>alert("hii");</script>
}
First use the built in SESSIOn library for codeigniter. But I had an issue where the session variable didn't get saved/read. Turned out to be a small issue with itself (some session fixation issue). https://github.com/EllisLab/CodeIgniter/wiki/Native-session sorts it out for you
I have three files - global.php, test.php, test1.php
Global.php
$filename;
$filename = "test";
test.php
$filename = "myfile.jpg";
echo $filename;
test1.php
echo $filename;
I can read this variable from both test and test1 files by
include 'global.php';
Now i want to set the value of $filename in test.php and the same value i want to read in test1.php.
I tried with session variables as well but due to two different files i am not able to capture the variable.
How to achieve this........
Thanks for help in advance.....
Use:
global.php
<?php
if(!session_id()) session_start();
$filename = "test";
if(!isset($_SESSION['filename'])) {
$_SESSION['filename'] = $filename;
}
?>
test.php
<?php
if(!session_id()) session_start();
//include("global.php");
$_SESSION['filename'] = "new value";
?>
test1.php
<?php
if(!session_id()) session_start();
$filename = $_SESSION['filename'];
echo $filename; //output new value
?>
I think the best way to understand this is as following:
You have one file index.php whit some variable defined.
$indexVar = 'sometext';
This variable is visible on all index.php file. But like a function, if you include other files, the variable will not be visible unless you specify that this variable has scope global.
You should redefine the scope of this variable in your new file, like this:
global $indexVar;
Then you will be able to acess directly by calling it as you were in your main file. You could use an "echo" in both files, index.php and any other file.
First you start session at the top of the page.
Assign your variable into your session.
Check this and Try it your self
test.php
<?php
session_start(); // session start
include("global.php");
$filename = "myfile.jpg";
$_SESSION['samplename']=$filename ; // Session Set
?>
test1.php
<?php
session_start(); // session start
$getvalue = $_SESSION['samplename']; // session get
echo $getvalue;
?>