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 11-27-2008, 08:41 AM   #1 (permalink)
The Addict
 
zxt3st's Avatar
 
Join Date: Apr 2008
Posts: 200
Thanks: 18
zxt3st is on a distinguished road
Default OOP - User System

Greetings!

In the past 2 days, I am just reading the basics of OO-PHP for i find it good and comfortable to use in doing stuff than the old-fashioned way. And just this day (thanks God), I have done lots of test and sample codes about the basics on how the OO-PHP goes around and somehow got the basic/simple concepts about it.

In regards to the new concept I have learned/understand, I wanted to do something out of it. I have decided to create an OO based User System (a basic/secured one). Its a user system that can be able to login and register users with no any other etc. features yet.

What on my mind right now, is to create pages likely like these below:
Code:
index.php #main page
register.php #registration page
login.php #login/auth page
I need suggestions/comments about how Im going to do things first as my start in my journey in this OOP - PHP.


Thanks,
t3st
__________________
Serenity Project - 5% (Layout) - Ongoing....
Project Serenity Free Life!....
zxt3st is offline  
Reply With Quote
Old 11-27-2008, 09:37 AM   #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

The first thing I would do, is to create a DB class that manages all the querys and all that.

After you got your DB class, you would begin creating a config file, where you start all your classes, so you create for example $db = new yourdbclass('params');

Then you create a index.php, include your config, and run it. If it displays blank, then you're good to go.

Start creating your user class. There's alot of ways to do a user class, but I would generally do functions in the order they're used:
function to
- check if username and password is correct
- login the user
- logout the user
- register a user
- etc

There's no real "guidelines" on how to do this. But just take it easy and whenever you hit a problem, systematicly errorcheck your script on what could be wrong.

Good luck!
__________________
Tanax is offline  
Reply With Quote
The Following User Says Thank You to Tanax For This Useful Post:
zxt3st (11-27-2008)
Old 11-27-2008, 10:16 AM   #3 (permalink)
The Addict
 
zxt3st's Avatar
 
Join Date: Apr 2008
Posts: 200
Thanks: 18
zxt3st is on a distinguished road
Default

Thank you about it Tanax, it really enlighten me a bit on how should i start this so-called project that i will be doing.

Any comments/suggestions are more welcome :)
__________________
Serenity Project - 5% (Layout) - Ongoing....
Project Serenity Free Life!....
zxt3st is offline  
Reply With Quote
Old 11-27-2008, 10:56 AM   #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

I just thought of something else, that you would take great advantage of.

When you create your user class, in your construct, or in a setdb function, set a DB variable, something like this:

