Codeigniter session working fine but not working on Godaddy

I developed a website in the php framework (Codeigniter) that works fine on my local server (WAMP Server). When I upload it to Godaddy hosting, it cannot log into the site. The following is the function of the input class.

public function index() { if ($this->session->userdata('admin_login') == 1) redirect(base_url() . 'index.php?admin/dashboard', 'refresh'); $this->load->view('backend/login'); } function ajax() { $response = array(); $email = $_POST["email"]; $password = $_POST["password"]; $response['submitted_data'] = $_POST; $login_status = $this->validate_login( $email , sha1($password) ); $response['login_status'] = $login_status; if ($login_status == 'success') { $response['redirect_url'] = ''; } echo json_encode($response); } function validate_login($email = '' , $password = '') { $credential = array( 'email' => $email , 'password' => $password ); $query = $this->db->get_where('users' , $credential); if ($query->num_rows() > 0) { $row = $query->row(); $this->session->set_userdata('admin_login', '1'); $this->session->set_userdata('admin_id', $row->id); $this->session->set_userdata('name', $row->name); $this->session->set_userdata('login_type', 'admin'); return 'success'; } return 'invalid'; } 

I understand that the $ this-> session-> set_userdata () method does not work on godaddy.

+6
source share
7 answers
 Please try this Please add session library just like below. $this->load->library('session'); if ($this->session->userdata('admin_login') == 1) redirect(base_url() . 'index.php?admin/dashboard', 'refresh'); $this->load->view('backend/login'); 
+2
source

First boot library then instead of using

  if ($this->session->userdata('admin_login') == 1) 

Use it

 if ($this->session->userdata['admin_login'] == 1) 

Check if it works or not.

+2
source

Try several alternatives: Change these lines in application/config/config.php

 $config['sess_cookie_name'] = 'cisession'; //remove(underscore) $config['cookie_domain'] = ".example.com"; //make it site wide valid 
+1
source

You can save multiple session variables in an array

 function validate_login($email = '', $password = '') { $credential = array('email' => $email, 'password' => $password); $query = $this->db->get_where('users', $credential); if ($query->num_rows() > 0) { $row = $query->row(); $sess_array = array( 'admin_login' => 1, 'admin_id' => $row->id, 'name', $row->name, 'login_type', 'admin' ); $this->session->set_userdata('logged_in', $sess_array); } return 'invalid'; } 

You need to load session library into your constructor

 public function __construct() { parent::__construct(); /* necessary! */ /* load model for calls to database */ $this->load->library('session');// load session library $this->load->helper('url'); } 

In index you should check if user is logged_in or not

 public function index() { if ($this->session->userdata('logged_in')){// check session set or not redirect(base_url() . 'index.php?admin/dashboard', 'refresh'); $this->load->view('backend/login'); } } 
+1
source

OK, I assume that you are using CI2 as you get FALSE in a non-existent var session. CI sessions use cookies and / or a database, depending on your configuration. What are the cookie settings in your configuration file?

You should pay attention to the following settings:

 $config['cookie_prefix'] = ""; $config['cookie_domain'] = ""; $config['cookie_path'] = "/"; $config['cookie_secure'] = FALSE; 

and these (the default configuration is shown in both cases):

 $config['sess_cookie_name'] = 'ci_session'; $config['sess_expiration'] = 7200; $config['sess_expire_on_close'] = FALSE; $config['sess_encrypt_cookie'] = FALSE; $config['sess_use_database'] = FALSE; $config['sess_table_name'] = 'ci_sessions'; $config['sess_match_ip'] = FALSE; $config['sess_match_useragent'] = TRUE; $config['sess_time_to_update'] = 300; 

Is your site hosted on a subdomain? cookies are set through the headers. is there any output generated before your cookies are set? set your log threshold to 2 so that you can see errors and debugging messages.

 $config['log_threshold'] = 2; 

Have you set the registration key?

 $config['encryption_key'] = '[there-must-be-something-here]'; 
+1
source

I tried all the answers that are mentioned here, but not working for my problem.

My problem

$this->session->userdata('admin_login') returns false.

Finally, I tried a database to store the session.

 $config['sess_use_database'] = TRUE; $config['sess_table_name'] = 'ci_sessions'; 

And create the ci_sessions table.

 CREATE TABLE IF NOT EXISTS `ci_sessions` ( session_id varchar(40) DEFAULT '0' NOT NULL, ip_address varchar(45) DEFAULT '0' NOT NULL, user_agent varchar(120) NOT NULL, last_activity int(10) unsigned DEFAULT 0 NOT NULL, user_data text NOT NULL, PRIMARY KEY (session_id), KEY `last_activity_idx` (`last_activity`) ); 

Then it works great

+1
source

It must be a configuration configuration

 $config['sess_cookie_name'] = 'cisession'; $config['sess_expiration'] = 7200; $config['sess_expire_on_close'] = TRUE; $config['sess_encrypt_cookie'] = TRUE; $config['sess_use_database'] = TRUE; $config['sess_table_name'] = 'ci_sessions'; $config['sess_match_ip'] = FALSE; $config['sess_match_useragent'] = TRUE; $config['sess_time_to_update'] = 300; 

Also in your model you must establish a session.

 public function login () { $user = $this->get_by(array( 'email' => $this->input->post('email'), 'password' => $this->hash($this->input->post('password')), ), TRUE); if (count($user)) { // Log in user $data = array( 'name' => $user->name, 'email' => $user->email, 'gender'=>$user->gender, 'user_num'=>$user->user_num, 'id' => $user->id, 'loggedin' => TRUE, 'balance' =>$user->balance, 'usertype' =>'admin', ); $this->session->set_userdata($data); } } 

This works for me :)

0
source

Source: https://habr.com/ru/post/977582/


All Articles