PHP Classes

File: Example.txt

Recommend this page to a friend!
  Classes of abubakkar siddiq   The Login Class   Example.txt   Download  
File: Example.txt
Role: Documentation
Content type: text/plain
Description: Example File For Class
Class: The Login Class
Autenticate users with records in a MySQL database
Author: By
Last change:
Date: 16 years ago
Size: 5,678 bytes


Class file image Download
Hello coders, This is my first attempt to post class in this recognised site.This is a simple class which is what i am using in my all commrecial site for user management and user login purpose.I will give you the clear idea of how to use this class.This file consist of class Login(login_class.php) and example text file(the same file).First of all,if you want to use this class,you should include this file using either require or include functions.For example, require("login_class.php"); or include("login_class.php"); This class files has information to initialize session and session,put include or require at the top of any file.i.e First line of the file coding should be above code. After including this file,you are free to create object from the class "Login".here it is the syntax to create object from a class. obj_var_name=new class_name; It is the simple syntax to create object from a class given class name.We will look into our class "Login".Just include this code in your file to create object for our CLASS "Login". login=new Login; For some reason ,I dont include constructor for this class.There are two function namely db and verify. Function db is used to make a connection to database.Function verify is used to retrive the password from databese and check with entered password.Function db takes arguments i listed below. Your Host Name-Name of the host (A string value). username-your username to make connection to database(A string value). password-password which you would use to connect to database.(A string value). Your database Name-your database name(A string value). with these informations,Now it's time to call db function with our object.isnt ? login->db("localhost","root","root","user"); this coding simply setup a connection to database and make connection handler available,if you want connection handler for that database connection,do this. global $conn; if you include this line,database connection handler will be availble thorugh out that block.this handler can be used in some of the function.for example,function such as mysql_affected_rows() need this connection handler to return the number of affected rows. next step is to call our verify function to verify the password.this function takes arguments listed below. username-username to verified with your database(string) password-password to be verified against username.(string) table name-table name where you stored the username and password.(string) encrption detail-it should be specified if you encrpted password while storing it in the database. (integer).either 1 or 0. 1 for encrpted password and 0 for non encrpted password.By default it is set to if you stored password in normal format in table,dont include this argument. login->verify("abu1882","password","user",0); //non encrpted password or login->verify("abu1882","password","user"); //non encrpted password with default argument 0. login->verify("abu1882","password","user",1); //to retrive encrpted password(md5 encrption) This function will return an integer value based on the result of operation it performs on the data.For example,it will return 0,if that username is not in the database(wrong username or not registered) It will return 1,if that username is correct and match with entered password and return 2,if password is incorrect. We can collect the returned value and then we can display proper error message based on that.for example, $return=login->verify("abu1882","password","user"); switch($return) { case 0: echo "username is not registered till"; break; case 1: echo "welcome".$_SESSION['username']; break; case 2: echo "password incorrect"; break; } As in the case 1 of above example,this class will register a session variable with value of username in it.Here it is full code example. <?PHP include("login_class.php"); $username=$_POST{'username']; $password=$_POST['password']; login=new Login; login->db("localhost","root","root","user"); $return=login->verify($username,$password,"user",1);//password is encrpted //using md5() switch($return) { case 0: echo "username is not registered till"; break; case 1: echo "welcome".$_SESSION['username']; break; case 2: echo "password incorrect"; break; } ?> That's you soon again with my next class.bye now.