PHP Code:
class user
{
private 
$db;

public function 
__construct($db)
{

if(
is_object($db)
{

$this->db $db;

}

else throw new 
exception('Database must be an object!');

}


and:

PHP Code:
$db = new yourdbclass('params');
$user = new user($db); 
I thought that this was pretty obvious, but I think that maybe someone doesn't know about it.

Anyways, what this does is that you can use the DB functions inside your user class, so whenever you query, you just do:
PHP Code:
$query $this->db->queryfunction($sql); 
Both looks good, and it's easy!
__________________
Tanax is offline  
Reply With Quote
The Following User Says Thank You to Tanax For This Useful Post:
zxt3st (11-27-2008)
Old 11-27-2008, 11:19 AM   #5 (permalink)
The Addict
 
zxt3st's Avatar
 
Join Date: Apr 2008
Posts: 200
Thanks: 18
zxt3st is on a distinguished road
Default

great!, thats pretty useful, im having a great readings today coz i wonder what will be the best thing to implement for me to set standards though.

Should i be having an interface in a class?
__________________
Serenity Project - 5% (Layout) - Ongoing....
Project Serenity Free Life!....
zxt3st is offline  
Reply With Quote
Old 11-27-2008, 12:03 PM   #6 (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

I would say no. Not really neccessary for you to build an interface. But you can do whatever you want. If I put it like this: It won't really be any difference right NOW if you implement an interface, or not.
__________________
Tanax is offline  
Reply With Quote
Old 11-27-2008, 11:24 PM   #7 (permalink)
The Addict
 
zxt3st's Avatar
 
Join Date: Apr 2008
Posts: 200
Thanks: 18
zxt3st is on a distinguished road
Default

So far, this are still the things i have done. My codes are below, and any critiques about it are very much welcome for me to improve.

sample_db.php
PHP Code:
<?php

class DBMysql
{
    var 
$query;
    
    public function 
__construct($db_host$db_user$db_pass)
    {
        
$this->dbhost $db_host;
        
$this->dbuser $db_user;
        
$this->dbpass $db_pass;
    }
    
    public function 
connect()
    {
        
$dblink = @mysql_connect($this->dbhost$this->dbuser$this->dbpass);
        if (
$dblink)
        {
            echo 
"success";
        }
        else
        {
            die(
'Mysql Error!.');
        }
    }
    
    public function 
do_query($dbquery)
    {
        
$result = @mysql_query($this->query);
        if (
$result)
        {
            echo 
"Successfull!";
        }
        else
        {
            throw new 
exception('Error executing query!.');
        }
    }
    
    public function 
disconnect()
    {
        
//mysql close here
    
}
}

?>
index.php
PHP Code:
<?php
error_reporting
(E_ALL);
include(
'sample_db.php');
include(
'dbconfig.php');

$connection = new DBMysql($at_Mhost$at_Muser$at_Mpass);
$connection->connect();

$sql "Create table aldin";

?>
dbconfig.php
PHP Code:
<?php

$at_Mhost 
'localhost';
$at_Muser 'root';
$at_Mpass '';

?>
I got problem on the query part, though im reading more on stuff, implementing a mysql class.

Feedback?

Thnx.
__________________
Serenity Project - 5% (Layout) - Ongoing....
Project Serenity Free Life!....
zxt3st is offline  
Reply With Quote
Old 11-27-2008, 11:42 PM   #8 (permalink)
The Addict
 
zxt3st's Avatar
 
Join Date: Apr 2008
Posts: 200
Thanks: 18
zxt3st is on a distinguished road
Default

Updates:

sample_db.php

PHP Code:
<?php

class DBMysql
{
    var 
$query;
    
    
/* public function __construct($db_host, $db_user, $db_pass)
    {
        $this->dbhost = $db_host;
        $this->dbuser = $db_user;
        $this->dbpass = $db_pass;
    } */
    
    
public function do_connect($db_host$db_user$db_pass)
    {
        
$dblink = @mysql_connect($this->$db_host$this->$db_user$this->$db_pass);
        if (
$dblink)
        {
            echo 
"success";
        }
        else
        {
            throw new 
exception('Error connecting to the database!.');
        }
    }
    
    public function 
do_query($dbquery)
    {
        
$result = @mysql_query($this->$dbquery);
        if (
$result)
        {
            echo 
"Successfull!";
        }
        else
        {
            throw new 
exception('Error executing query!.');
        }
        return 
$result;
    }
    
    public function 
disconnect()
    {
        @
mysql_close($this->connection);
    }
}

?>


index.php
PHP Code:
<?php
#error_reporting(E_ALL);
include('sample_db.php');
include(
'dbconfig.php');

$db = new DBMysql();

$connection $db->do_connect($at_Mhost$at_Muser$at_Mpass);
$sql "create table aldin";
$query $db->do_query($sql);

?>
dbconfig.php
Code:
nothing change
I have modified a little on the codes just to see if my class are working well. And now as what you can see on the query im creating the database, and when i check it there is no error, but still i can't create the table i want.

Im still messing around :) any feedback is greatly appreciated.

Thnx.
__________________
Serenity Project - 5% (Layout) - Ongoing....
Project Serenity Free Life!....
zxt3st is offline  
Reply With Quote
Old 11-27-2008, 11:59 PM   #9 (permalink)
The Addict
 
Enfernikus's Avatar
 
Join Date: Jun 2008
Posts: 335
Thanks: 2
Enfernikus is on a distinguished road
Default

var is depreciated in PHP5, use public/private/protected and if your testing on a host they oft won't let tables be created via scripts.
Enfernikus is offline  
Reply With Quote
The Following User Says Thank You to Enfernikus For This Useful Post:
zxt3st (11-28-2008)
Old 11-28-2008, 12:04 AM   #10 (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

You don't set $this->dbquery anywhere.
You haven't even defined $dbquery anywhere.
You defined $query, which isn't used anywhere.
You used var $query, instead of private $query, which would be the most optimal choice.
When you disconnect, you disconnect $this->connection, which isn't set anywhere either, NOR defined.
You return your query results, even if it failed.

And lastly, I don't think you should echo ANYTHING AT ALL in your class. Classes are just a way of managing your data. Return DATA instead, and process the data in your controller file(in this case - index.php).

PHP Code:
if($query// meaning if it returned true
{

echo 
'Query successful!';

}

else
{

echo 
'Query failed..';


Just my opinion
__________________
Tanax is offline  
Reply With Quote
The Following User Says Thank You to Tanax For This Useful Post:
zxt3st (11-28-2008)
Old 11-28-2008, 12:10 AM   #11 (permalink)
The Frequenter
Newcomer 
 
xenon's Avatar
 
Join Date: Dec 2007
Location: Bucharest, Romania
Posts: 438
Thanks: 3
xenon is on a distinguished road
Default

Drop the "var" keyword. It's deprecated as of PHP 5.0.0.

Now, about the problem with the table creation. Tables are assigned to databases. Therefore, you need to actually select a database before trying to create a table. In your do_connect method, after mysql_connect, you need to add another line:

PHP Code:
mysql_select_db($dbname); 
Where $dbname is perhaps another parameter passed to the method. Also, you can easily remove the prefixes from the methods names. do_connect would become connect, do_query would become query and so on. That way you can easily remember the names later, while writing less code.

Also, try to add class properties (..or class variables, as some would call them) as you need them, not randomly across the code. And please, oh please, stop suppressing errors using the error suppression operator (@). Use error_reporting(E_ALL) combined with ini_set('display_errors', 'on') when debugging, and the opposite when going live. You MIGHT need to debug some problem later and you will not be able to tell what caused that problem, if you suppress all the errors/notices/warnings, what ever.

Enfernikus: creating tables cannot be enforced on that level. You can, or you can't create tables. End of story. It doesn't matter if you run a query in phpmyadmin or if you run your own php file that creates it. As long as a "create table" privilege is given on the user you're using to connect to the database, creating tables can be done from anywhere on that server (and in some cases, remotely - where the server accepts remote connections - not really the case with mysql).
__________________
I have optimistic thoughts, even though sometimes (if not always) life's a bitch.
xenon is offline  
Reply With Quote
The Following User Says Thank You to xenon For This Useful Post:
zxt3st (11-28-2008)
Old 11-28-2008, 01:03 AM   #12 (permalink)
The Addict
 
zxt3st's Avatar
 
Join Date: Apr 2008
Posts: 200
Thanks: 18
zxt3st is on a distinguished road
Default

o.O thanks for the replies, i just notice that im pretty focused on something, that i didn't notice im did create a table though what im thinking is that im planning to create a sample db. whew!

update code will be posted later, i need to modify it now.

thank you.
__________________
Serenity Project - 5% (Layout) - Ongoing....
Project Serenity Free Life!....
zxt3st is offline  
Reply With Quote
Old 11-28-2008, 01:46 AM   #13 (permalink)
The Addict
 
zxt3st's Avatar
 
Join Date: Apr 2008
Posts: 200
Thanks: 18
zxt3st is on a distinguished road
Default

Yes, i have done updating my codes, and i have choose to use an interface for my class.

db.php - interface
PHP Code:
<?php

interface db
{
    
    public function 
connect();
    public function 
query($query);
    public function 
close();
}

?>
mysql.class.php - mysql classes/functions
PHP Code:
<?php

require "includes/db.php";

class 
dbmysql implements db
{
    private 
$link;
    
    public function 
connect($dbhost=''$dbuser=''$dbpass='')
    {
        
$this->link mysql_connect($dbhost$dbuser$dbpass);
        
    }
    
    public function 
query($query)
    {
        return 
mysql_query($query$this->link);
    }
    
    public function 
close()
    {
        return 
mysql_close($this->link);
    }
}

?>
index.php
PHP Code:
<?php

include('includes/mysql.class.php');
include(
'includes/dbconfig.php');

$at_Mdb = new dbmysql();
$at_Mdb->connect($s_host$s_user$s_pass);
$sql "create database $s_dbname";
$query $at_Mdb->query($sql);
$at_Mdb->close();


?>
dbconfig.php
PHP Code:
<?php

$s_host 
'localhost';
$s_user 'root';
$s_pass '';
$s_dbname 'usys';

?>
i got these running and fully working, and so far i think im good to start it now :) Thanks for all the comments and suggestions. You help me a lot understand more about these things.

Any more comments and suggestions are welcome to help me visualize and improvise which is really better for usability of codes :)

Regards,
t3st
__________________
Serenity Project - 5% (Layout) - Ongoing....
Project Serenity Free Life!....
zxt3st is offline  
Reply With Quote
Old 11-28-2008, 07:54 AM   #14 (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

Mm better
You could go for a fetch function which would fetch an array?
Also you could design a secure function which would secure the specified string from mysql injections, and then return the secured string?

Just some from the top of my head!
__________________
Tanax is offline  
Reply With Quote
The Following User Says Thank You to Tanax For This Useful Post:
zxt3st (11-28-2008)
Old 11-28-2008, 08:30 AM   #15 (permalink)
The Addict
 
zxt3st's Avatar
 
Join Date: Apr 2008
Posts: 200
Thanks: 18
zxt3st is on a distinguished road
Default

Thanks Tanax, im adding it up to the list of functions that i will be needing for the project.
PHP Code:
public function real_escape_string($string);
public function 
stripslashes($string);
public function 
fetch_array($string); 
Btw, as what i put on the parameters on my 3 functions above, is it possible/okay to have it the same variable name to all the three functions? Coz im confuse, since i dont declare those variables inside the class itself but there are just mere parameter(s) within methods right? So it come to my mind, that maybe its possible. What do you guyz think?


All the best,
t3st
__________________
Serenity Project - 5% (Layout) - Ongoing....
Project Serenity Free Life!....
zxt3st is offline  
Reply With Quote
Old 11-28-2008, 10:35 AM   #16 (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

Yes you can use the same variable name on all functions.
However, there already is a function called "stripslashes" for example, and you cannot use the same name as an already existing function. I would suggest you to "merge" the 2 functions to a function called something like "secure".

I also thought about something in your updated code. You don't actually check if the connection succeeded, so I would suggest you to do something like
PHP Code:
if($this->link//meaning the connection returned true..
{
return 
true;
}
else return 
false
and put that in your connect function for a better structure and errorclearing.
__________________
Tanax is offline  
Reply With Quote
The Following User Says Thank You to Tanax For This Useful Post:
zxt3st (11-28-2008)
Old 11-28-2008, 10:50 AM   #17 (permalink)
The Addict
 
zxt3st's Avatar
 
Join Date: Apr 2008
Posts: 200
Thanks: 18
zxt3st is on a distinguished road
Default

So thats it, thank you very much Tanax :) I really appreciate it and you guyz really help me a lot. I got a better understanding now in OO than the last day :)

