最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

php - CI4 Not Staying Logged In - Stack Overflow

programmeradmin1浏览0评论

I have an issue that I've been troubleshooting for days that just doesn't make any sense to me. I'm hoping someone has any idea to help!

I'm working on a page where you need to be logged in. The first login works, and I am redirected to the landing page. I see the session variables in my console.

If I refresh the page or try to submit the form on that page I am redirected to the login page and am logged out.

Here is the strange part-

-If I use our production database, instead of the development database, the code works fine and stays logged in (so then I think the issue is the database).

-However, if I use my login from the development database in my colleagues developer space (with the same checked out code), there are no problems. (then I think the issue is with the browser).

-If I use a different browser, totally different machine, clear cookies...I still have the problem.

We are using Codeigniter 4, with CodeIgniter-Ion-Auth for authentication. This is the code on the controller page checking the login:

$this->ionAuth = new \IonAuth\Libraries\IonAuth();
//a user must be logged in to go here so check if they are logged in and are an admin
if (!$this->ionAuth->loggedIn()) {    
            $this->session->setTempdata('message', 'Please log in to access this part of the website');
            // for CI4/CI3 redirection use just uri_string
            $this->session->setTempdata('destination', uri_string(current_url()));
            // for CI4 use url
            $this->session->setTempdata('url', current_url());
            return redirect()->to('auth/login');
            exit;
        }

I tried:

-Creating a new user in the development database and using that user to log in (still have the issue)

-using my colleagues login credentials from the development database in my development space on my machine (still have the issue)

-Clearing cookies, using incognito mode, using different machine (still have the issue)

-Flushing DNS/reboot router and computer (still have the issue)

Any ideas are appreciated!

I have an issue that I've been troubleshooting for days that just doesn't make any sense to me. I'm hoping someone has any idea to help!

I'm working on a page where you need to be logged in. The first login works, and I am redirected to the landing page. I see the session variables in my console.

If I refresh the page or try to submit the form on that page I am redirected to the login page and am logged out.

Here is the strange part-

-If I use our production database, instead of the development database, the code works fine and stays logged in (so then I think the issue is the database).

-However, if I use my login from the development database in my colleagues developer space (with the same checked out code), there are no problems. (then I think the issue is with the browser).

-If I use a different browser, totally different machine, clear cookies...I still have the problem.

We are using Codeigniter 4, with CodeIgniter-Ion-Auth for authentication. This is the code on the controller page checking the login:

$this->ionAuth = new \IonAuth\Libraries\IonAuth();
//a user must be logged in to go here so check if they are logged in and are an admin
if (!$this->ionAuth->loggedIn()) {    
            $this->session->setTempdata('message', 'Please log in to access this part of the website');
            // for CI4/CI3 redirection use just uri_string
            $this->session->setTempdata('destination', uri_string(current_url()));
            // for CI4 use url
            $this->session->setTempdata('url', current_url());
            return redirect()->to('auth/login');
            exit;
        }

I tried:

-Creating a new user in the development database and using that user to log in (still have the issue)

-using my colleagues login credentials from the development database in my development space on my machine (still have the issue)

-Clearing cookies, using incognito mode, using different machine (still have the issue)

-Flushing DNS/reboot router and computer (still have the issue)

Any ideas are appreciated!

Share Improve this question edited Jan 29 at 16:35 xanabobana asked Jan 29 at 13:33 xanabobanaxanabobana 873 silver badges19 bronze badges 9
  • Sounds like a cookie problem. I'd suggest you follow the examples in the libraries documentation and the CI4 handbook. Additionally the code you posted might be relevant but you have not told us what specifically prevented you to do it as documented in the first place, it is not visible in the code nor your description. Please edit in all the relevant details. – hakre Commented Jan 29 at 14:38
  • I am using IonAuth and CI4 in this case according to the documentation, I'm not sure what makes you think it is done differently than what is documented? github/benedmunds/CodeIgniter-Ion-Auth/blob/4/… – xanabobana Commented Jan 29 at 14:58
  • This comment specifically: " // use header because CI4 redirect needs to return and can't return in initController ": Different to what you have just linked, your code does not return redirect()->to('auth/login');. So also the link you posted. – hakre Commented Jan 29 at 15:00
  • Thank you! I didn't think the issue was the code, because there is no problem when I use a different database for authentication. I updated the code to use return redirect()->to('auth/login'); in the method, cleared the cookies again, but still am being logged out. – xanabobana Commented Jan 29 at 16:33
  • Hmm, if you didn't think the code is related, why did you write your question about it? And as you've shown it could be unrelated to the cookie problem itself as you write that the problem persists. – hakre Commented Jan 29 at 16:44
 |  Show 4 more comments

1 Answer 1

Reset to default 0

I was able to fix the issue but still don't quite understand the why. We have Codeigniter 3 and Codeigniter 4 running side by side while we do a large migration project. I had my index.php for CI3 set to the production database. When my .env for CI4 was set to development, things didn't work. When I changed the index.php to development, and the two matched, I was able to stay logged in.

The code for CI4 pages shouldn't be hitting CI3 code, so I'm going to try to track down why that was happening. It's a complex configuration we have going on, but we have an extremely large code base that needs to be rewritten.

发布评论

评论列表(0)

  1. 暂无评论