Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6684373
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T04:55:58+00:00 2026-05-26T04:55:58+00:00

I want to keep login status with cookie in Codeigniter, but I can’t set

  • 0

I want to keep login status with cookie in Codeigniter, but I can’t set cookie in Codeigniter. This is my code in Controller. when I click submit button in the login form it call chk_login() function to check username and password in DB. After that, it go to this line echo “You are not log in” only. how to use cookie in Codeigniter.

<?php
class Login extends CI_Controller
{

    public function __construct()
    {
        parent::__construct();
                $this->load->helper('cookie');
    }

    function index()
    {
        redirect('login/form_login');
    }

    function form_login()
    {
        $data['title'] = 'Login';
        $this->load->view('form_login_view', $data);
    }

    function goto_test()
    {
            if($this->input->cookie('login')){
               echo "You are logged in"; 
            }
            else{

                echo "You are not log in";  // It show string in this line every time. that mean cookie not set yet. how to use cookie in Codeigniter. 
            }
    }


function chk_login()
{
$this->load->model('login_model', 'login');
$this->login->username = $this->input->post('username');
$this->login->password = $this->input->post('password');

$user_login = $this->login->get_by_user_pass();

         if($user_login==null){
                $data['errors']  = 'Not found Username.<br />';
                $this->load->view('form_login_view', $data);

    }else{

        // I set directed data in cookie but I can't get this cookie in goto_test().

        $this->input->set_cookie(array(
                               'login'=>TRUE,
                'username'=>"aaa",
                'password'=>"123",
                'status'=>"user")); 


            redirect('login/goto_test');  // Go to function goto_test in this class
    }  
}

}
?>

I try to test with this code it show bool(false).

$cookie = array(
          'name'   => 'test_cookie',
          'value'  => 'test',
          'domain' => '/',
          'secure' => TRUE
          );

$this->input->set_cookie($cookie); 

var_dump($this->input->cookie('name'));
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T04:55:58+00:00Added an answer on May 26, 2026 at 4:55 am

    Now I know that. I can’t set cookie like that. I have 2 way for set cookie.

    1.

    $cookies = array(
            array(
              'name'   => 'login',
              'value'  => true,
              'domain' => '/',
              'secure' => true
        ),
            array(
              'name'   => 'username',
              'value'  => 'aaa',
              'domain' => '/',
              'secure' => true
        ),
            array(
              'name'   => 'password',
              'value'  => '123',
              'domain' => '/',
              'secure' => true
        )
    );
    
    foreach($cookie in $cookies){
            $this->input->set_cookie($cookie); 
    }
    

    2.

    setcookie('login', true);
    setcookie('username','aaa');
    setcookie('password','123');
    

    The cookie can’t set like this.

    $cookie = array(
              'login'   => TRUE,
              'username'  => 'aaa',
              'password' => '123'
              );
    
    $this->input->set_cookie($cookie); 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to keep viewstate of a specific page in session but following code
I want to create a login page, it can easy implement using Ruby on
I've had this before, but can't seem to replicate the steps I did back
I have implanted a login code to a site , after that I want
I'm new to android development In my application I want to keep log-in status
I'll keep this one as simple as I can. I have a method in
I want to keep logs of some things that people do in my app,
I want to keep my website/s in version control (Subversion specifically) and use svn
I want to keep the class simple and not defined a constructor so i
I want to keep tabs on the number of concurrent users of my application.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.