Social Networking Site: Improving of Login System using Advance PHP
Submitted by GeePee on Monday, April 20, 2015 - 23:50.
In this tutorial, we will focus on how to improve our PHP code in more dynamic ways using Object Oriented Programming. To start with this application, open first our web directories then look for the index.php file and make a copy of it, and name the old “index.php” into” index_basic.php”.
Then inside the includes folder, look for the “member.php” and open it because we’re going to add another static function that will be used during the user authentication. And I will name this function as “AuthenticateMember”, and this function will accept an argument such as email address and user password. And here’s the following code:
The code above will accept a two parameter based on the user input and will be passed into a query. Then, since we’re using an Object-Oriented programming, the result is an object not an array. Then all the results will be stored in our session variables. And finally, the result will return to true.
And here’s the whole code for “member.php”.
This time, let’s make use of our "member" class for user login. To do this, open the "index.php" file. And between the end of the head and start of the body tag, change all the code to this new bit of code:
And on the “home.php”, the “Logout" menu is found under the “Account” menu.And it look like as shown below:
And this time, we're going to create a new PHP file named “logout.php” and add the following code:
And here's all the code for "index.php" file.
If you want to see more of my works, new Source Code or Application and Tutorials Just click here.
- static function AuthenticateMember($email="", $h_upass=""){
- global $mydb;
- $res=$mydb->setQuery("SELECT * FROM `user_info` WHERE `email`='" . $email . "' and `pword`='" . $h_upass ."' LIMIT 1");
- $found_user = $mydb->loadSingleResult();
- $_SESSION['member_id'] = $found_user->member_id;
- $_SESSION['fName'] = $found_user->fName;
- $_SESSION['lName'] = $found_user->lName;
- $_SESSION['email'] = $found_user->email;
- $_SESSION['pword'] = $found_user->pword;
- $_SESSION['mm'] = $found_user->mm;
- $_SESSION['dd'] = $found_user->dd;
- $_SESSION['yy'] = $found_user->yy;
- $_SESSION['gender'] = $found_user->gender;
- return $found_user;
- }
- <?php
- /**
- * Description: This is a class for member.
- * Author: Joken Villanueva
- * Date Created: Nov. 2, 2013
- * Revised By:
- */
- require_once(LIB_PATH.DS.'database.php');
- class member {
- protected static $tbl_name = "user_info";
- function db_fields(){
- global $mydb;
- return $mydb->getFieldsOnOneTable(self::$tbl_name);
- }
- function listOfmembers(){
- global $mydb;
- $mydb->setQuery("Select * from ".self::$tbl_name);
- $cur = $mydb->loadResultList();
- return $cur;
- }
- static function AuthenticateMember($email="", $h_upass=""){
- global $mydb;
- $res=$mydb->setQuery("SELECT * FROM `user_info` WHERE `email`='" . $email . "' and `pword`='" . $h_upass ."' LIMIT 1");
- $found_user = $mydb->loadSingleResult();
- $_SESSION['member_id'] = $found_user->member_id;
- $_SESSION['fName'] = $found_user->fName;
- $_SESSION['lName'] = $found_user->lName;
- $_SESSION['email'] = $found_user->email;
- $_SESSION['pword'] = $found_user->pword;
- $_SESSION['mm'] = $found_user->mm;
- $_SESSION['dd'] = $found_user->dd;
- $_SESSION['yy'] = $found_user->yy;
- $_SESSION['gender'] = $found_user->gender;
- return $found_user;
- }
- static function bPrimary($id=0){
- global $mydb;
- $mydb->setQuery("SELECT * FROM ".self::$tbl_name." WHERE auto_id={$id} LIMIT 1");
- $row = $mydb->loadSingleResult();
- $s = $row->autostart + $row->incval;
- $a = $row->appenchar;
- return $a.$s;
- }
- static function bPrimaryUpdate($id=0){
- global $mydb;
- $mydb->setQuery("SELECT * FROM ".self::$tbl_name." WHERE auto_id={$id} LIMIT 1");
- $row = $mydb->loadSingleResult();
- $s = $row->autostart + $row->incval;
- return $s;
- }
- /*---Instantiation of Object dynamically---*/
- static function instantiate($record) {
- $object = new self;
- foreach($record as $attribute=>$value){
- if($object->has_attribute($attribute)) {
- $object->$attribute = $value;
- }
- }
- return $object;
- }
- /*--Cleaning the raw data before submitting to Database--*/
- private function has_attribute($attribute) {
- // We don't care about the value, we just want to know if the key exists
- // Will return true or false
- }
- protected function attributes() {
- // return an array of attribute names and their values
- global $mydb;
- foreach($this->db_fields() as $field) {
- if(property_exists($this, $field)) {
- $attributes[$field] = $this->$field;
- }
- }
- return $attributes;
- }
- protected function sanitized_attributes() {
- global $mydb;
- // sanitize the values before submitting
- // Note: does not alter the actual value of each attribute
- foreach($this->attributes() as $key => $value){
- $clean_attributes[$key] = $mydb->escape_value($value);
- }
- return $clean_attributes;
- }
- /*--Create,Update and Delete methods--*/
- public function save() {
- // A new record won't have an id yet.
- }
- public function create() {
- global $mydb;
- // Don't forget your SQL syntax and good habits:
- // - INSERT INTO table (key, key) VALUES ('value', 'value')
- // - single-quotes around all values
- // - escape all values to prevent SQL injection
- $attributes = $this->sanitized_attributes();
- $sql = "INSERT INTO ".self::$tbl_name." (";
- $sql .= ") VALUES ('";
- $sql .= "')";
- echo $mydb->setQuery($sql);
- if($mydb->executeQuery()) {
- $this->id = $mydb->insert_id();
- return true;
- } else {
- return false;
- }
- }
- public function update($id=0) {
- global $mydb;
- $attributes = $this->sanitized_attributes();
- foreach($attributes as $key => $value) {
- $attribute_pairs[] = "{$key}='{$value}'";
- }
- $sql = "UPDATE ".self::$tbl_name." SET ";
- $sql .= " WHERE auto_id=". $id;
- $mydb->setQuery($sql);
- if(!$mydb->executeQuery()) return false;
- }
- public function delete($id=0) {
- global $mydb;
- $sql = "DELETE FROM ".self::$tbl_name;
- $sql .= " WHERE auto_id=". $id;
- $sql .= " LIMIT 1 ";
- $mydb->setQuery($sql);
- if(!$mydb->executeQuery()) return false;
- }
- }
- ?>
- <?php
- //form has been submitted1
- //check if the email and password is equal to nothing or null then it will show message box
- if ($email == '') {
- ?> <script type="text/javascript">
- alert("Username or Password Not Registered! Contact Your administrator.");
- </script>
- <?php
- } elseif ($upass == '') {
- ?> <script type="text/javascript">
- alert("Username or Password Not Registered! Contact Your administrator.");
- </script>
- <?php
- } else {
- //it creates a new objects of member
- $member = new member();
- //Make use of the static function, and we passed two parameters
- $res = $member::AuthenticateMember($email, $h_upass);
- //then it check if the function return to true
- if ($res == true) {
- ?> <script type="text/javascript">
- //then it will be redirected to home.php
- window.location = "home.php";
- </script>
- <?php
- } else {
- ?> <script type="text/javascript">
- alert("Username or Password Not Registered! Contact Your administrator.");
- window.location = "home.php";
- </script>
- <?php
- }
- }
- } else {
- $email = "";
- $upass = "";
- }
- ?>
- <?php
- // Four steps to closing a session
- // (i.e. logging out)
- // 1. Find the session
- // 2. Unset all the session variables
- // 3. Destroy the session cookie
- }
- // 4. Destroy the session
- <script type="text/javascript">
- window.location = "index.php?logout=1";
- </script>
- <?php
- ?>
- <?php
- require_once("includes/initialize.php");
- ?>
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="utf-8">
- <meta content="width=device-width, initial-scale=1.0" name="viewport">
- <meta content="" name="description">
- <meta content="" name="author">
- <link href="#" rel="shortcut icon">
- <title>Philsocial</title><!-- Bootstrap core CSS -->
- <link href="css/bootstrap.css" rel="stylesheet"><!-- Custom styles for this template -->
- <link href="jumbotron.css" rel="stylesheet">
- <script type="text/javascript" src="js/registrationformValidation.js"> </script>
- <?php
- if (logged_in()) {
- ?>
- <script type="text/javascript">
- window.location = "home.php";
- </script>
- <?php
- }
- ?>
- </head>
- <?php
- //form has been submitted1
- //check if the email and password is equal to nothing or null then it will show message box
- if ($email == '') {
- ?> <script type="text/javascript">
- alert("Username or Password Not Registered! Contact Your administrator.");
- </script>
- <?php
- } elseif ($upass == '') {
- ?> <script type="text/javascript">
- alert("Username or Password Not Registered! Contact Your administrator.");
- </script>
- <?php
- } else {
- //it creates a new objects of member
- $member = new member();
- //make use of the static function, and we passed to parameters
- $res = $member::AuthenticateMember($email, $h_upass);
- //then it check if the function return to true
- if($res == true){
- ?> <script type="text/javascript">
- //then it will be redirected to home.php
- window.location = "home.php";
- </script>
- <?php
- } else {
- ?> <script type="text/javascript">
- alert("Username or Password Not Registered! Contact Your administrator.");
- window.location = "home.php";
- </script>
- <?php
- }
- }
- } else {
- $email = "";
- $upass = "";
- }
- ?>
- <body>
- <div class="navbar navbar-inverse navbar-fixed-top">
- <div class="container">
- <div class="navbar-header">
- <button class="navbar-toggle" data-target=".navbar-collapse" data-toggle="collapse" type=
- "button"><span class="icon-bar"></span> <span class="icon-bar"></span> <span class=
- "icon-bar"></span></button> <a class="navbar-brand" href="#" style=
- "font-weight: bold">Philsocial</a>
- </div>
- <div class="navbar-collapse collapse">
- <form class="navbar-form navbar-right" method="POST" action="index.php">
- <div class="form-group">
- <input type="text" placeholder="Email" class="form-control" name="log_email">
- </div>
- <div class="form-group">
- <input type="password" placeholder="Password" class="form-control" name="log_pword">
- </div>
- <button type="submit" class="btn btn-success" name="btnlogin">Sign in</button>
- </form>
- </div><!--/.navbar-collapse -->
- </div>
- </div><!-- Main jumbotron for a primary marketing message or call to action -->
- <div class="container">
- <div class="rows">
- <div class="col-xs-6">
- <h3>Philsocial helps you connected and share with the other people in your
- life</h3><img src="img/background.png" width="500px"></div>
- <div class="col-xs-6">
- <!--action="register.php" onsubmit="return checkRegistration();"-->
- <form action="register.php" class="form-horizontal" id="register" method="post" onSubmit="return checkRegistration();" >
- <fieldset>
- <legend>Sign Up</legend>
- <h4>It’s free and always will be.</h4>
- <div class="rows">
- <div class="col-xs-12">
- <div class="form-group">
- <div class="rows">
- <div class="col-md-12">
- <div class="col-lg-6" id="divfname">
- <input class="form-control input-lg" id="fName" name="fName" placeholder=
- "First Name" type="text" >
- </div>
- <div class="col-lg-6">
- <input class="form-control input-lg" id="lName" name="lName" placeholder=
- "Last Name" type="text">
- </div>
- </div>
- </div>
- </div>
- <div class="form-group" id="divemail">
- <div class="rows">
- <div class="col-md-12">
- <div class="col-lg-12">
- <input class="form-control input-lg" id="email" name="email"
- placeholder="Your Email" type="text" onblur="checkEmail();">
- </div>
- </div>
- </div>
- </div>
- <div class="form-group" id="divremail">
- <div class="rows">
- <div class="col-md-12">
- <div class="col-lg-12">
- <input class="form-control input-lg" id="reemail" name="reemail"
- placeholder="Re-enter Email" type="text" onblur="checkEmail2();">
- </div>
- </div>
- </div>
- </div>
- <div class="form-group" id="divpass">
- <div class="rows">
- <div class="col-md-12">
- <div class="col-lg-12">
- <input class="form-control input-lg" id="password" name="password"
- placeholder="New Password" type="password">
- </div>
- </div>
- </div>
- </div>
- <div class="form-inline">
- <div class="rows">
- <div class="col-md-12">
- <div class="col-md-3">
- <label>Birthday</label>
- </div>
- <div class="col-lg-3">
- <select class="form-control input-sm" name="month" id="month">
- <option value="">Month</option>
- <?php
- foreach ($m as $month) {
- echo '<option value='.$month.'>'.$month.'</option>';
- }
- ?>
- </select>
- </div>
- <div class="col-lg-3">
- <select class="form-control input-sm" name="day" id="day">
- <option value="">Day</option>
- <?php
- foreach ($d as $day) {
- echo '<option value='.$day.'>'.$day.'</option>';
- }
- ?>
- </select>
- </div>
- <div class="col-lg-3">
- <select class="form-control input-sm" name="yr" id="yr">
- <option value="">Year</option>
- <?php
- foreach ($years as $yr) {
- echo '<option value='.$yr.'>'.$yr.'</option>';
- }
- ?>
- </select>
- </div>
- </div>
- </div>
- </div>
- <div class="form-group">
- <div class="rows">
- <div class="col-md-12" style="text-align: left">
- <div class="col-lg-3">
- <div class="radio">
- <label><input checked id="optionsRadios1" name="gender" type=
- "radio" value="Female">Female</label>
- </div>
- </div>
- <div class="col-lg-3">
- <div class="radio">
- <label><input id="optionsRadios2" name="gender" type="radio"
- value="Male"> Male</label>
- </div>
- </div>
- </div>
- </div>
- </div>
- <div class="form-inline">
- <div class="rows">
- <div class="col-md-12">
- <p> By clicking Sign Up, you agree to our Terms and that you have
- read our Data Use Policy, including our Cookie Use.</p>
- </div>
- </div>
- </div>
- <div class="form-group">
- <div class="rows">
- <div class="col-md-8">
- <div class="col-lg-12">
- <button class="btn btn-success btn-lg" type="submit" name="Submit">Sign Up</button>
- </div>
- </div>
- </div>
- </div>
- </div>
- </div>
- </fieldset>
- </form>
- </div>
- </div><!--rows-->
- </div><!--container-->
- <hr>
- <footer>
- <p style="text-align: center">© Philsocial 2013</p>
- </footer><!-- /container -->
- <!-- Bootstrap core JavaScript
- ================================================== -->
- <!-- Placed at the end of the document so the pages load faster -->
- <script src="assets/js/jquery.js"></script>
- <script src="js/bootstrap.min.js"></script>
- </body>
- </html>
Comments
Add new comment
- Add new comment
- 125 views