Time for my hands to take on some notepad++ :)
__________________
Serenity Project - 5% (Layout) - Ongoing....
Project Serenity Free Life!....
zxt3st is offline  
Reply With Quote
Old 11-28-2008, 10:54 AM   #18 (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

Glad to help!
Just post if you need more help
__________________
Tanax is offline  
Reply With Quote
Old 11-28-2008, 03:17 PM   #19 (permalink)
The Addict
 
Enfernikus's Avatar
 
Join Date: Jun 2008
Posts: 335
Thanks: 2
Enfernikus is on a distinguished road
Default

Quote:
Originally Posted by xenon View Post
Drop the "var" keyword. It's deprecated as of PHP 5.0.0.

Now, about the problem with the table creation. Tables are assigned to databases. Therefore, you need to actually select a database before trying to create a table. In your do_connect method, after mysql_connect, you need to add another line:

PHP Code:
mysql_select_db($dbname); 
Where $dbname is perhaps another parameter passed to the method. Also, you can easily remove the prefixes from the methods names. do_connect would become connect, do_query would become query and so on. That way you can easily remember the names later, while writing less code.

Also, try to add class properties (..or class variables, as some would call them) as you need them, not randomly across the code. And please, oh please, stop suppressing errors using the error suppression operator (@). Use error_reporting(E_ALL) combined with ini_set('display_errors', 'on') when debugging, and the opposite when going live. You MIGHT need to debug some problem later and you will not be able to tell what caused that problem, if you suppress all the errors/notices/warnings, what ever.

