Welcome Guest, Not a member yet? Register   Sign In
codeigniter 3.1.9 session failure
#1

am building a project and apparently my session works and sometimes stops working.when i post that data to the first controller and access it using a session it works but when i want to access it in other controllers and views it does not.
Reply
#2

(10-04-2019, 11:03 PM)chimwemwe Wrote: am building a project and apparently my session works and sometimes stops working.when i post that data to the first controller and access it using a session it works but when i want to access it in other controllers and views it does not.
Since you have not provided additional information in the form of a piece of code where you work with sessions or logs from writable/logs/.
I can assume that you initialized the session class in only one method in which it works for you, and in the one to which you refer to this initialization is not...
You must initialize the library in each controller method for it to work.
If you do not want to do this in each method then it is highly recommended to use models.
I would change this world, but God doesn't give me the source.
Reply
#3

(This post was last modified: 10-05-2019, 06:21 AM by ciadmin.)

(10-05-2019, 02:28 AM)Digital_Wolf Wrote:
(10-04-2019, 11:03 PM)chimwemwe Wrote: am building a project and apparently my session works and sometimes stops working.when i post that data to the first controller and access it using a session it works but when i want to access it in other controllers and views it does not.
Since you have not provided additional information in the form of a piece of code where you work with sessions or logs from writable/logs/.
I can assume that you initialized the session class in only one method in which it works for you, and in the one to which you refer to this initialization is not...
You must initialize the library in each controller method for it to work.
If you do not want to do this in each method then it is highly recommended to use models.
/this is my code the one sessions below i need to access across the app

Code:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Requestpage extends CI_Controller {
public function index()
{
parent::__construct();
$this->load->model('requestpage_model');
//getting all services types
$services_list=$this->requestpage_model->get_allservices();
$payments_list=$this->requestpage_model->get_allpaymenttypes();
$data['services_list']=$services_list;
$data['payments_list']=$payments_list;
$data['data'] = ($services_list);
//var_dump($services_list);
$this->load->view('requestpage',$data);
}

public function insert_cont()
{
//insert records into database
$this->load->model('requestpage_model');
$this->load->library('sms');
$this->load->library('email');

//getting the cureent date and time
$currentDateTime = date('Y-m-d H:i:s');
//populating the status of new entry
$new_entry="new pending action";

//we now getting information for the files to be saved
$file_name=$_FILES['id_attachment']['name'];  //file name
$file_type=$_FILES['id_attachment']['type'];  //file type
$file_size=$_FILES['id_attachment']['size'];  //file size
$file_tem_loc=$_FILES['id_attachment']['tmp_name']; //temporary location
$file_store="upload/".$file_name;

move_uploaded_file($file_tem_loc,$file_store);


$new=1;
$data=array(
"salutation"=>$this->input->post('salutation'),
"first_name" => $this->input->post('first_name'),
"last_name" => $this->input->post('last_name'),
"id_type" => $this->input->post('id_type'),
"id_number" => $this->input->post('id_number'),
"address" => $this->input->post('address'),
"contact_number" => $this->input->post('contact_number'),
"other_number" => $this->input->post('other_number'),
"email" => $this->input->post('email'),
"service_id" => $new,
"attachment" => $file_name,
"comment" => $this->input->post('comment')
);


$assigned_by="customer";
$assigned_to="zamtel";
$data1=array(

"assigned_to" => $assigned_to,
"assigned_by" => $assigned_by,
"date_entered" => $currentDateTime,
"date_updated" => $currentDateTime,
"payment_type" => $this->input->post('payment_options'),
"receipt_number" => $this->input->post(''),
"comment" =>$this->input->post('comment')
);


$data2 = $data;
$data3 = $data1;
// $this->load->view('packages',$data2);
session_start();
$_SESSION['customer_data1']=$data2;
$_SESSION['customer_data2']=$data3;


redirect('packages');

}
}

?>

**EDIT: code tags added for readability**
Reply
#4

(This post was last modified: 10-05-2019, 10:16 AM by Digital_Wolf. Edit Reason: No reason )

PHP Code:
<?php 
defined
('BASEPATH') OR exit('No direct script access allowed');

