TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 01-09-2009, 11:32 AM   #1 (permalink)
The Acquainted
 
Join Date: Oct 2007
Posts: 172
Thanks: 18
maZtah is an unknown quantity at this point
Default Need help with a User class

Recently I'm working on a User class. I need some help with this class to get it on track.

For example, I want a user to get logged in, how would I do this? Do I need to pass the HTML form to the login() function? Or do I need to pass all variables (like email and password) as variables, or?

Also, how to correctly interact with the Database class I've written? Should I do something like this?

PHP Code:
private $m_pConn;

function 
__construct()
{
    
$m_pConn = new Database();

Or am I thinking in the wrong way? Oh well, someday I will fully understand the OOP way of thinking. Thanks in advance for your replies.

This is what I have thus far:

PHP Code:
class User
{
    private 
$m_iId;
    private 
$m_szName;
    private 
$m_szEmail;
    
    private 
$m_bLoggedIn;
    
    function 
login()
    {
        
// Do login here.
    
}
    
    function 
get_id()
    {
        return 
$this->m_iId;
    }
    
    function 
get_name()
    {
        return 
$this->m_szName;
    }
    
    function 
get_email()
    {
        return 
$this->m_szEmail;
    }
    
    function 
is_logged_in()
    {
        return (bool) 
$m_bLoggedIn;
    }

maZtah is offline  
Reply With Quote
Old 01-09-2009, 01:31 PM   #2 (permalink)
The Addict
 
CoryMathews's Avatar
 
Join Date: Nov 2007
Location: USA
Posts: 258
Thanks: 7
CoryMathews is on a distinguished road
Default

you just need to pass in the username and password to the login then you do your query to check if its a valid login or not. the login function should return a true or false. The db class can be created like that, that is fine. You will have to store this class in a session variable to keep from reloading it every page as well.
CoryMathews is offline  
Reply With Quote
The Following User Says Thank You to CoryMathews For This Useful Post:
maZtah (01-10-2009)
Old 01-10-2009, 09:58 AM   #3 (permalink)
The Acquainted
 
Join Date: Oct 2007
Posts: 172
Thanks: 18
maZtah is an unknown quantity at this point
Default

Thanks for your reply! It helps me out well.

Quote:
Originally Posted by CoryMathews View Post
You will have to store this class in a session variable to keep from reloading it every page as well.
I assume - with 'this class' - you mean the User class. Should I store only the boolean $m_bLoggedIn in a session, or the whole object?

Like:

PHP Code:
$_SESSION['user'] = $pUser->is_logged_in();

// or
$_SESSION['user'] = $pUser
maZtah is offline  
Reply With Quote
Old 01-10-2009, 12:38 PM   #4 (permalink)
The Contributor
 
Join Date: Jan 2009
Posts: 40
Thanks: 10
Scottymeuk is on a distinguished road
Default

I wouldnt do that.

You need to do things like

PHP Code:
$user->login($_POST
That above bit goes in the bit that says, if post etc.

Then on the login bit you need to make sessions etc. I will post my one (Please note this wont work properly as it integrates methods etc that my framework has so you will need to change it a lot but you will be able to see what i mean):

Please note that its not perfect as i did code it at like 3am.

PHP Code:
<?php
function login($userData$redirect '')
{        
    if(!
$this->isRegistered()) 
    {
        
$this->core->session->destroy();
        if(!empty(
$userData['username']) and !empty($userData['password']))
        {
            
$password $this->core->app->generateHash($userData['password'], $userData['username']);
               
            
$data = array('userId','username','password');
            
            
$sql "SELECT userId
                    FROM users
                    WHERE username = '" 
$userData['username'] . "'
                        AND password = '$password'
                    LIMIT 1"
;
            
            
$checkUser $this->core->database->query($sql);
           
            if(
$this->core->database->getNumRows($checkUser) == 1)
            {
                while(
$userDetails $this->core->database->getArray($checkUser))
                {
                    
$sessionId $this->core->app->generateHash(session_id(), $userData['username']);
                        
                    if(
$this->core->session->register('isregistered') == false)
                    {
                        
$this->core->session->delete_all_var();
                        return 
false;
                    }
                    else
                    {
                        if(
$this->core->session->set_var('userid',$userDetails['userId']) == false)
                        {
                            
$this->core->session->delete_all_var();
                            return 
false;
                        }
                        else
                        {
                            if(
$this->core->session->set_var('password',$password) == false)
                            {
                                
$this->core->session->delete_all_var();
                                return 
false;
                            }
                            else
                            {
                                if(
$this->core->session->set_var('session_hash',$sessionId) == false)
                                {
                                    
$this->core->session->delete_all_var();
                                    return 
false;    
                                }
                            }
                        }
                    }
                       
                    
$expiresTime time() + 3600;
                       
                    
$sql "INSERT INTO sessions
                            (userid,sessionhash,expires)
                            VALUES ('" 
$userDetails['userId'] . "','$sessionId','$expiresTime')";
                    
                    
$query $this->core->database->query($sql);
                    
                    if(
$query)
                    {
                           if(!empty(
$redirect))
                           {
                               
$this->core->app->redirect($redirect);
                           }
                        return 
true;
                    }
                    else
                    {
                        return 
false;
                    }
                }
            }
            return 
false;
        }
        return 
false;
    }
}
?>
Scottymeuk is offline  
Reply With Quote
The Following User Says Thank You to Scottymeuk For This Useful Post:
maZtah (01-10-2009)
Old 01-10-2009, 04:57 PM   #5 (permalink)
The Acquainted
 
Join Date: Oct 2007
Posts: 172
Thanks: 18
maZtah is an unknown quantity at this point
Default

Thanks for your reply too. It's quite hard to read due to no commenting, but I get the trick.

I will soon post my new code.
maZtah is offline  
Reply With Quote
Old 01-10-2009, 05:04 PM   #6 (permalink)
The Contributor
 
Join Date: Jan 2009
Posts: 40
Thanks: 10
Scottymeuk is on a distinguished road
Default

Well basically it does:
  1. Checks to see if the user details are ok
  2. Starts A Session with session hash( not just the session id, but a mix of a lot of things), a password hash (for checking each time isRegistered() is called), and an expires time
  3. Then it adds the session infomation to the database

I then have a isRegistered() function that checks all the information against database et.
Scottymeuk is offline  
Reply With Quote
Old 01-11-2009, 02:20 AM   #7 (permalink)
The Addict
 
CoryMathews's Avatar
 
Join Date: Nov 2007
Location: USA
Posts: 258
Thanks: 7
CoryMathews is on a distinguished road
Default

personally I just store the entire user class. Mine is pretty small with only about 8-10 vars and just a couple functions.

A second reason why I save the entire class is so that I don't have to keep doing the same sql queries every time I load the page to get the users name ect. I only run them once when the user logs in. and can then easily access them any time I need.
CoryMathews is offline  
Reply With Quote
Old 01-11-2009, 01:47 PM   #8 (permalink)
The Contributor
 
Join Date: Jan 2009
Posts: 40
Thanks: 10
Scottymeuk is on a distinguished road
Default

I do as well but I just don't want to send him my whole class.
Scottymeuk is offline  
Reply With Quote
Old 01-12-2009, 10:04 AM   #9 (permalink)
The Acquainted
 
Join Date: Oct 2007
Posts: 172
Thanks: 18
maZtah is an unknown quantity at this point
Default

Thanks guys. I'm getting more and more into OOP.

At the moment I'm wondering if it's better do do something like this:

Create a Login class and a User class. First the user logs in via the Login class. If succesfully logins, the Login class creates a User class with the id of the user f.e. like $pUser = new User(1);. Then the Login class puts the User class in a session.

Shouldn't this be more logically (while thinking OOP)? Or am I just lost again? :-p
maZtah is offline  
Reply With Quote
Old 01-12-2009, 04:39 PM   #10 (permalink)
The Addict
 
CoryMathews's Avatar
 
Join Date: Nov 2007
Location: USA
Posts: 258
Thanks: 7
CoryMathews is on a distinguished road
Default

That would work, however on one hand does your login class really have much to it? Personally I only have 2 functions and no vars that go with my login. So just adding those 2 to my user class is not much to store. But if you have quiet a few items that are only used in your login then it might make sense to split it up. I don't think it will make a difference one way or the other its really just how you want to do it.
CoryMathews is offline  
Reply With Quote
Old 01-13-2009, 06:02 PM   #11 (permalink)
The Contributor
 
Join Date: Jan 2009
Posts: 40
Thanks: 10
Scottymeuk is on a distinguished road
Default

No point in 2 classes. Just have a login function

PHP Code:
function login($username,$password)
{
    
//DO LOGIN STUFF

Scottymeuk is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
Easy to Modify Login Script with Hierarchical User Permissions and XML Account File Wildhoney Script Giveaway 3 05-08-2009 03:22 PM
Please rip my code to pieces: generic user management class adamsargant Advanced PHP Programming 6 09-12-2008 11:22 AM
[Tutorial] Basic tutorial about class basics Tanax Absolute Beginners 14 07-24-2008 01:37 PM
PHP5 Classes A to Z Part 1 quantumkangaroo Advanced PHP Programming 11 04-01-2008 04:21 AM
Tutorial: PHP and OOP, a beginners guide Village Idiot Tips & Tricks 0 09-06-2007 04:23 PM


All times are GMT. The time now is 08:23 AM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design