User Registration and Login using PHP Oops Concepts | PHP Gurukul
In this tutorial we will learn how user can register and login using PHP Oops concepts.
File Structure for this tutorial :
- index.php (User Registration form)
- sigin.php (User signin form)
- function.php (Database connection and function for username availability, registration, and login)
- welcome.php (After login user will redirect to this page)
- logout.php (for logout)
MySQL table tblusers structure used in this tutorial
1 2 3 4 5 6 7 8 | CREATE TABLE `tblusers` ( `id` int(11) NOT NULL, `FullName` varchar(120) DEFAULT NULL, `Username` varchar(120) DEFAULT NULL, `UserEmail` varchar(200) DEFAULT NULL, `Password` varchar(250) DEFAULT NULL, `RegDate` timestamp NULL DEFAULT CURRENT_TIMESTAMP ) ENGINE=InnoDB DEFAULT CHARSET=latin1; |
Now creates a function.php. In this file class Class DB_con, Inside this class define the database connection.Now inside this create your function for username availability, user registration and login.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | <?php define('DB_SERVER','localhost'); define('DB_USER','root'); define('DB_PASS' ,''); define('DB_NAME', 'userdb'); class DB_con { function __construct() { $con = mysqli_connect(DB_SERVER,DB_USER,DB_PASS,DB_NAME); $this->dbh=$con; // Check connection if (mysqli_connect_errno()) { echo "Failed to connect to MySQL: " . mysqli_connect_error(); } } // for username availblty public function usernameavailblty($uname) { $result =mysqli_query($this->dbh,"SELECT Username FROM tblusers WHERE Username='$uname'"); return $result; } // Function for registration public function registration($fname,$uname,$uemail,$pasword) { $ret=mysqli_query($this->dbh,"insert into tblusers(FullName,Username,UserEmail,Password) values('$fname','$uname','$uemail','$pasword')"); return $ret; } // Function for signin public function signin($uname,$pasword) { $result=mysqli_query($this->dbh,"select id,FullName from tblusers where Username='$uname' and Password='$pasword'"); return $result; } } ?> |
DB_con is the constructor function that will create localhost connection and database selection.
public function usernameavailblty () function has one parameter which will check the username availability.
public function registration() function have some parameter which will accept inputs from the HTML registration form.
public function signin() function will check the user username, the password is valid or not.
PHP Gurukul
Welcome to PHPGurukul. We are a web development team striving our best to provide you with an unusual experience with PHP. Some technologies never fade, and PHP is one of them. From the time it has been introduced, the demand for PHP Projects and PHP developers is growing since 1994. We are here to make your PHP journey more exciting and useful.
Website : https://phpgurukul.com
Comments
Post a Comment