CREATE TABLE `userlogin` (
`userid` int(11) NOT NULL,
`username` varchar(30) NOT NULL,
`password` varchar(30) NOT NULL,
`fullname` varchar(60) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
INSERT INTO `userlogin` (`userid`, `username`, `password`, `fullname`) VALUES
(1, 'cairocoders', '123456', 'Cairocoders Ednalan');
ALTER TABLE `userlogin`
ADD PRIMARY KEY (`userid`);
ALTER TABLE `userlogin`
MODIFY `userid` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=2;
index.php
conn.php
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 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 | //index.php <!DOCTYPE html> <html> <head> <title>PHP Mysqli Simple Login Session with Validation</title> <link rel= "stylesheet" href= "https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" /> <link rel= "stylesheet" href= "https://stackpath.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" > <style> .message {color: #FF0000;} </style> </head> <body> <?php $Message = $ErrorUname = $ErrorPass = "" ; if ( $_SERVER [ "REQUEST_METHOD" ] == "POST" ) { $username = check_input( $_POST [ "username" ]); if (!preg_match( "/^[a-zA-Z0-9_]*$/" , $username )) { $ErrorUname = "Space and special characters not allowed but you can use underscore(_)." ; } else { $fusername = $username ; } $fpassword = check_input( $_POST [ "password" ]); if ( $ErrorUname != "" ){ $Message = "Login failed! Errors found" ; } else { include ( 'conn.php' ); $query =mysqli_query( $conn , "select * from userlogin where username='$fusername' && password='$fpassword'" ); $num_rows =mysqli_num_rows( $query ); $row =mysqli_fetch_array( $query ); if ( $num_rows >0){ $Message = "Login Successful!" ; } else { $Message = "Login Failed! User not found" ; } } } function check_input( $data ) { $data = trim( $data ); $data = stripslashes ( $data ); $data = htmlspecialchars( $data ); return $data ; } ?> <section class = "login" > <div class = "container" > <div class = "row justify-content-center" > <div class = "col-md-6 text-center mb-5" > <h2 class = "heading-section" >PHP Mysqli Simple Login Session with Validation</h2> </div> </div> <div class = "row justify-content-center" > <div class = "col-md-7 col-lg-5" > <div class = "p-4 p-md-5" > <div class = "d-flex" > <div class = "w-100" > <h3 class = "mb-4" >Sign In</h3> </div> <div class = "w-100" > <p class = "justify-content-end" > <a href= "#" class = "align-items-center justify-content-center" ><span class = "fa fa-facebook" ></span></a> <a href= "#" class = "align-items-center justify-content-center" ><span class = "fa fa-twitter" ></span></a> </p> </div> </div> <form method= "post" action= "<?php echo htmlspecialchars($_SERVER[" PHP_SELF "]);?>" > <p><span class = "message" >* required field.</span></p> <div class = "form-group mt-3" > <input type= "text" name= "username" class = "form-control" required> <label class = "form-control-placeholder" for = "username" >Username</label> <span class = "message" >* <?php echo $ErrorUname ;?></span> </div> <div class = "form-group" > <input type= "password" name= "password" class = "form-control" required> <label class = "form-control-placeholder" for = "password" >Password</label> <span class = "message" >* <?php echo $ErrorPass ;?></span> <span class = "fa fa-fw fa-eye" ></span> </div> <div class = "form-group" > <button type= "submit" class = "form-control btn btn-primary rounded submit px-3" >Sign In</button> </div> <span class = "message" > <?php if ( $Message == "Login Successful!" ){ echo $Message ; echo 'Welcome, ' . $row [ 'fullname' ]; } else { echo $Message ; } ?> </span> <div class = "form-group d-md-flex" > <div class = "w-50 text-left" > <label class = "checkbox-primary mb-0" >Remember Me <input type= "checkbox" checked> <span class = "checkmark" ></span> </label> </div> <div class = "w-50 text-md-right" > <a href= "#" >Forgot Password</a> </div> </div> </form> <p class = "text-center" >Not a member? <a data-toggle= "tab" href= "#signup" >Sign Up</a></p> </div> </div> </div> </div> </section> </body> </html> |
1 2 3 4 5 6 7 8 9 10 | //conn.php <?php $conn = mysqli_connect( "localhost" , "root" , "" , "testingdb" ); // Check connection if (mysqli_connect_errno()) { echo "Failed to connect to MySQL: " . mysqli_connect_error(); } ?> |