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
Related
I have /signup/select-plan which lets the user select a plan, and /signup/tos which displays the terms of services. I want /signup/tos to be only accessible from /signup/select-plan. So if I try to go directly to /signup/tos without selecting a plan, I want it to not allow it. How do I go about this?
In the constructor, or the route (if you are not using contructors), you can check for the previous URL using the global helper url().
public function tos() {
if ( !request()->is('signup/tos') && url()->previous() != url('signup/select-plan') ) {
return redirect()->to('/'); //Send them somewhere else
}
}
In the controller of /signup/tos which returns the tos view just add the following code:
$referer = Request::referer();
// or
// $referer = Request::server('HTTP_REFERER');
if (strpos($referer,'signup/select-plan') !== false) {
//SHOW THE PAGE
}
else
{
dd("YOU ARE NOT ALLOWED")
}
What we are doing here is checking the HTTP referrer and allowing the page access only if user comes from select-plan
You are need of sessions in laravel. You can see the following docs to get more info: Laravel Sessions
First of all you need to configure till how much time you want to have the session variable so you can go to your directory config/sessions.php and you can edit the fields 'lifetime' => 120, also you can set expire_on_close by default it is being set to false.
Now you can have following routes:
Route::get('signup/select-plan', 'SignupController#selectPlan');
Route::post('signup/select-token', 'SignupController#selectToken');
Route::get('signup/tos', 'SignupController#tos');
Route::get('registered', 'SignupController#registered');
Now in your Signupcontroller you can have something like this:
public function selectPlan()
{
// return your views/form...
}
public function selectToken(Request $request)
{
$request->session()->put('select_plan_token', 'value');
return redirect('/signup/tos');
}
Now in signupController tos function you can always check the session value and manipulate the data accordingly
public function tos()
{
$value = $request->session()->get('select_plan_token');
// to your manipulation or show the view.
}
Now if the user is registered and you don't need the session value you can delete by following:
public function registered()
{
$request->session()->forget('select_plan_token');
// Return welcome screen or dashboard..
}
This method will delete the data from session. You can manipulate this. You won't be able to use in tos function as you are refreshing the page and you want data to persist. So its better to have it removed when the final step or the nextstep is carried out. Hope this helps.
Note: This is just the reference please go through the docs for more information and implement accordingly.
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");
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