Skip to main content

PHP - Simple Password Hider Using JavaScript

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

Lastly, this is the link for the bootstrap that i used for the layout design

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.

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());
  4. if(!$conn){
  5. die("Error: Failed to connect to database!");
  6. }
  8. $default_query = mysqli_query($conn, "SELECT * FROM `user`") or die(mysqli_error());
  9. $default_check = mysqli_num_rows($default_query);
  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.

  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="">Sourcecodester</a>
  11. </div>
  12. </nav>
  13. <div class="col-md-3"></div>
  14. <div class="col-md-6 well">
  15. <h3 class="text-primary">PHP - Simple Password Hider</h3>
  16. <hr style="border-top:1px dotted #ccc;"/>
  17. <div class="col-md-3"></div>
  18. <div class="col-md-6" style="border:1px dotted #ccc; padding:20px;">
  19. <form method="POST" action="login.php">
  20. <div class="form-group">
  21. <label>Username</label>
  22. <input type="text" class="form-control" name="username" required="required"/>
  23. </div>
  24. <div class="form-group">
  25. <label>Password</label>
  26. <input type="password" id="password" class="form-control" name="password" required="required"/>
  27. </div>
  28. <p><input type="checkbox" onclick="passwordHider();"/>Show Password</p>
  29. <button class="btn btn-primary" name="login"><span class="glyphicon glyphicon-log-in"></span> Login</button>
  30. </form>
  31. </div>
  32. </div>
  33. <script type="text/javascript">
  34. function passwordHider(){
  35. var input = document.getElementById('password');
  37. if(input.type === "password"){
  38. input.type = "text";
  39. }else{
  40. input.type = "password";
  41. }
  42. }
  43. </script>
  44. </body>
  45. </html>


  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="">Sourcecodester</a>
  16. </div>
  17. </nav>
  18. <div class="col-md-3"></div>
  19. <div class="col-md-6 well">
  20. <h3 class="text-primary">PHP - Simple Password Hider</h3>
  21. <hr style="border-top:1px dotted #ccc;"/>
  22. <div class="col-md-3"></div>
  23. <div class="col-md-6" style="border:1px dotted #ccc; padding:20px;">
  24. <h2>Welcome!</h2>
  25. <a href="logout.php">Logout</a>
  26. </div>
  27. </div>
  28. </body>
  29. </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.

  1. <?php
  2. require_once 'conn.php';
  4. if(ISSET($_POST['login'])){
  5. $username = $_POST['username'];
  6. $password = $_POST['password'];
  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);
  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. ?>


  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!!!

Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.

Virus note: All files are scanned once-a-day by for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.


1. Re-scan downloaded files using your personal virus checker before using it.
2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.

Add new comment

This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.