Jump to content

Login Script


doddsey_65

Recommended Posts

okay, i have a login page. When the user presses submit jQuery is used to send the post data to the login_process.php script, which fires the loginUser() method.

 

if(isset($_POST['username']) && isset($_POST['password']) && isset($_POST['remember']))
{
    $user->loginUser($_POST['username'], $_POST['password'], $_POST['remember']);
}

 

this class method does all of the validation then outputs either 'success' or the error back to the jquery script. The problem is that i have this line in the class:

 

$this->userData[] = $row;

 

userData is public $userData = array();

 

its supposed to add the query result array to this array so i can call the info from other scripts like

 

$user->userData['u_username']

 

but userData never gets populated with the query result. I am calling the new userClass via the initialize.php page which is called from every page. Could this be why?

 

Here is the full user class:

 

class userClass extends mainCore
{
    private $db;
    
    /**
     * Hash Method
     * can be one string or multiple sepearated with comma
     *
     * @return array
     */
    public $hash = 'sha1, md5, sha1, sha1';
    
    public $start_hash;
    public $end_hash;
    
    public $cookieName;
    public $cookiePath = '/';
    public $cookieValue;
    public $cookieTime = 9999999;
    
    /**
     * holds all the data for a logged in user
     *
     * @return array
     */
    public $userData = array();
    
    public $asf;

    public function loginUser($username, $password, $remember = 'no')
    {
        session_start();
        
        $this->getConfig();
    
        // return an error if there are blank fields
        if(empty($username) || empty($password))
        {
            $this->error('Please fill in all fields');
        }
        
        // connect to the database
        $this->db = new db_pdo(
                            $this->config['db']['hostname'],
                            $this->config['db']['database'],
                            $this->config['db']['username'],
                            $this->config['db']['password']);
        
        // convert the hash into an array
        $hash = explode(',', $this->hash);
        
        // the beginning of the hash srting
        $this->start_hash = implode('(',$hash);
            
        // the length of the hash
        // used to print right amount of closing brackets
        $hash_length = count($hash);
        
        // add an extra bracket which isn't added by implode
        $this->start_hash .= '(';
        
        // add the correct amount of closing brackets
        for($i=0; $i<$hash_length; $i++)
        {
            $this->end_hash = str_repeat(')', $hash_length);
        }
        
        // hash the password
        $password = $this->start_hash."'".$password."'".$this->end_hash;
        
        // query to match the user submitted info
        $lookup_query = 
            "SELECT u_username, u_salt, u_password
            FROM ".TBL_PREFIX."users
            WHERE u_username = '$username'
            AND u_password = $password
            LIMIT 1";
        
        // execute the lookup query
        $execute = $this->db->query($lookup_query)
            or die($this->db->printError($lookup_query));
        
        // grab the query results
        $row = $execute->fetch(PDO::FETCH_ASSOC);
        
        // assign userData array
        if($row)
        {
            $this->userData[] = $row;
            
            // if remember me was checked add a cookie
            if($remember == 'yes')
            {
                $this->cookieName  = 'uniqueId';
                $this->cookieValue = md5($this->userData['u_username']);
                $cookie = setcookie($this->cookieName, $this->cookieValue, time()+$this->cookieTime, $this->cookiePath);
            }

            $_SESSION['logged_in'] = 'yes';
            
            echo 'success';
            
            return $this->userData;
        }
        else
        {
             $this->error('Invalid details');
        }
    }
}

 

Link to comment
Share on other sites

It sounds to me like you need to store that data in $_SESSION.  You may be able to store the class instance in $_SESSION, but make sure the class is defined when session data is read in (ie session_start() must occur after classes are defined, unless you are autoloading class definitions).

 

Just to confirm, you are storing the data from a script called from jQuery, then you want to access the data in a php script called from elsewhere?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.