I have MY_Controller Extends CI_Controller
I have A Controller Extends MY_Controller and B Controller Extends MY_Controller
When I set session set_userdata in MY_COntroller in can be access in A Controller and also B Controller
But when set session set_userdata in A_Controller it can't be access in MY_Controller and B Controller
But after set session in a controller and print all userdata, the session is exist but not exist in other controller (B & MY COntroller)
IVE set the autoload libraries and also the config
$config['sess_driver'] = 'files';
$config['sess_cookie_name'] = 'ci_session';
$config['sess_expiration'] = 1800;
$config['sess_save_path'] = sys_get_temp_dir();
$config['sess_match_ip'] = FALSE;
$config['sess_time_to_update'] = 300;
$config['sess_regenerate_destroy'] = FALSE;
My COntroller extends CI CONtroller
function __construct()
{
parent::__construct();
$this->readPath();
$this->readApiConfig();
$this->load->library('session');
$this->load->library('user_agent');
}
Login Controller extends My Controller
function __Construct()
{
parent::__Construct();
$this->load->library('encrypt');
$this->load->library('form_validation');
$this->load->helper('captcha');
$this->load->library('session');
$this->load->helper('cookie');
date_default_timezone_set("Asia/Bangkok");
}
function login(){
$email = array(
'usr_email' => $this->input->post("email")
);
$this->session->set_userdata('logged_in', $email);
redirect('landing');
}
Landing Controller extends My COntroller
function __construct()
{
parent::__construct();
$this->load->library('encrypt');
$this->load->library('session');
}
function index() {
//$prod = $this->getExistingAccount();
print_r($this->session->userdata('logged_in'));die();
}
AND ALSO I TRY to ses sess_save_path to APPPATH."CI_SESSION";
and add folder inside application folder name CI_SESSION and set the permission to 0700 but show white blank page
Web url : 192.xxx.x.x : port
Related
I don't know if the title above is an correct title about my question.
Is there a way to declare a variable which we can access it from anywhere in view without need to redefine it again in each function in controller?
for example in controller file Students.php contains many function that handle the views, take a look below :
public function __construct() {
parent::__construct();
$data['count_student'] = $this->m_data->allStudent(); // count all students
}
public function index() {
$data['content'] = 'view-students';
$this->load->view('frontend/header' , $data);
}
public function showDetails() {
$data['content'] = 'view-detail-students';
$this->load->view('frontend/header' , $data);
}
I expected we can access $count_student in both view-students.php and view-detail-student.php without to define $data['count_student'] = $this->m_data->allStudent(); on each function that handle the view.
Is there possible ways to do that?
In the application/core/ create MY_Controller.php
<?php
class MY_Controller extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->model('m_data');
$this->data['count_student'] = $this->m_data->allStudent();
}
}
Controller use $this->data
class Students extends MY_Controller {
public function __construct() {
parent::__construct();
}
public function index() {
$this->data['content'] = 'view-students';
$this->load->view('frontend/header', $this->data);
}
public function showDetails() {
$this->data['content'] = 'view-detail-students';
$this->load->view('frontend/header', $this->data);
}
}
Now that you have extended the controller you shoudld be able to just go like
<?php echo $count_student;?>
On the view
I noticed that you can access variables in views without passing them if you declare them in the controller with $this->. Probably because they default to public visibility.
public function __construct() {
parent::__construct();
// $data['count_student'] = $this->m_data->allStudent(); // count all students
//
$this->count_all_the_students = $this->m_data->allStudents();
}
public function index() {
$data['content'] = 'view-students';
$this->load->view('frontend/header' , $data);
}
And then in the view you can use $this->count_all_the_students without putting it in the $data array.
<?php defined('BASEPATH') OR exit('No direct script access allowed'); ?>
<?php
echo 'I have misled ' . $this->count_all_the_students . ' students with my MVC breaking suggestions.';
If you are using the same variable in every controller function then you have to declare it as follows
class Example extends CI_Controller{
protected $data = array();
public function __construct() {
parent::__construct();
$this->data['count_student'] = $this->m_data->allStudent();
}
public function index() {
$this->data['content'] = 'view-students'; //variables are same
$this->load->view('frontend/header' , $this->data);
}
public function showDetails() {
$this->data['content'] = 'view-detail-students'; //variables are same
$this->load->view('frontend/header' , $this->data);
}
}
This applies only if you have common variables for all views in this controller.
If you are using different variables then use the following code.
class Example extends CI_Controller{
protected $count_student= "";
public function __construct() {
parent::__construct();
$this->count_student = $this->m_data->allStudent();
}
public function index() {
$data['content'] = 'view-students'; //Variable is different
$data['count_student'] = $this->count_students;
$this->load->view('frontend/header' , $data);
}
public function showDetails() {
$data['details'] = 'view-detail-students'; // Variable is different
$data['count_student'] = $this->count_students;
$this->load->view('frontend/header' , $data);
}
}
I cant access to my login controller which is located in the sub directory login.
it is working perfectly in my local server.
in my default controller I did this
public function __construct()
{
parent::__construct();
$this->load->helper('url');
}
public function index()
{
redirect("login");
}
And in my routes.php
$route['login'] = 'login/log';
Can anyone help me out please?
Try this:
IN YOU CONFIG.PHP
$config['base_url'] = '';
$config['index_page'] = '';
$config['uri_protocol'] = 'REQUEST_URI';
I am new to CodeIgniter framework. I am using 2.1.4 version. I designed a simple login form, with a javascript validation, and the home page of a site. Can you please help me to understand how to declare session , and how to destroy the session on clicking signout link.
controller file of login page ( to load the view page login.php ):-
class Login extends CI_Controller {
function __construct() {
parent::__construct();
$this->load->helper('url');
}
function index(){
$this->load->view('login');
}
function success() {
redirect ('home');
}
}
The controller file home.php for the view home.php
class Home extends CI_Controller {
// local constructor will be overriding the one in the parent controller class
// for using a constructor in any of my Controllers
function __construct() {
parent::__construct();
}
public function index()
{
$this->load->view('home');
}
}
I have designed the view page home.php, and gave the signout link:-
<div class="logout">Signout</div>
For initializing the session, i need to know, what all constructor changes/ config changes need, and the method of session destoy.
To start session library, Go to application/config/config.php and change the below line:
$autoload['libraries'] = array('session');
It would be better if you start your session in the autoload.php. To destroy session you would use :
$this->session->sess_destroy();
To set session :
$newdata = array(
'username' => 'johndoe',
'email' => 'johndoe#some-site.com',
'logged_in' => TRUE
);
$this->session->set_userdata($newdata);
here is an controller... first of all u need to declare a session so that you have two choice to declare one is Go to application/config/config.php change the code as
$autoload['libraries'] = array('session');
and follow this following method (controller)
class Login extends CI_Controller{
function __construct(){
parent::__construct();
$this->load->library('session');
}
function index(){
$this->load->view('login');
}
function success() {
$user=$this->input->post('user');
$psw=$this->input->post('pswd');
$this->load->model('validation');
$result=$this->validation->useraccess($user,$psw);
if($result)
{
$this->session->set_userdata('username', $user); //setting session
redirect ('home');
}
else
{
$this->index();
}
}
function logout()
{
$this->session->unset_userdata('username');
redirect('login','refresh');
}
}
this is model where validation done
Class Validation extends CI_Model{
function __construct(){
parent::__construct();
}
function useraccess($user,$pswd)
{
$query = $this->db->query("select * from user where username='$user' AND password='$pswd'");
foreach ($query->result_array() as $row)
{
if($row['username']==$user AND $row['password']==$pswd)
{
return true;
}
else
{
return false;
}
}
}
}
here is a view
login page
create 2 text box and 1 submit button and declare form action as
localhost/index.php/login/success
for logut
localhost/index.php/login/logout
I have following
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Hello extends CI_Controller {
var $name = 'test';
function index() {
$this->name = 'Andy';
$data['name'] = $this->name;
$this->load->view('you_view', $data); // THIS WORKS
}
function you() {
$data['name'] = $this->name;
$this->load->view('you_view', $data); // BUT THIS DOESN'T WORK
}
}
My question is how to I pass the $this->name = 'Andy'; to you() ??
Since it is being set in a different method of the controller, which equates to another request in your code, you will need to store it in a session variable to have it persist across page requests.
function index() {
$this->name = 'Andy';
$data['name'] = $this->name;
$this->session->set_userdata('name', $this->name);
$this->load->view('you_view', $data); // THIS WORKS
}
function you() {
$data['name'] = $this->session->userdata('name');
$this->load->view('you_view', $data); // BUT THIS DOESN'T WORK
}
If its a value that is part of the class you can put it in the constructor
class Hello extends CI_Controller {
public function __construct() {
parent::__construct();
// will be available to any method in the class
$this->name = 'andy';
}
I am using Tank_auth for user authentication in codeigniter. To check if any user is logged in or not, if he is then to get the username and id I need to use the following code in every functions of my controllers, which is very irritating.
// If any user is logged in get his/her userid or name
if ($this->tank_auth->is_logged_in()) {
$data['user_id'] = $this->tank_auth->get_user_id();
$data['username'] = $this->tank_auth->get_username();
}
So , I was wondering if I could make life easier by putting the following code inside the function __construct() like following,
but its not working.
Could you please tell me how to get it work?
Thanks in Advance :)
function __construct()
{
parent::__construct();
$this->load->helper('url');
$this->load->library('tank_auth');
if ($this->tank_auth->is_logged_in()) {
$data['user_id'] = $this->tank_auth->get_user_id();
$data['username'] = $this->tank_auth->get_username();
}
}
if you plan to use this array only inside this controller, you can use it like this:
//outside the functions define the $data as controller class property:
private $data = array();
function __construct()
{
parent::__construct();
$this->load->helper('url');
$this->load->library('tank_auth');
if ($this->tank_auth->is_logged_in()) {
$this->data['user_id'] = $this->tank_auth->get_user_id();
$this->data['username'] = $this->tank_auth->get_username();
}
}
//and then use it like
function index(){
$this->load->view("something.php",$this->data);
}