class 
Requestpage extends CI_Controller
{
    
// set public or private
    
public $data null;

    
//    Constructor
    
public function __construct() {
        
//    init construct
        
parent::__construct();
        
//    Pre-loading libs & helpers & models
        
$this->load->model('requestpage_model');
        
$this->load->library('sms');
        
$this->load->library('email');
    };

    
//    Base page
    
public function index() {
        
//getting all services types
        
$this->data = [
            
'services_list' => $this->requestpage_model->get_allservices(),
            
'payments_list' => $this->requestpage_model->get_allpaymenttypes(),
            
// 'data' => ($services_list) // I do not understand why you need the same data as in "services_list" , just delete it and refer to $data["services_list"]
        
];
        
//var_dump($services_list);
        
$this->load->view('requestpage'$this->data);
        
//    Clear memory...
        
unset($this->data);
    };

    
//    ###
    
public function insert_cont() {
        
//getting the cureent date and time
        
$currentDateTime date('Y-m-d H:i:s');
        
//populating the status of new entry
        
$new_entry "new pending action";
        
//we now getting information for the files to be saved
        
$this->data = [
            
//    ##############
            
['File_Info'] => [
                
//    file name
                
'name' => $_FILES['id_attachment']['name'],
                
//    file type
                
'type' => $_FILES['id_attachment']['type'],
                
//    file size
                
'size' $_FILES['id_attachment']['size'],
                
//    temporary location
                
'tem_loc' $_FILES['id_attachment']['tmp_name'],
                
//    path for saving uploads
                
'store' "upload/".$file_name
            
],
            
//    ##############
            
['Input_Info'] => [
                
"salutation" => $this->input->post('salutation'),
                
"first_name" => $this->input->post('first_name'),
                
"last_name" => $this->input->post('last_name'),
                
"id_type" => $this->input->post('id_type'),
                
"id_number" => $this->input->post('id_number'),
                
"address" => $this->input->post('address'),
                
"contact_number" => $this->input->post('contact_number'),
                
"other_number" => $this->input->post('other_number'),
                
"email" => $this->input->post('email'),
                
"service_id" => $new,
                
"attachment" => $_FILES['id_attachment']['name'],
                
"comment" => $this->input->post('comment')
            ],
            
//     ##############
            
['Assigned'] => [
                
"assigned_to" => "zamtel",
                
"assigned_by" => "customer",
                
"date_entered" => $currentDateTime,
                
"date_updated" => $currentDateTime,
                
"payment_type" => $this->input->post('payment_options'),
                
"receipt_number" => $this->input->post(''),
                
"comment" => $this->input->post('comment')
            ]
        ];
        
//    Moving uploads
        
move_uploaded_file($this->data['File_Info']['tem_loc'], $this->data['File_Info']['store']);
        
$new 1;
        
// $this->load->view('packages', $this->data['your_key']);
        
session_start();

        
//    CHANGE THIS SESSION DATA ON U PARAMETERS WITH USING $THIS->DATA->[KEY]
        
$_SESSION = [
            [
'customer_data1'] => $this->data['key1'],
            [
'customer_data2'] => $this->data['key2']
        ];
        
redirect('packages');
        
//    Clear memory...
        
unset($this->data);
    };
}
?>

I'm not much of a codebreaker, and it's a little annoying, but for you, I made a huge exception... There is one thing, but correct a little code at the end of the method "insert_cont ()", so that the data was saved correctly !

UPD: Working with a database or sessions in controllers according to the PHP (H)MVC standard is not good, for good practice I recommend to study the model system and work in them, and transfer ready - made data to the controller on request.

P.S. Sorry my English  Angel
I would change this world, but God doesn't give me the source.
Reply
#5

(10-05-2019, 10:07 AM)Digital_Wolf Wrote:
PHP Code:
<?php 
defined
('BASEPATH') OR exit('No direct script access allowed');

