PHP Script to View Page Only After Login

PHP script to view page only after login . If non logged-in user click on page it will get redirected to login page. This means non-logged in users will not have access to locked pages. In this Example you will learn how to use session management to hide a page and PHP headers redirect to another page in PHP. Also show login / logout buttons if user is logged out or logged in respectively.

Demo Link


Create a directory say "show_pages_on_login"
  1. First File with name "index.php" PS: you create file with ext ".php" and NOT ".php.txt"
  2. Second File with name "loggingin.php" PS: you create file with ext ".php" and NOT ".php.txt"
  3. Third File with name "login.php" PS: you create file with ext ".php" and NOT ".php.txt"
  4. Fourth File with name "logout.php" PS: you create file with ext ".php" and NOT ".php.txt"
  5. Fifth File with name "locked.php" PS: you create file with ext ".php" and NOT ".php.txt"
Place both the files in directory "show_pages_on_login" now if you want to test the script in local machine. Place this directory "show_pages_on_login" in the PHP Server (LAMP / XAMPP / WAMP) inside directory "www" and "htdocs" in case of XAMPP. Make sure PHP server (LAMP / WAMP / XAMPP) is running.
If you dont have PHP Server Installed in your machine Learn to Install XAMPP in your Windows Machine and Learn to Install LAMP in your Linux Machine
Call the URL in your browser http://localhost/show_pages_on_login

If you want to test the script directly in server Place this directory "show_pages_on_login" in your Server and check by Calling the URL in your browser www.yourdomain.com/show_pages_on_login

index.php
  1. <?php
  2. ?>
  3. <title>PHP Script to View Page Only After Login | PlanetGhost.com</title>
  4. <center>
  5. <h3>PHP Script to View Page Only After Login(Demo)</h3>
  6. <b><a href="index.php">HOME</a></b> | <a href="locked.php">Locked Page</a> |
  7. <?php
  8. if(!isset($_SESSION['auth']))
  9. {
  10. ?>
  11. <a href="login.php">Login</a>
  12. <?php
  13. }
  14. else
  15. {
  16. ?>
  17. <a href="logout.php">Logout</a>
  18. <?php
  19. }
  20. ?>
  21. <h2>Home Page</h2>
  22. <?php
  23. if(isset($_SESSION['success']))
  24. {
  25. ?>
  26. <div class="success">
  27. <?php echo $_SESSION['success'] ; ?>
  28. </div>
  29. <?php
  30. unset($_SESSION['success']) ;
  31. }
  32. if(isset($_SESSION['failure']))
  33. {
  34. ?>
  35. <div class="failure">
  36. <?php echo $_SESSION['failure'] ;?>
  37. </div>
  38. <?php
  39. unset($_SESSION['failure']) ;
  40. }
  41. ?>
  42. </center>
  43. <style>
  44. .form{
  45. border: 1px solid #D3D3D3;
  46. text-align: center;
  47. width: 200px;
  48. }
  49. .success{
  50. background: none repeat scroll 0 0 #90EE90;
  51. width: 200px;
  52. border: 1px solid darkgreen ;
  53. }
  54. .failure{
  55. background: none repeat scroll 0 0 #E56255 ;
  56. width: 200px;
  57. border: 1px solid red ;
  58.  
  59. }
  60. </style>


loggingin.php
  1. <?php
  2. $user_id = $_POST['user_id'] ;
  3. $user_passwd = $_POST['user_passwd'] ;
  4. if($user_id =="john" && $user_passwd =="john@123")
  5. {
  6. $_SESSION['success'] = "Successfully Logged In";
  7. $_SESSION['auth'] = 1 ;
  8. header("Location:index.php") ;
  9. }
  10. else
  11. {
  12. $_SESSION['failure'] = "Incorrect";
  13. header("Location:login.php") ;
  14. }
  15. ?>


logout.php
  1. <?php
  2. header("Location:login.php") ;
  3. ?>


login.php
  1. <title>PHP Script to View Page Only After Login | PlanetGhost.com</title>
  2. <center>
  3. <h3>PHP Script to View Page Only After Login(Demo)</h3>
  4. <a href="index.php">HOME</a> | <a href="locked.php">Locked Page</a> |
  5. <?php
  6. if(!isset($_SESSION['auth']))
  7. {
  8. ?>
  9. <b><a href="login.php">Login</a> </b>
  10. <?php
  11. }
  12. else
  13. {
  14. ?>
  15. <b><a href="logout.php">Logout</a></b>
  16. <?php
  17. }
  18. ?>
  19. <?php
  20. if(isset($_SESSION['success']))
  21. {
  22. ?>
  23. <div class="success">
  24. <?php echo $_SESSION['success'] ; ?>
  25. </div>
  26. <?php
  27. unset($_SESSION['success']) ;
  28. }
  29. if(isset($_SESSION['failure']))
  30. {
  31. ?>
  32. <div class="failure">
  33. <?php echo $_SESSION['failure'] ;?>
  34. </div>
  35. <?php
  36. unset($_SESSION['failure']) ;
  37. }
  38. ?>
  39. <h2>Login Page</h2>
  40. <div class="form">
  41. <form action="loggingin.php" method="post">
  42. User Name: <input type="text" name="user_id"><br>
  43. Password : <input type="password" name="user_passwd"><br>
  44. <input type="submit" value="login"><br>
  45. </form>
  46. </div>
  47. </center>
  48. <style>
  49. .form{
  50. border: 1px solid #D3D3D3;
  51. text-align: center;
  52. width: 200px;
  53. }
  54. .success{
  55. background: none repeat scroll 0 0 #90EE90;
  56. width: 200px;
  57. border: 1px solid darkgreen ;
  58. }
  59. .failure{
  60. background: none repeat scroll 0 0 #E56255 ;
  61. width: 200px;
  62. border: 1px solid red ;
  63. }
  64. </style>


locked.php
  1. <?php
  2. if(!isset($_SESSION['auth']))
  3. {
  4. header("Location:login.php") ;
  5. }
  6. ?>
  7. <title>PHP Script to View Page Only After Login | PlanetGhost.com</title>
  8. <center>
  9. <h3>PHP Script to View Page Only After Login(Demo)</h3>
  10. <?php
  11. if(isset($_SESSION['success']))
  12. {
  13. ?>
  14. <div class="success">
  15. <?php echo $_SESSION['success'] ; ?>
  16. </div>
  17. <?php
  18. unset($_SESSION['success']) ;
  19. }
  20. if(isset($_SESSION['failure']))
  21. {
  22. ?>
  23. <div class="failure">
  24. <?php echo $_SESSION['failure'] ;?>
  25. </div>
  26. <?php
  27. unset($_SESSION['failure']) ;
  28. }
  29. ?>
  30. <a href="index.php">HOME</a> | <b><a href="locked.php">Locked Page</a></b> |
  31. <?php
  32. if(!isset($_SESSION['auth']))
  33. {
  34. ?>
  35. <a href="login.php">Login</a>
  36. <?php
  37. }
  38. else
  39. {
  40. ?>
  41. <a href="logout.php">Logout</a>
  42. <?php
  43. }
  44. ?>
  45. <h2>Locked Page</h2>
  46. <p>You can view this page and content as you are logged in.<br>
  47. Non-Logged in users will be redirected to login page when they come in this page</p>
  48. </center>


Tags: 

If You Think This Post has helped you buy us a Coffee :)

Make Donation

  • Other: $

Donate now