Enfernikus: creating tables cannot be enforced on that level. You can, or you can't create tables. End of story. It doesn't matter if you run a query in phpmyadmin or if you run your own php file that creates it. As long as a "create table" privilege is given on the user you're using to connect to the database, creating tables can be done from anywhere on that server (and in some cases, remotely - where the server accepts remote connections - not really the case with mysql).
I think I wrote my previous post in a mistaken fashion, what I meant was the accounts that you ordinarily make via web host control panel by default don't have the privilege to make tables such as Dreamhost.
Enfernikus is offline  
Reply With Quote
Old 11-28-2008, 11:28 PM   #20 (permalink)
The Addict
 
zxt3st's Avatar
 
Join Date: Apr 2008
Posts: 200
Thanks: 18
zxt3st is on a distinguished road
Default

Thanks bud :), you have a point there, but im testing it locally for the moment. Learning purposes only :)
__________________
Serenity Project - 5% (Layout) - Ongoing....
Project Serenity Free Life!....
zxt3st 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 4 05-04-2011 06:11 AM
Help: Php User Messenger system with ajax tego10122 Advanced PHP Programming 2 10-01-2008 11:42 AM
User profile page h0ly lag General 2 05-08-2008 08:53 PM
Highly secure login system ReSpawN Advanced PHP Programming 6 12-12-2007 08:28 PM
User system Swordbeta Script Giveaway 4 12-04-2007 10:45 PM


All times are GMT. The time now is 05:52 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