TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   Absolute Beginners (http://www.talkphp.com/absolute-beginners/)
-   -   OOP - User System (http://www.talkphp.com/absolute-beginners/3660-oop-user-system.html)

zxt3st 11-27-2008 08:41 AM

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

Tanax 11-27-2008 09:37 AM

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!

zxt3st 11-27-2008 10:16 AM

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 :)

Tanax 11-27-2008 10:56 AM

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!

zxt3st 11-27-2008 11:19 AM

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?

Tanax 11-27-2008 12:03 PM

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.

zxt3st 11-27-2008 11:24 PM

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.

zxt3st 11-27-2008 11:42 PM

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.

Enfernikus 11-27-2008 11:59 PM

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.

Tanax 11-28-2008 12:04 AM

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

xenon 11-28-2008 12:10 AM

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).

zxt3st 11-28-2008 01:03 AM

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.

zxt3st 11-28-2008 01:46 AM

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

Tanax 11-28-2008 07:54 AM

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!

zxt3st 11-28-2008 08:30 AM

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

Tanax 11-28-2008 10:35 AM

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.

zxt3st 11-28-2008 10:50 AM

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++ :)

Tanax 11-28-2008 10:54 AM

Glad to help!
Just post if you need more help :-)

Enfernikus 11-28-2008 03:17 PM

Quote:

Originally Posted by xenon (Post 19956)
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.

zxt3st 11-28-2008 11:28 PM

Thanks bud :), you have a point there, but im testing it locally for the moment. Learning purposes only :)


All times are GMT. The time now is 08:57 PM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0