Welcome Guest, Not a member yet? Register   Sign In
Sessions don't seem to be catching
#1

[eluser]RockerBoy402[/eluser]
I have a custom library but I can't seem to access the sessions library (which is autoloaded). I know sessions are working because it inserts the basic session information into a database table, but it doesn't ever insert custom session information. Here is the code I've got:


Code:
<?php if( ! defined('BASEPATH')) exit('No direct script access allowed');

class Auth
{

    public $CI;

    /*
     * Auth class constructor
     */
    function __construct()
    {
        // Create an instance of CI resources
        $this->CI =& get_instance();
    }

    /*
     * Takes the submitted information and registers the visitor
     *
     * @access  public
     * @param   array
     * @return  array
     */
    function register_visitor($visitor_details)
    {
        
    }

    /*
     * Takes submitted credentials and looks for a valid match in the database
     *
     * @access  public
     * @param1  string
     * @param2  string
     * @return  boolean
     */
    function validate_visitor($username, $password)
    {
        
        $password = md5($password); // Apply md5() hash to the password
        
        // See if the credentials match that in the database
        $sql = "SELECT * FROM `users` WHERE `username`='{$username}' AND
                `password`='{$password}' AND `banned`=0 AND `active`=1 LIMIT 1";
                
        $query = $this->CI->db->query($sql);
        
        $count = $query->num_rows();
        
        if ($count > 0) // User validates
        {
            // Go ahead and update the users IP Address and Last Login in the database
            $sql2 = "UPDATE `users` SET `ip_address`='" . $_SERVER['REMOTE_ADDR'] . "'
                WHERE `username`='{$username}'";

            $query2 = $this->CI->db->query($sql2);

            // Now set the session information
            $user_session_information = array(
                'username' => $username,
                'logged_in' => TRUE
            );

            $this->CI->session->set_userdata($user_session_information);

            return true;
        } else { // Invalid user
            return false;
        }
    }

    /*
     * Determines if the user is authenticated
     *
     * @access  public
     * @return  boolean
     */
    function is_authenticated()
    {
        if ($this->CI->session->userdata('logged_in') == TRUE)
        {
            return true;
        } else {
            return false;
        }
    }

}


/* End of file Auth.php */

In is_authenticated() it should be grabbed the userdata inserted up in validate_visitor(), right?

Thanks in advanced guys!
#2

[eluser]RockerBoy402[/eluser]
*bump* Sad
#3

[eluser]InsiteFX[/eluser]
Try this:
Code:
/*
     * Auth class constructor
     */
    function __construct()
    {
        parent::__construct();

        // Create an instance of CI resources
        $this->CI =& get_instance();
    }

InsiteFX
#4

[eluser]bubbafoley[/eluser]
[quote author="InsiteFX" date="1298704991"]Try this:
Code:
/*
     * Auth class constructor
     */
    function __construct()
    {
        parent::__construct();

        // Create an instance of CI resources
        $this->CI =& get_instance();
    }

InsiteFX[/quote]

This library has no parent :coolsmile:



It works for me. Are you sure you're getting a valid database result?

Here's my code.

application/libraries/auth.php:
Code:
<?php

class Auth
{

    public $CI;

    /*
     * Auth class constructor
     */
    function __construct()
    {
        // Create an instance of CI resources
        $this->CI =& get_instance();
    }

    /*
     * Takes the submitted information and registers the visitor
     *
     * @access  public
     * @param   array
     * @return  array
     */
    function register_visitor($visitor_details)
    {
        
    }

    /*
     * Takes submitted credentials and looks for a valid match in the database
     *
     * @access  public
     * @param1  string
     * @param2  string
     * @return  boolean
     */
    function validate_visitor($username, $password)
    {

            // Now set the session information
            $user_session_information = array(
                'username' => $username,
                'logged_in' => TRUE
            );

            $this->CI->session->set_userdata($user_session_information);

            return true;
    }

    /*
     * Determines if the user is authenticated
     *
     * @access  public
     * @return  boolean
     */
    function is_authenticated()
    {
        if ($this->CI->session->userdata('logged_in') == TRUE)
        {
            return true;
        }
        return false;
    }

}
tip: if you return inside an "if()" then you don't need an else. just continue on like normal.


application/controllers/welcome.php
Code:
<?php

class Welcome extends CI_Controller {

    function __construct()
    {
        parent::__construct();
        $this->load->library('session');
    }

    function index()
    {
        $this->load->library('auth');
        $this->auth->validate_visitor('foo', 'bar');
        if($this->auth->is_authenticated())
        {
            echo $this->session->userdata('username');
        }
    }

}

Output:
Code:
foo




Theme © iAndrew 2016 - Forum software by © MyBB