I am getting this very strange error "Cannot use a scalar value as an array"
neither google god nor stuffs on SOF helped me this what i am trying to do is i have already created user session[with session data username and password] now after user login on my_profile page i am appending some session data like user_id then i start getting this error??? Any help or Hints
//controller
<?php
class My_profile extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->library('session');
$this->load->helper('form');
$this->load->helper('url');
$this->load->helper('html');
$this->load->database();
$this->load->library('form_validation');
// $this->is_login();
}
public function index () {
if($this->session->userdata('is_login')) {
$session_data = $this->session->userdata('is_login');
$session_data['user_id'] = $this->session->userdata('user_id');
$this->session->set_userdata("is_login", $session_data);
$this->load->model('My_profilee');
$data['query']=$this->My_profilee->view_data();
$this->load->helper('url');
$this->load->view('header2');
$this->load->view('my_profile');
// $this->load->view('footer');
}else {
echo "you don't have permission to access this page <a href=../Homecontroller/index/>Login</a>";
die();
}
}
}
?>
//model
<?php
class My_profilee extends CI_Model {
function __construct()
{
// Call the Model constructor
parent::__construct();
$this->load->database();
}
public function view_data() {
//echo $this->session->userdata('user_id');
$query=$this->db->get('tbl_usrs');
return $query->result();
}
}
?>
what i am really trying to do is to add user id [from databse] of user after login into session data and then display user information according to user id??
This is pretty straight forward.
You have an assignment wherein a single value, be it an integer or string, is assigned to a variable:
$session_data = $this->session->userdata('is_login');
This is now a scalar value. it is unmutable unless redefined in context. You cannot apply an array value to an existing scalar value, you must instantiate a new array, and apply the value as an index, then add the new item as an index to the new array.
$session_array['session_data'] = $this->session->userdata('is_login');
And continue assigning variables to this array and so forth.
Related
I have controller in codeigniter that name is Product. When I access product controller, I am passing cateid variable in URL as query string parameter and getting on product controller.
I have created two methods in controller with names index and productajax . When I click on product link then controller index method executes. I need to use cateid variable value from URL query string in productajax method when page is initially loaded or controller index method being called.
class Product
{
public function index()
{
$cateid= $this->input->get('cateid'):
$this->load->view('product'):
}
public function productajax()
{
// I need to get cateid from index function
}
}
Hope this will help you :
You can do it by creating a public or private variable $cate_id in class and when index loads then
assign $cate_id variable in your index method and when you access productajax you can get cat id in productajax by using $this->cat_id
Your controller should be like this :
// Product class
class Product extends CI_Controller
{
public $cate_id;
public function __construct()
{
parent::__construct();
$this->load->helper('url');
}
public function index()
{
$cateid = $this->input->get('cateid'):
$this->cate_id = $cateid;
$this->load->view('produxt');
}
public function productajax()
{
echo $this->cate_id;
}
}
For more : http://php.net/manual/en/language.oop5.php
I am trying to redirect to controller index if not authorized the access to other functions within same controller. According to my coding it is looking like infinite loop. Please help me to do that.
class Customer_Dashboard extends CI_Controller {
public function __construct() {
$method= $this->router->fetch_method();
if ($this->session->userdata("cus_sel_comp")) {
}else{
if($method !="index"){
redirect(base_url()."customer_dashboard");exit;
}
}
}
public function index() {
// Here do some operations and let the user to select company and update the "cus_sel_comp" session variable. After set that session user can access the other controller functions.
}
public function other_function1() {
}
public function other_function2() {
}
}
My coding is as above. I need to do this using same controller. Problem is if that session not set there is a infinite loop.
Instead of redirecting return index function. See the code below
if($method !="index"){
return $this->index();
}
You are calling the same function and redirecting it to same method.
class Customer_Dashboard extends CI_Controller {
public function __construct() {
$method= $this->router->fetch_method();
if ($this->session->userdata("cus_sel_comp")) {
}else{
if($method !="index"){
redirect(base_url()."Customer_Dashboard/index"); // Redirect it to index if other method is invoked.
}
}
}
public function index() {
// Here do some operations and let the user to select company and update the "cus_sel_comp" session variable. After set that session user can access the other controller functions.
}
public function other_function1() {
}
public function other_function2() {
}
}
Also dont use base_url() instead of that define an path in config
base_url() has many other entries present which are un-necessarily called.
I need to resolve a doubt, I leave the details.
I have a class that has multiple related queries to the database with user data, to access these methods need to verify that the user is logged in, and I do it using php initializer "__construct ()" methods, specify there if the user logged on.
<?php
class User()
{
public function __construct() {
if ( !isset($_SESSION['user']) ) {
$data = array(
'response' => false,
'message' => 'You must login to access this page'.
);
echo json_encode($data);
}
}
public function index() {
// The user can access if you are logged
}
public function edit_profile() {
// The user can not access if you have not logged
}
public function save_profile_data() {
// The user can not access if you have not logged
}
}
?>
My questions:
Use the __construct() is a good optimal choice resource-intensive?
The __construct() is safe to use and prevent the user to access other methods that have not specified whether there is coded session variable.
Ie if a user calls the edit_profile() method, and this method does not have the code to check for the session, but I have specified in the __construct(), the user can access this method?
I hope you can help me, I would greatly appreciate.
I suggest you to create your own library file in library folder
Here is the class file
class Authenticate {
var $table;
public function __construct()
{
$this->ci =& get_instance();
}
public function is_logged_in()
{
$sessionid = $this->ci->session->userdata('moderId');
if($sessionid)
{
return isset($sessionid);
}
else if(!$sessionid) {
redirect(base_url() . 'moderator');
}
}
}
And in your controller,use this function.if you put this function in the constructor of the controller,then it wil be available to all methods
Controller
class B2bcategory extends CI_Controller {
function __construct() {
parent::__construct();
$this->load->model('moderator/b2bcategory_model');
$this->authenticate->is_logged_in();
}
}
Suppose i have a controller called home like this
class Home extends CI_Controller{
public function __construct()
{
parent::__construct();
//load the settings model
$this->load->Model('settings_model');
//get the all settings and store it into a variable
$siteSettings = $this->settings_model->SiteSettings();
//how to pass the data to view
$data['site'] = $siteSettings;
}
public function index()
{
//some code and pass the value to view
$data["some_item"] = "some value";
//Load the view
$this->load->view("home",$data);
}
public function SomeMethod1()
{
//some code and pass the value to view
$data["some_item"] = "some value";
//Load the view
$this->load->view("home",$data);
}
public function SomeMethod2()
{
//some code and pass the value to view
$data["some_item"] = "some value";
//Load the view
$this->load->view("home",$data);
}
}
But my problem is i want to pass $siteSettings to my each method. I don't want to fetch the data every time from settings_model for my different method of home controller. I just want to get the data from database on __construct() and pass the value to each method when i call the different method.
How can i achieve this? Should I use a public variable and store the fetched data to this variable and call the variable from different method?
Set that in session. and can access all over the project.
public function __construct()
{
parent::__construct();
$this->session->unset_userdata('site'); # unset on each and every time
$this->load->Model('settings_model');
$siteSettings = $this->settings_model->SiteSettings();
$this->session->set_userdata('site', $siteSettings); # Set after end of __construct
}
Or do like this
public function __construct()
{
parent::__construct();
$this->load->Model('settings_model');
$this->data['site'] = $this->settings_model->SiteSettings();
}
In function
$this->load->view("name",$this->data);
i'm making an application where i'm saving user information in user controller of my code igniter application inside a static variable, now i want to access static variable in other controller, how can i approach that? my code here
<?php
class User extends CI_Controller{
public static $user_data = array();
public __construct()
{
parent::__construct();
self::$user_data = array('value'); // values from the model
}
}
// now i can user that static variable from the view in this controller
class Friends extends CI_Controller{
public __construct()
{
parent::__construct();
if(User::$user_data->isFriends)
{
redirect('person/'.User::$user_data->id);
}
}
}
// how can i access this functionality in codeigniter? it gives error undefined class User not found
static::$user_data = array('value')
shouldn't it be self::... in your User class?
There are a number of issues with your code.
As you have defined that $user_data is an array with public static $user_data = array(); at the top of the User class, it is unnecessary to redefine it on line 7, i.e. self::$user_data = array('value');
Second issue is that the $user_data array will only contain one element, 'value'. I assume by lines 15 and 17 (User::$user_data->isFriends and User::$user_data->id), you are looking for two elements, 'isFriends' and 'id', these will not exist.
This brings me to another issue, the syntax you are using to get an element of the $user_data array will not work. The syntax '->' followed by a property name is used for variables of type Object, and cannot be used for an associative array.
Instead, you should use - for example - $user_data['isFriends'].
I have updated your code below...
<?php
class User extends CI_Controller{
public static $user_data = array();
public __construct()
{
parent::__construct();
// this does the same thing as self::$user_data = array('value');
// but without unnecessarily declaring the array again.
self::$user_data[] = 'value';
}
}
// now i can user that static variable from the view in this controller
class Friends extends CI_Controller{
public __construct()
{
parent::__construct();
if(User::$user_data['isFriends'])
{
redirect('person/'.User::$user_data['id']);
}
}
}
// how can i access this functionality in codeigniter? it gives error undefined class User not found
Try running the code and let me know what errors are being displayed and I will try and resolve them by updating my answer