PHP - Simple Password Hider Using JavaScript

Submitted by: 
Language: 
Visitors have accessed this post 230 times.

In this tutorial we will create a Simple Password Hider using JavaScript. Using PHP, you can let your user directly interact with the script and easily to learned its syntax. JavaScript is designed to simplify the client-side scripting of HTML. It is designed to make it easier to navigate a document, via classes and id's attribute. So let's now do the coding...

Getting started:

First you have to download & install XAMPP or any local server that run PHP scripts. Here's the link for XAMPP server https://www.apachefriends.org/index.html.

Lastly, this is the link for the bootstrap that i used for the layout design https://getbootstrap.com/.

Creating Database

Open your database web server then create a database name in it db_hider, after that click Import then locate the database file inside the folder of the application then click ok.
tut1

Creating the database connection

Open your any kind of text editor(notepad++, etc..). Then just copy/paste the code below then name it conn.php.

  1. <?php
  2. $conn = mysqli_connect("localhost", "root", "", "db_hider") or die(mysqli_error());
  3.  
  4. if(!$conn){
  5. die("Error: Failed to connect to database!");
  6. }
  7.  
  8. $default_query = mysqli_query($conn, "SELECT * FROM `user`") or die(mysqli_error());
  9. $default_check = mysqli_num_rows($default_query);
  10.  
  11. if($default_check === 0){
  12. mysqli_query($conn, "INSERT INTO `user` VALUES('', 'admin', 'admin')") or die(mysqli_error());
  13. }
  14. ?>

Creating The Interface

This is where we will create a simple form for our application. To create the forms simply copy and write it into your text editor, then save it as shown below.
index.php

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1"/>
  5. <link rel="stylesheet" type="text/css" href="css/bootstrap.css"/>
  6. </head>
  7. <body>
  8. <nav class="navbar navbar-default">
  9. <div class="container-fluid">
  10. <a class="navbar-brand" href="<a href="https://sourcecodester.com">Sourcecodester</a>
  11. " rel="nofollow">https://sourcecodester.com">Sourcecodester</a>
  12. </a> </div>
  13. </nav>
  14. <div class="col-md-3"></div>
  15. <div class="col-md-6 well">
  16. <h3 class="text-primary">PHP - Simple Password Hider</h3>
  17. <hr style="border-top:1px dotted #ccc;"/>
  18. <div class="col-md-3"></div>
  19. <div class="col-md-6" style="border:1px dotted #ccc; padding:20px;">
  20. <form method="POST" action="login.php">
  21. <div class="form-group">
  22. <label>Username</label>
  23. <input type="text" class="form-control" name="username" required="required"/>
  24. </div>
  25. <div class="form-group">
  26. <label>Password</label>
  27. <input type="password" id="password" class="form-control" name="password" required="required"/>
  28. </div>
  29. <p><input type="checkbox" onclick="passwordHider();"/>Show Password</p>
  30. <button class="btn btn-primary" name="login"><span class="glyphicon glyphicon-log-in"></span> Login</button>
  31. </form>
  32. </div>
  33. </div>
  34. <script type="text/javascript">
  35. function passwordHider(){
  36. var input = document.getElementById('password');
  37.  
  38. if(input.type === "password"){
  39. input.type = "text";
  40. }else{
  41. input.type = "password";
  42. }
  43. }
  44. </script>
  45. </body>
  46. </html>

home.php

  1. <!DOCTYPE html>
  2. <?php
  3. if(!ISSET($_SESSION['user'])){
  4. header("location: index.php");
  5. }
  6. ?>
  7. <html>
  8. <head>
  9. <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1"/>
  10. <link rel="stylesheet" type="text/css" href="css/bootstrap.css"/>
  11. </head>
  12. <body>
  13. <nav class="navbar navbar-default">
  14. <div class="container-fluid">
  15. <a class="navbar-brand" href="<a href="https://sourcecodester.com">Sourcecodester</a>
  16. " rel="nofollow">https://sourcecodester.com">Sourcecodester</a>
  17. </a> </div>
  18. </nav>
  19. <div class="col-md-3"></div>
  20. <div class="col-md-6 well">
  21. <h3 class="text-primary">PHP - Simple Password Hider</h3>
  22. <hr style="border-top:1px dotted #ccc;"/>
  23. <div class="col-md-3"></div>
  24. <div class="col-md-6" style="border:1px dotted #ccc; padding:20px;">
  25. <h2>Welcome!</h2>
  26. <a href="logout.php">Logout</a>
  27. </div>
  28. </div>
  29. </body>
  30. </html>

Creating the PHP Query

This code contains the php query of the application. This code will login the user to the main page, and also can logout after exploring the page. To do this just copy and write these block of codes as shown below inside the text editor and save it as shown below.
login.php

  1. <?php
  2. require_once 'conn.php';
  3.  
  4. if(ISSET($_POST['login'])){
  5. $username = $_POST['username'];
  6. $password = $_POST['password'];
  7.  
  8. $query = mysqli_query($conn, "SELECT * FROM `user` WHERE `username` = '$username' && `password` = '$password'") or die(mysqli_error());
  9. $rows = mysqli_num_rows($query);
  10. $fetch = mysqli_fetch_array($query);
  11.  
  12. if($rows > 0){
  13. $_SESSION['user'] = $fetch['user_id'];
  14. header('location: home.php');
  15. }else{
  16. echo "<script>alert('Invalid username or password')</script>";
  17. echo "<script>window.location = 'index.php'</script>";
  18. }
  19. }
  20. ?>

logout.php

  1. <?php
  2. unset($_SESSION['user']);
  3. header("location: index.php");
  4. ?>

There you have it we successfully created a Simple Password Hider using JavaScript. I hope that this simple tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!!!


Add new comment

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • You may insert videos with [video:URL]
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd> <table> <tr> <td> <th> <img> <h1> <h2> <h3> <iframe> [video]
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <asp>, <c>, <cpp>, <csharp>, <css>, <html4strict>, <java>, <javascript>, <mysql>, <php>, <python>, <sql>, <vb>, <vbnet>. The supported tag styles are: <foo>, [foo].
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.