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 12-29-2007, 02:16 PM   #1 (permalink)
Super Moderator
Inquisitive 
 
codefreek's Avatar
 
Join Date: Sep 2007
Location: Near you.
Posts: 791
Thanks: 241
codefreek is on a distinguished road
Help [FeedBack] Could use your help :)

Class_user.php

Hello, i have much to learn.
Could anyone please take the time and corect me.
with /*comment tags */ so i know what i did rong..


Thank you ;)




PHP Code:
<?php

class users {

    public 
$date
    
public $login
    
public $register
    
public $userpage /* Not done yet */
    
public $admin /* Not done yet */

public function logg(){
    print 
"This user was last logged in" $this->date;
          
SELECT * (`date` = '".($date)."')) from `user
            print 
'$date';
    }

}

public function 
userlogin(){

    
{
    
SELECT `idfrom user WHERE((md5(`username`)."')&&(`password` = '".md5($password)."')) 

if("
$username && $password == );
    print 
"you have accses to login";
                 
}else{
      print 
"You may not login";

  }

}

public function 
reg(){
    
    
{

        
$sql "INSERT INTO `".SUFFIX."user` SET `username` = '".$username."', `password` = '".md5($password)."', `email` = '".$email."', `usrlvl` = '0', `registered` = NOW()";


    }






?>

Last edited by codefreek : 12-29-2007 at 03:32 PM. Reason: take off the function in the function.. (sorry xD) (EDIT ONE MORE TIME xD)
codefreek is offline  
Reply With Quote
Old 12-29-2007, 02:54 PM   #2 (permalink)
The Prestige
Upcoming Programmer Inquisitive 
 
Tanax's Avatar
 
Join Date: Sep 2007
Location: Sweden, Stockholm
Posts: 1,080
Thanks: 115
Tanax is on a distinguished road
Default

On the register function, you have in the query that the username should be set to $username, but where is that variable set?

Also, you refer to $this->db when you're calling a query, but I don't see where you set the $db variable in the class..

Also, why do you have $this->register in the beginning of the reg function?
Tanax is offline  
Reply With Quote
The Following User Says Thank You to Tanax For This Useful Post:
codefreek (12-29-2007)
Old 12-29-2007, 03:17 PM   #3 (permalink)
Super Moderator
Inquisitive 
 
codefreek's Avatar
 
Join Date: Sep 2007
Location: Near you.
Posts: 791
Thanks: 241
codefreek is on a distinguished road
Default

tanax ty for your comment but could you explain more what you are asking me :P
hum.. oh right now i get it or do i :P ? "but where is that variable set?"
explain??

hum..
codefreek is offline  
Reply With Quote
Old 12-29-2007, 03:23 PM   #4 (permalink)
The Prestige
Upcoming Programmer Inquisitive 
 
Tanax's Avatar
 
Join Date: Sep 2007
Location: Sweden, Stockholm
Posts: 1,080
Thanks: 115
Tanax is on a distinguished road
Default

Well, your register function, could look something like this:

PHP Code:
public function reg($username$password$email) {

// Making the sql statement which would insert the values
$sql "INSERT INTO `".SUFFIX."user` SET `username` = '".$username."', `password` = '".md5($password)."', `email` = '".$email."', `usrlvl` = '0', `registered` = NOW()";

// Making a variable that executes the query when the variable is called.
$query mysql_query($sql);

// Here we call the variable and check if it's returned true at the same time.
if($query) {

// If the query returns true, then we return the function as true.
return true;

}

// If not, we return the function as false.
return false;


Tanax is offline  
Reply With Quote
The Following User Says Thank You to Tanax For This Useful Post:
codefreek (12-29-2007)
Old 12-29-2007, 03:28 PM   #5 (permalink)
Super Moderator
Inquisitive 
 
codefreek's Avatar
 
Join Date: Sep 2007
Location: Near you.
Posts: 791
Thanks: 241
codefreek is on a distinguished road
Default

hum. ok i get it
hum i will work on this..
thank you..
codefreek is offline  
Reply With Quote
Old 12-29-2007, 03:45 PM   #6 (permalink)
The Wanderer
 
deflated's Avatar
 
Join Date: Dec 2007
Location: 127.0.0.1
Posts: 19
Thanks: 7
deflated is on a distinguished road
Default

You're trying to access $this->db. I reckon that you were planning to write your own Database class. Your class is a mix of database access, output and access on user data. A class should not contain any HTML or access on superglobals like $_POST, $_GET, etc. for portability reasons. So it might be better to separate the output from the user interaction and the logic like it's done in MVC structures. SHA1 is more secure than MD5 so you should use sha1() instead of md5(). Here's how I would have realized it:

PHP Code:
class Users {
    private 
$db nulll ;
    
    
/**
     * Constructor of the class
     *
     * @param Database $db
     */
    
    
public function __construct Database $db ) {
        
$this->db $db ;
    }
    
    
/**
     * Returns the last login of a user
     *
     * @param integer $userId
     * @return integer    UNIX timestamp
     */
    
    
public function getLastLogin $userId ) {
        
$query $this->db->query 'SELECT last_login FROM `users` WHERE id = ?'$userId ) ;
        
$row $this->db->fetchRow $queryDatabase::FETCHMODE_NUM ) ;
        return 
$row ] ;
    }
    
    
