..


Sponsored Links

PHP / OOP: Create a system of registration and authentication for users

Article written by Claudio Garau
Page 1 of 5

From the mail I receive and discussions on the forums I follow, still known to some difficulty for some PHP developers in the paradigm shift from procedural to object-oriented programming, in fact I think the basis of everything there is only a matter of habits , OOP is not as difficult as it seems and most of those who would use it already have, often without knowing it, the technical tools to transform it into a means to build their own applications.

To demonstrate what I have just argued, this guide will present the steps needed to create a system of registration and authentication based on object-oriented programming will be an opportunity to show very simply as classes, methods, properties and, in fact, objects are not necessarily intended for use in complex applications.
Our script will use the MySQL DBMS for storing information, and then as a first step we create for ourselves a database and give it a name such as "registration" within it will have a table which, for convenience of the reader, carry SQL dump ready for import:

 



 CREATE TABLE IF NOT EXISTS `members` (



  



 `User_id` int (4) NOT NULL AUTO_INCREMENT,



  



 `User_name` varchar (20) DEFAULT NULL,



  



 `Password` varchar (40) DEFAULT NULL,



  



 Real_name `` varchar (50) DEFAULT NULL,



  



 `Email` varchar (80) DEFAULT NULL,



  



 PRIMARY KEY (`user_id`),



  



 UNIQUE KEY `username` (`user_name`),



  



 UNIQUE KEY `email` (`email`)







 ) ENGINE = MyISAM DEFAULT CHARSET = latin1 AUTO_INCREMENT = 1;



The table "members" will be composed of 5 fields:

  • user_id: to house the unique identifier for each record;
  • username: the username of each user record;
  • Password: allow every registered user to login to the page type;
  • real_name : The real name of the user, may also be different from the username;
  • email: e-mail address of the member.

But how will it be structured our application? We will have a first configuration file, for example, we'll call him "config.php", which will allow us to interact with the DBMS to connect to it and selecting the database you created earlier, we see:

 



 <? Php







 # Define constants for authentication to the DBMS







 define ('DATA_HOST', 'localhost');







 define ('DATA_UTENTE', 'root');







 define ('DATA_PASS','');







 define ('DATA_DB', 'Registration');







 # Class for interacting with the database







 {class DATA_Class



  



 # Definition of manufacturer



  



 function __construct () {



    



 # Connect to the DBMS



    



 $ Link = @ mysql_connect (DATA_HOST, DATA_UTENTE, DATA_PASS) or die ('Error in connection:'. Mysql_error ());



    



 # Select the database



    



 @ Mysql_select_db (DATA_DB, $ connection) or die ('Error from the database:'. Mysql_error ());



  



 }







 }







 ?>

 

Nothing particularly complex, the file "config.php" will in practice:

  • define the constants that contain the information necessary for connection;
  • define a class that will handle the interaction with MySQL;
  • connect to the DBMS and select the data base of reference.

As you can see, the class will be introduced to simple declaration, to define it just give it a name:

 



 DATA_Class class {..

 

Within the class, delimited by curly brackets, there will be a special method, called "builder", defined as follows:

 



 function __construct () {..

 

The manufacturer is in practice a method associated with a class that is responsible for putting in place the instance of the class and initialize it to create an object, a call to the constructor is done automatically when it is generated a new object of a class.
A concept too complicated? Only apparently, in fact, the manufacturer proposed code defines the code needed to connect to MySQL and select the database, which means that each instance of the class will correspond to an automatic call the manufacturer, who will perform these tasks.
Now that the framework will be easier you can take the next step, namely the creation of the file containing the other methods to be used in our application.

In the same category ...
E-Learning
Linux (Course) Linux (Course)
Complete guide to open-source system. From 49 €.
MySQL (Course) MySQL (Course)
Management of open-source database. From 39 €.
PHP (Course) PHP (Course)
Full course for creating dynamic Web sites. From 49 €.
Sponsored Links