0

I am using native PHP sessions ($_SESSION) with CodeIgniter framework.

I have a "Login" controller loads view where user enters login and password. After the user submits the login form, the "Login" controllers authenticate() method is called. If everything is alright i add some data to $_SESSION array, then i redirect user to "Organisation" controllers myOrganisation() method. I'm calling session_start() in Login/login() , Login/authenticate() and Organisation/myOrganisation() methods, but still the session is not passed, because in myOrganisation() method the session is new.

I tested my cookies functionality with creating 2 test php pages, where i just echo session id. It works perfectly. Maybe i am not putting session_start() in all places it needs to be? (i put them in all controllers methods).

Login Controller:

class Login extends CI_Controller {
public function index() {
        session_start();
        $this->load->view("Login/index", $data);

    public function authenticate() {
                    session_start();
                    $_SESSION['login'] = $login; //  everything is alright, redirect
        header("location: ".base_url()."Organisations/MyOrganisation");

Organisation controller:

public function MyOrganisation() {
    session_start(); // here session is a new one, not passed
    if(isset($_SESSION['login'])) {
3
  • Does base_url() return the same domain as where you login? Otherwise your session would restart. Commented Apr 18, 2012 at 21:58
  • Out of curiosity, is there a reason you are not using the CodeIgniter built in Session Library? Also, this might just be a product of you typing it in here, but you haven't closed your first function. index() is still open when you start authenticate(). Commented Apr 18, 2012 at 22:09
  • in src, index() is closed. I am not using CI sessions because i got some problems, trying to configure and make them work. Spent like 4 hours, and they never worked, so i decided to switch to native seesions Commented Apr 19, 2012 at 9:15

2 Answers 2

1

I don't know what was wrong with the session_start() placements that i did, but the one thing that solved the problem was to place it in index.php in main folder

Sign up to request clarification or add additional context in comments.

1 Comment

that solved the problem for me too. I asked a similar question, but I thought the issue was different because I am using CodeIgniter with a subdomain.
0

session_start() can also be specified in a constructor, not every method in a class. That means both your controllers can have this:

function __construct () {
    ini_set("session.gc_maxlifetime", 14400);
    ini_set("session.cookie_domain", .yourdomain.com);
    session_set_cookie_params(14400, '/', .yourdomain.com);
    session_start();
}

The first 3 lines in a constructor are to make sure the session cookie is valid for a long time and under you domain.

Besides that (and not closed index() and authenticate() methods), where's $login coming from?

1 Comment

i just omitted the most part of code, which was not important

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.