/**
     * Checks if the username and the password are correct
     *
     * @param string $username
     * @param string $password
     * @return boolean
     */
    
    
public function login $username $password ) {
        
$query $this->db->query 'SELECT count(id) FROM user WHERE username = ? AND password = ?'$usernamesha1($password) ) ;
        
$row $this->db->fetchRow $queryDatabase::FETCHMODE_NUM ) ;
        return 
$row ] > ;
    }
    
    
/**
     * Registers an account
     *
     * @param string $username
     * @param string $password
     * @param string $eMail
     * @param integer $userLevel
     */
    
    
public function register $username $password $eMail $userLevel ) {
        
$data = array (
            
'username'   => $username ,
            
'password'   => sha1 $password ) ,
            
'email'      => $eMail ,
            
'user_level' => $userLevel
        
) ;
        
        
$this->db->insert $data'user' ) ;
    }

And here's the structure of the Database class. I haven't implemented the methods itself because there are enough resources on the internet about how to create a database class.

PHP Code:
class Database {
    const 
FETCHMODE_NUM   ;
    const 
FETCHMODE_ASSOC ;
    
//add more types...
    
    /**
     * Executes a SQL query
     *
     * @param string $sql
     * @param array $bindings
     */
    
    
public function query ($sql, array $bindings = array ( )) {
        
//...
    
}
    
    
/**
     * Gets a row
     *
     * @param resource $query
     * @param integer $fetchMode
     */
    
    
public function fetchRow $query $fetchMode self::FETCHMODE_ASSOC ) {
        
//...
    
}
    
    
/**
     * Inserts a row
     *
     * @param array $data
     * @param string $tableName
     */
    
    
public function insert ( array $data $tableName ) {
        
//...
    
}

Both class should be self-explanatory.

If you have any questions don't hesitate to ask. Thanks.

Last edited by deflated : 07-18-2010 at 01:42 PM.
deflated is offline  
Reply With Quote
The Following User Says Thank You to deflated For This Useful Post:
codefreek (12-29-2007)
Old 12-29-2007, 04:54 PM   #7 (permalink)
Super Moderator
Inquisitive 
 
codefreek's Avatar
 
Join Date: Sep 2007
Location: Near you.
Posts: 791
Thanks: 241
codefreek is on a distinguished road
Default

man you realy where a good helper on this ;) ty.
but i dont know i want to so bad learn and i just dont get it :S

but i wont give up not this time
its time to stop being lazy and start learning xD

so ty dude and i might just take up your offer on asking again ;)

as i said great help ty :D


ps: i knew about the sha1() but i forgot what it was called so i went with md5 sorry xD ;)

Last edited by codefreek : 12-29-2007 at 04:56 PM. Reason: edit sha1()
codefreek is offline  
Reply With Quote
Old 12-29-2007, 06:00 PM   #8 (permalink)
The Wanderer
 
deflated's Avatar
 
Join Date: Dec 2007
Location: 127.0.0.1
Posts: 19
Thanks: 7
deflated is on a distinguished road
Default

No problem.

There's a pretty cool blog by sunilbhatia79 which might support you with learning PHP:
Geek Files

Have fun!

Last edited by deflated : 07-18-2010 at 01:42 PM.
deflated is offline  
Reply With Quote
The Following User Says Thank You to deflated For This Useful Post:
codefreek (12-30-2007)
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


All times are GMT. The time now is 06:00 AM.

 
     

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