PHP7 Login Script with Session

Simplest PHP7 Login Script with Session . In this Example you will learn how to use PHP session management to show message and PHP headers to redirect to another page.

Demo Link

Create a directory say "php_script_login_with_session"

  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"
Place both the files in directory "php_script_login_with_session" now if you want to test the script in local machine. Place this directory "php_script_login_with_session" 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/php_script_login_with_session

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

index.php
  1. <title>PHP Login Script with Session | PlanetGhost.com</title>
  2. <h3>Login Script in PHP with Session Message(Demo)</h3>
  3. <?php
  4. session_start() ;
  5. if(isset($_SESSION['success']))
  6. {
  7. ?>
  8. <div class="success">
  9. <?php echo $_SESSION['success'] ; ?>
  10. </div>
  11. <?php
  12. unset($_SESSION['success']) ;
  13. }
  14. if(isset($_SESSION['failure']))
  15. {
  16. ?>
  17. <div class="failure">
  18. <?php echo $_SESSION['failure'] ;?>
  19. </div>
  20. <?php
  21. unset($_SESSION['failure']) ;
  22. }
  23. ?>
  24. <div class="form">
  25. <form action="loggingin.php" method="post">
  26. User Name: <input type="text" name="user_id"><br>
  27. Password : <input type="password" name="user_passwd"><br>
  28. <input type="submit" value="login"><br>
  29. </form>
  30. </div>
  31. .form{
  32. border: 1px solid #D3D3D3;
  33. text-align: center;
  34. width: 200px;
  35. }
  36. .success{
  37. background: none repeat scroll 0 0 #90EE90;
  38. width: 200px;
  39. border: 1px solid darkgreen ;
  40. }
  41. .failure{
  42. background: none repeat scroll 0 0 #E56255 ;
  43. width: 200px;
  44. border: 1px solid red ;
  45.  
  46. }



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'] = "Correct";
  7. }
  8. else
  9. {
  10. $_SESSION['failure'] = "Incorrect";
  11. }
  12. header("Location:index.php") ;
  13. ?>

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

Make Donation

  • Other: $

Donate now