Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 4

Html Code for User Login

<html>  

1. <head>  

2.     <title>PHP login system</title>  

3.     // insert style.css file inside index.html  

4.     <link rel = "stylesheet" type = "text/css" href = "style.css">   

5. </head>  

6. <body>  

7.     <div id = "frm">  

8.         <h1>Login</h1>  

9.         <form name="f1" action = "authentication.php" onsubmit = "return valida

tion()" method = "POST">  

10.             <p>  

11.                 <label> UserName: </label>  

12.                 <input type = "text" id ="user" name  = "user" />  

13.             </p>  

14.             <p>  

15.                 <label> Password: </label>  

16.                 <input type = "password" id ="pass" name  = "pass" />  

17.             </p>  

18.             <p>     

19.                 <input type =  "submit" id = "btn" value = "Login" />  

20.             </p>  

21.         </form>  

22.     </div>  

23.     // validation for empty field   

24.     <script>  

25.             function validation()  

26.             {  
27.                 var id=document.f1.user.value;  

28.                 var ps=document.f1.pass.value;  

29.                 if(id.length=="" && ps.length=="") {  

30.                     alert("User Name and Password fields are empty");  

31.                     return false;  

32.                 }  

33.                 else  

34.                 {  

35.                     if(id.length=="") {  

36.                         alert("User Name is empty");  

37.                         return false;  

38.                     }   

39.                     if (ps.length=="") {  

40.                     alert("Password field is empty");  

41.                     return false;  

42.                     }  

43.                 }                             

44.             }  

45.         </script>  

46. </body>     

47. </html>  

Connection.php
<?php      

48.     $host = "localhost";  
    $user = "root";  

49.     $password = '';  
    $db_name = "javatpoint";  

50.       
    $con = mysqli_connect($host, $user, $password, $db_name);  
51.     if(mysqli_connect_errno()) {  
        die("Failed to connect with MySQL: ". mysqli_connect_error());  

52.     }  
?>  

Authentication.php

<?php      

53.     include('connection.php');  
    $username = $_POST['user'];  

54.     $password = $_POST['pass'];  
      

55.         //to prevent from mysqli injection  
        $username = stripcslashes($username);  

56.         $password = stripcslashes($password);  
        $username = mysqli_real_escape_string($con, $username);  

57.         $password = mysqli_real_escape_string($con, $password);  
      

58.         $sql = "select *from login where username = '$username' and password = 

'$password'";  
        $result = mysqli_query($con, $sql);  

59.         $row = mysqli_fetch_array($result, MYSQLI_ASSOC);  
        $count = mysqli_num_rows($result);  

60.           
        if($count == 1){  

61.             echo "<h1><center> Login successful </center></h1>";  
        }  
62.         else{  
            echo "<h1> Login failed. Invalid username or password.</h1>";  

63.         }     
?>  

You might also like