How do I output the current session data.
This is my code in the controller
$this->load->library('session'); //load library of session; encryption key = key
$data['user'] = $this->session->set_userdata('email', 'email#hp.com');
The value that should be outputed in the view is email#hp.com
I tried this in the view.php but it is not working.
foreach($user as $row){
$row->email;
}
but it doesnt work. it says invalid argument.
Try like this,
$this->session->set_userdata('email', 'email#hp.com'); // set the session
$data['user'] = $this->session->userdata('email'); // get the session and store it in an array which is passed to the view
and in view file echo the value,
echo $user;
Otherwise, you can directly get the session value in view file like,
$objCI =& get_instance(); //now CI object can be used
$objCI->session->userdata('email');
set_userdata() is used to set data in session,
to get session data you should use userdata()
like
$this->session->set_userdata('email', 'email#hp.com');//to set data
$data['user']=$this->session->userdata('email');//to get sessiondata
And in view echo $user; will do.
here
Related
Controller:logic part
$query1['shipping']=$this->cart_model->get_shipping_info();
$query2['pay']=$this->cart_model->get_payment_info();
$query1=$this->session->set_userdata($query1);
$query2=$this->session->set_userdata($query2);
Model:fetch data from database
public function get_shipping_info()
{
$query=$this->db->query('select * from shipping;');
return $query->result();
}
public function get_payment_info()
{
$query=$this->db->query('select * from payment;');
return $query->result();
}
View:here goes the view that i want to show to user
print_r($this->session->userdata('shipping'));
echo "<br/>";
echo "<br/>";
print_r($this->session->userdata('pay'));
Depends if you auto-load the session library or not, we will need to include session library:
$this->load->library('session');
Then you able to get session :
$session_id = $this->session->userdata('SessionID');
And still can't get then try to store array instead of stdClass Object.
Does this get you what you need?
You are setting session like this 'shipping' and 'pay'. But in your session there is no variable created for this.
$newdata = array(
'shipping' => 'shipping_123',
'pay' => 'pay_456'
);
$this->session->set_userdata($newdata);
and to retrive the session you can use
echo($this->session->userdata('shipping')); # Outputs shipping_123
echo($this->session->userdata('pay')); # Outputs pay_456
While looking your code, I think you need to pass data from controller to view. If true mention it too
Read this
Session Library - Codeigniter.com
I want to send data with a redirect function to another function within a controller here are my codes
1.
function index($id) {
redirect("/names/particular/", $id, 'refresh');
}
2.
function particular($id){
$fno=$this->uri->segment(3);
$this->load->model('names_rank');
$this->data["names"]=$this->names_rank->get_particular($id);
$this->load->view("view/details", $this->data);
}
I want to redirect with $id from function index in 1 to function particular within controller with function shown in 2..
I need help please
Why don't you better pass it through session or flash data.
//to set a flash data
$this->session->set_flashdata('id',$id);
redirect('/names/particular/') ;
//to access this flash data
$data = $this->session->flashdata('id');
But if you wish to send using redirect method then you can give this a try.
$id=1;
redirect(base_url()."/names/particular/".$id);
you can excess by any method as :
$id = $this->uri->segment(3);
You should put dot to concatinate with $id like this: redirect("names/particular/".$id, 'refresh');
And i'm not sure about 'refresh',if it belongs there at all!
I am using my own session class, in that class, I am using some protected data members and some public data methods While I storing some variable on my session class Like
Mage::getSingleton('decision/session')->storeProductInfo('2');
Here is function implementation, $this->_productId is the private data member of my session class.
Public function storeProductInfo($product_id){
$this->_productId = $product_id;
return $this;
}
I am getting the stored variable by calling the below statement, it return me "null".
$product_stored_id = Mage::getSingleton('decision/session')->getStoredProductInfo();
public function getStoredProductInfo(){
return $this->_productId;
}
Even
Mage:getSingleton('decision/session')->setData('product_id', '2');
Didn't working. Can you please let me know where I am going wrong? I have to store some arrays in my session that's why I created my own session class to separately deal with my logic.
Use Magento Magic Method get and set
For that when your observer will call then you can create the session and set the value of that.
you can set the session using set, getting value using get and unset session using uns.
Mage::getSingleton('core/session')->setMySessionVariable('MyValue');
$myValue = Mage::getSingleton('core/session')->getMySessionVariable();
echo $myValue;
To Unset the session
Mage::getSingleton('core/session')->unsMySessionVariable();
$inputMessage = 'Hello World';
Mage::getSingleton('core/session')->setWelcomeMessage($inputMessage);
Now you want to echo the "welcome message" somewhere else in your code/site.
$outputMessage = Mage::getSingleton('core/session')->getWelcomeMessage();
echo $this->__($outputMessage);
I want to ask this question if how can we make a session in code igniter specially in logging in and logging out on the account. I want to know the step by step following the MVC of code igniter.
At the login time after executing query set session data in set_userdata function and passing data array whos you want to set.
$this->session->set_userdata('session data here');
And at the time of logout you have to call unset_userdata function and passing array of array whos you have to set at login time.
$this->session->unset_userdata('session data here');
using my code as an example you can do this i have a controller called iris.php and a model called script.php. i use the iris to call and make use of the script model.
class Iris extends CI_Controller {
function __construct(){
parent::__construct();
$this->load->model('script');
$this->load->model('alert');
}
public function index()
{ $this->load->view('index');
}
public function login_in()
{
$login = $this->script->check_login();
if($login->num_rows() == 1){
foreach ($login->result_array() as $row) {
$newdata = array(
'fullname' => $row['fullname'],
'email' => $row['email'],
'member_id' => $row['member_id'],
'transtatus'=>$row['transtatus']
);
$this->session->set_userdata($newdata);
}
redirect('iris/user_home');
}else
{
$data = array('alert'=>$this->alert->log_alert());
$this->load->view('common/header');
$this->load->view('login',$data);
$this->load->view('common/footer');
}
}`
i first load the model script model under the constructor and in the login function of the iris controller i called the function in the script $login=$this->script->check_login();
in the script.php we have the following code.
{public function check_login(){
$email = $this->input->post('email');
$password = $this->input->post('password');
$query = "SELECT * FROM `iris_user`
WHERE`email`=? AND`password`= ? ";
$result = $this->db->query($query, array($email, $password));
return $result;
}
remember you have to have loader the session class helper form the application/config/autoload.php file in the CIfolder
$autoload['libraries'] = array('database', 'session');
the session is alway start once it has been autoloaded, but can be destroyed when maybe creating a logout function.
also note when adding to the session data variable to access the session variable you will have to use the name that was used when declaring the session variable. e.g to access the fullname you would do this in code
echo $_SESSION['fullname'];
In the controller load library session :
$this->load->library('session');
Use below sentence for session create :
$this->session->set_userdata("session_name",session_value);
For Session Unset:
$this->session->unset_userdata("session_name");
I defined a session class,which uses values from the $_POST variable in an array called $sessionVars. When a user logs-in a new instance of the session class is created, a construct function sets session variables.I checked that this is working correctly.Problem: When i try to access those variables from a different page the session shows that its not started and those variables are undefined. Confused cause I thought $_SESSION being a super global means its accessible all the time(i.e scope doesn't matter) . I suspect im doing something wrong when I try to access the $_SESSION variables since they are in a class. I appreciate any help..thanks in advance.
class userSession{
public function __construct($sessionVars){
session_start();
$_SESSION['userEmail']=$sessionVars['user'];
$_SESSION['userID']=$sessionVars['userID'];
$_SESSION['userFolder']='users/user_'.$_SESSION['userID'];
}
/*just for housekeeping. not used in application*/
function showvars(){
echo $_SESSION['userEmail'].'<br><br>';
echo $_SESSION['userID'].'<br><br>';
echo $_SESSION['userFolder'];
$sessionID=session_id();
echo '<br><br>'.$sessionID;
}
}//**END USER SESSION
/*This is the login script that calls the session*/
include 'library.php';
$show=new render;
$show->index();
if(!isset($_POST['login']) ){
$show->usrLogin();
} else{
if(!empty($_POST['email'])){
$postVars=array('user'=>$_POST['email'],'pass'=>$_POST['password']);
$user=new user();
$data=$user->loginUser($postVars);
$currSession=new userSession($data);
}else{
die('No data in POST variable');}
}
/*file upload that needs the session[userFolder] variable*/
function file_upload(){
$userFolder=&$_SESSION['userFolder'];
echo '<hr>userFolder is : '.$userFolder;
function do_upload(){
if(!empty( $_FILES) ){
echo $userFolder.'<hr>';
$tmpFldr=$_FILES['upFile']['tmp_name'];
$fileDest=$userFolder.'/'.$_FILES['upFile']['name'];
if(move_uploaded_file($tmpFldr,$fileDest)){
echo 'file(s) uploaded successfully';
}
else{
echo 'Your file failed to upload<br><br>';
}
return $fileDest; //returns path to uploaded file
} else{die( 'Nothing to upload');}
}//END FUNCTION DO_UPLOAD,
/*Perform upload return file location*/
$fileLoc=do_upload();
return $fileLoc;
}
You need to instantiate an object of this class on every page that uses the session (or start the session manually). Also, you'll not want that constructor, instead some other way of setting vars. This is just for illustration of how it works with your current code, there are much better approaches:
class userSession {
public function __construct(){
session_start();
}
function set_login_vars($sessionVars){
$_SESSION['userEmail']=$sessionVars['user'];
$_SESSION['userID']=$sessionVars['userID'];
$_SESSION['userFolder']='users/user_'.$_SESSION['userID'];
}
}
//page1.php
$session = new userSession;
$session->set_login_vars($loginVars);
//page2.php
//you need to start the session, either with the class
$session = new userSession;
//or session_start();
print_r($_SESSION);