Welcome Guest, Not a member yet? Register   Sign In
Log In Session Destroyed - (Solved)
#1

[eluser]Unknown[/eluser]
Hey,

I am noticing that my new log in form, which points to:

www.site.com/calendar/authorize

creates a session and saves a value 'user_id' to it as well once the user is authenticated.
That's fine. Problem is when I am on the control panel home page, I click on a link to create a calendar event:

http://www.site.com/calendar/create/

the session is lost instantly and a new one is created.

This happens regardless of whether I use the DB or not to store sessions (as instructed in your docs).

The session loads by default in the autoload.php file, btw. Below is code of the authorize, index, create and controller functions in case you wish to look at the code.

Create Function: (/create/)

Code:
function create(){
    
    $data['title'] = "Add Events to Calendar";

    if(isset($_POST['add']))
    {
        //check for empty inputs
        if((isset($_POST['date']) && !empty($_POST['date'])) && (isset($_POST['eventTitle']) && !empty($_POST['eventTitle'])) && (isset($_POST['eventContent']) && !empty($_POST['eventContent'])))    
        {
            //add new event to the database
            $data['alert']= $this->MCalendar->addEvents();
        }
        else
        {
            //alert message for empty input
            $data['alert'] = "No empty input please";
        }
    }
        
        $data['header'] = 'Create new event';
        $data['main'] = 'calendar_create';
        $this->load->vars($data);
        $this->load->view('calendar_create_temp');

}

The Authorize Function:

Code:
function authorize()
  {
  
  $username=$_POST["username"];
  $password=$_POST["password"];
  
  $hash_pass=md5($password);
  
  $query = $this->db->query("select ID, username,password from eventcal_users where username='$username' AND password='$hash_pass'");

  if ($query->num_rows() > 0)
  {
  
  # Add to session
  
  $row=$query->row();
  $user_id=$row->ID;

  # array to add to existing session  
$newdata = array(
                   'username'  => $username,
                   'logged_in' => TRUE,
                   'user_id'   => $user_id
               );

$this->session->set_userdata($newdata);

header("Location: http://www.site.net/calendar/");

  }
  
  else
  
  {
  echo "<P>Sorry, please try again!";
  }
  
  }

Index function (Welcome page - session shows up fine)

Code:
function index(){
    // The forth segment will be used as timeid
    
    $timeid = $this->uri->segment(3);
    if($timeid==0)
        $time = time();
    else
        $time = $timeid;
    
    // we call _date function    
    $data = $this->_date($time);
    
    // Set all other variables here
    $data['title'] = "Manage Calendar";
    
    $data['user_id'] = $this->session->userdata('user_id');
          
    $data['header'] = 'Calendar';
    $data['main'] = 'calendar_home';
    // $this->load->view('calendar_home',$data);
    $this->load->vars($data);
    $this->load->view('template');
}

Parent:

Code:
function Calendar(){
    parent::Controller();
    
    # Having this loaded means any function, such as dayevents, can pull from the model by calling
    # $this->MCalendar->getDayEvents($day);
    
    $this->load->model('MCalendar');
  
  }

I hope this helps. Thank you for your time, everybody.
#2

[eluser]Unknown[/eluser]
Interesting. For those going through the same thing, here is what I noticed:

If I log in to:

http://www.mysite.com/calendar and then directly visit:

http://www.mysite.com/calendar/create

the session holds

Problem is the links lead to:

http://www.mysite.com/index.php/calendar/create

Note the index.php. I figured it had to be something head slappingly stupid. I was right! Time to remove the index.php reference for good.




Theme © iAndrew 2016 - Forum software by © MyBB