Accéder au contenu principal

PHP MySQL login

This tutorial demonstrates how to create a login page with MySQL Data base. Before enter into the code part, You would need special privileges to create or to delete a MySQL database. So assuming you have access to root user, you can create any database using mysql mysqladmin binary.

Config.php

Config.php file is having information about MySQL Data base configuration.
<?php
   define('DB_SERVER', 'localhost:3036');
   define('DB_USERNAME', 'root');
   define('DB_PASSWORD', 'rootpassword');
   define('DB_DATABASE', 'database');
   $db = mysqli_connect(DB_SERVER,DB_USERNAME,DB_PASSWORD,DB_DATABASE);
?>

Login.php

Login PHP is having information about php script and HTML script to do login.
<?php
   include("config.php");
   session_start();
   
   if($_SERVER["REQUEST_METHOD"] == "POST") {
      // username and password sent from form 
      
      $myusername = mysqli_real_escape_string($db,$_POST['username']);
      $mypassword = mysqli_real_escape_string($db,$_POST['password']); 
      
      $sql = "SELECT id FROM admin WHERE username = '$myusername' and passcode = '$mypassword'";
      $result = mysqli_query($db,$sql);
      $row = mysqli_fetch_array($result,MYSQLI_ASSOC);
      $active = $row['active'];
      
      $count = mysqli_num_rows($result);
      
      // If result matched $myusername and $mypassword, table row must be 1 row
  
      if($count == 1) {
         session_register("myusername");
         $_SESSION['login_user'] = $myusername;
         
         header("location: welcome.php");
      }else {
         $error = "Your Login Name or Password is invalid";
      }
   }
?>
<html>
   
   <head>
      <title>Login Page</title>
      
      <style type = "text/css">
         body {
            font-family:Arial, Helvetica, sans-serif;
            font-size:14px;
         }
         label {
            font-weight:bold;
            width:100px;
            font-size:14px;
         }
         .box {
            border:#666666 solid 1px;
         }
      </style>
      
   </head>
   
   <body bgcolor = "#FFFFFF">
 
      <div align = "center">
         <div style = "width:300px; border: solid 1px #333333; " align = "left">
            <div style = "background-color:#333333; color:#FFFFFF; padding:3px;"><b>Login</b></div>
    
            <div style = "margin:30px">
               
               <form action = "" method = "post">
                  <label>UserName  :</label><input type = "text" name = "username" class = "box"/><br /><br />
                  <label>Password  :</label><input type = "password" name = "password" class = "box" /><br/><br />
                  <input type = "submit" value = " Submit "/><br />
               </form>
               
               <div style = "font-size:11px; color:#cc0000; margin-top:10px"><?php echo $error; ?></div>
     
            </div>
    
         </div>
   
      </div>

   </body>
</html>

welcome.php

After successful login, it will display welcome page.
<?php
   include('session.php');
?>
<html">
   
   <head>
      <title>Welcome </title>
   </head>
   
   <body>
      <h1>Welcome <?php echo $login_session; ?></h1> 
      <h2><a href = "logout.php">Sign Out</a></h2>
   </body>
   
</html>

Logout page

Logout page is having information about how to logout from login session.
<?php
   session_start();
   
   if(session_destroy()) {
      header("Location: login.php");
   }
?>

session.php

Session.php will verify the session, if there is no session it will redirect to login page.
<?php
   include('config.php');
   session_start();
   
   $user_check = $_SESSION['login_user'];
   
   $ses_sql = mysqli_query($db,"select username from admin where username = '$user_check' ");
   
   $row = mysqli_fetch_array($ses_sql,MYSQLI_ASSOC);
   
   $login_session = $row['username'];
   
   if(!isset($_SESSION['login_user'])){
      header("location:login.php");
   }
?>

Commentaires

Posts les plus consultés de ce blog

Basic PHP Syntax

Basic PHP Syntax   A PHP script can be placed anywhere in the document.   A PHP script starts with <?php and ends with ?> : <?php // PHP code goes here ?>   The default file extension for PHP files is ".php".   A PHP file normally contains HTML tags, and some PHP scripting code.   Below, we have an example of a simple PHP file, with a PHP script that uses a built-in PHP function "echo" to output the text "Hello World!" on a web page:

Bootstrap Icons CSS

Bootstrap Icons   To use the Bootstrap glyphicons , add the following line inside the <head > section of your HTML page :   <link rel = "stylesheet" href = "http://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" >   visite link

طريقة الحصول على بطاقة ماستركارد Mastercard افتراضية مجانا

طريقة الحصول على بطاقة ماستركارد   Mastercard افتراضية مجانا كيفية الحصول على بطاقة ائتمان "ماستر كارد"  Mastercard VCC الكترونية بالمجان تشتغل 100 % قابلة للشحن بعدت طرق , اسهل طريقة للحصول على بطاقة الكترونية بدون اي شروط , يمكن استعمالعا على المواقع التي تطلب التاكيد ببطاقة بنكية للاستفادة من الخدمات المجانية مثل VPS جوجل , VPS امازون و العديد من المواقع التي تقدم خدمات مقابل التاكيد بالبنوك الالكترونية طريقة الحصول على بطاقة ماستركارد Mastercard افتراضية مجانا اليوم سوف نقدم لكم طريقة الحصول على بطاقة افتراضية ماستر كارد  Mastercard مجانية VCC , بها ارقام الحساب مع تاريخ نهاية الصلاحية و ثلاثة ارقام سرية  , كل هذه البيانات التي تطلبها منك المواقع لتفعيل اي حساب تريد تجربته بالمجان مثل موقع امزون الذي يوفر VPS لمدة سنة كاملة مجانا . العرض من طرف ياندكس الروسي Yandex هو متعدد الخدمات مثل جوجل , يحتوي على محرك بحث , بريد الكتروني و الذي يهمنا في هده التدوينة هو Money Yandex المصرف او البنك الالكتروني , حيث يمكنك من خلاله الحصول على بطاقة افتراضية بمجرد ال...