class 
Requestpage extends CI_Controller
{
    
// set public or private
    
public $data null;

    
//    Constructor
    
public function __construct() {
        
//    init construct
        
parent::__construct();
        
//    Pre-loading libs & helpers & models
        
$this->load->model('requestpage_model');
        
$this->load->library('sms');
        
$this->load->library('email');
    };

    
//    Base page
    
public function index() {
        
//getting all services types
        
$this->data = [
            
'services_list' => $this->requestpage_model->get_allservices(),
            
'payments_list' => $this->requestpage_model->get_allpaymenttypes(),
            
// 'data' => ($services_list) // I do not understand why you need the same data as in "services_list" , just delete it and refer to $data["services_list"]
        
];
        
//var_dump($services_list);
        
$this->load->view('requestpage'$this->data);
        
//    Clear memory...
        
unset($this->data);
    };

    
//    ###
    
public function insert_cont() {
        
//getting the cureent date and time
        
$currentDateTime date('Y-m-d H:i:s');
        
//populating the status of new entry
        
$new_entry "new pending action";
        
//we now getting information for the files to be saved
        
$this->data = [
            
//    ##############
            
['File_Info'] => [
                
//    file name
                
'name' => $_FILES['id_attachment']['name'],
                
//    file type
                
'type' => $_FILES['id_attachment']['type'],
                
//    file size
                
'size' $_FILES['id_attachment']['size'],
                
//    temporary location
                
'tem_loc' $_FILES['id_attachment']['tmp_name'],
                
//    path for saving uploads
                
'store' "upload/".$file_name
            
],
            
//    ##############
            
['Input_Info'] => [
                
"salutation" => $this->input->post('salutation'),
                
"first_name" => $this->input->post('first_name'),
                
"last_name" => $this->input->post('last_name'),
                
"id_type" => $this->input->post('id_type'),
                
"id_number" => $this->input->post('id_number'),
                
"address" => $this->input->post('address'),
                
"contact_number" => $this->input->post('contact_number'),
                
"other_number" => $this->input->post('other_number'),
                
"email" => $this->input->post('email'),
                
"service_id" => $new,
                
"attachment" => $_FILES['id_attachment']['name'],
                
"comment" => $this->input->post('comment')
            ],
            
//     ##############
            
['Assigned'] => [
                
"assigned_to" => "zamtel",
                
"assigned_by" => "customer",
                
"date_entered" => $currentDateTime,
                
"date_updated" => $currentDateTime,
                
"payment_type" => $this->input->post('payment_options'),
                
"receipt_number" => $this->input->post(''),
                
"comment" => $this->input->post('comment')
            ]
        ];
        
//    Moving uploads
        
move_uploaded_file($this->data['File_Info']['tem_loc'], $this->data['File_Info']['store']);
        
$new 1;
        
// $this->load->view('packages', $this->data['your_key']);
        
session_start();

        
//    CHANGE THIS SESSION DATA ON U PARAMETERS WITH USING $THIS->DATA->[KEY]
        
$_SESSION = [
            [
'customer_data1'] => $this->data['key1'],
            [
'customer_data2'] => $this->data['key2']
        ];
        
redirect('packages');
        
//    Clear memory...
        
unset($this->data);
    };
}
?>

I'm not much of a codebreaker, and it's a little annoying, but for you, I made a huge exception... There is one thing, but correct a little code at the end of the method "insert_cont ()", so that the data was saved correctly !

UPD: Working with a database or sessions in controllers according to the PHP (H)MVC standard is not good, for good practice I recommend to study the model system and work in them, and transfer ready - made data to the controller on request.

P.S. Sorry my English  Angel
Reply
#6

that methods seems not to work either, the thing is when i var dump my session right innthe requestpage controller i am able to get the results but cant be accessed in the other controllers.




thats my response when i var dump the following,

var_dump($_SESSION['customer_data1']);
var_dump($_SESSION['customer_data2']);
die();




array(12) { ["salutation"]=> string(2) "Dr" ["first_name"]=> string(9) "chimwemwe" ["last_name"]=> string(6) "moonga" ["id_type"]=> string(15) "drivers license" ["id_number"]=> string(2) "23" ["address"]=> string(11) "lusaka west" ["contact_number"]=> string(10) "0955203166" ["other_number"]=> string(10) "0950003968" ["email"]=> string(25) "[email protected]" ["service_id"]=> int(1) ["attachment"]=> string(5) "1.PNG" ["comment"]=> string(5) "hello" } array(7) { ["assigned_to"]=> string(6) "zamtel" ["assigned_by"]=> string(8) "customer" ["date_entered"]=> string(19) "2019-10-05 19:44:42" ["date_updated"]=> string(19) "2019-10-05 19:44:42" ["payment_type"]=> NULL ["receipt_number"]=> NULL ["comment"]=> string(5) "hello" }
Reply
#7

(This post was last modified: 10-05-2019, 11:30 AM by Digital_Wolf.)

You should use the session library, and load it into the __construct method there will be an automatic start of sessions, according to the documentation, the start of sessions should always be at the beginning of all code and before that there should be no other code output...

If you were working on CI4, I would send you an example of the finished code. As I said in the beginning, I'm too lazy to dig into someone else's code and read the documentation for CI3, sorry !
I would change this world, but God doesn't give me the source.
Reply
#8

(10-05-2019, 11:25 AM)Digital_Wolf Wrote: You should use the session library, and load it into the __construct method there will be an automatic start of sessions, according to the documentation, the start of sessions should always be at the beginning of all code and before that there should be no other code output...

If you were working on CI4, I would send you an example of the finished code. As I said in the beginning, I'm too lazy to dig into someone else's code and read the documentation for CI3, sorry !
okayy its fine thanks for the heads up
Reply
#9

I would use CodeIgniters ./application/config/autoload.php

And autoload the session library.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply




Theme © iAndrew 2016 - Forum software by © MyBB