Search
 
SCRIPT & CODE EXAMPLE
 

PHP

how to make login and logout to blog with php without database or MySQL

<?php
// (A) LOGIN CHECKS
require "check.php";

// (B) LOGIN PAGE HTML 
?>


<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  <title>My Portfolio</title>

  <link rel="stylesheet" href="reset.css" />
  <link rel="stylesheet" href="portfolio_style.css" />
  <link rel="stylesheet" href="login_style.css" />
</head>

<body>
  <div class="container1">
    <header class="header1">
      <h1 class="title1">My name</h1>
      <p class="subtitle"><i>Hello World!</i></p>
    </header>

    <section class="section1">
      <nav class="nav1">
        <ul>
          <li>
            <a href="portfolio_page1.html"><span class="nav2"></span>About Me</a>
          </li>
          <li>
            <a href="portfolio_page2.html"><span class="nav2"></span>Experience</a>
          </li>
          <li class="active">
            <a href="blog.php"><span class="nav2"></span>Blog</a>
          </li>
        </ul>
      </nav>
      <div class="container2">
        <div class="content">

        <?php
        if (isset($failed)) { echo "<div>invalid username or password</div>"; }
        ?>

          <button onclick="document.getElementById('id01').style.display='block'" style="width:auto;">Login</button>
          <div id="id01" class="modal">
          <form class="modal-content animate" method="post" target="_self">
            <div class="imgcontainer">
              <span onclick="document.getElementById('id01').style.display='none'" class="close" title="Close Modal">&times;</span>
            </div>
            <div class="container4">
            <label for="user"><b>Username</b></label>
            <input type="text" placeholder="Enter Username" name="user" required>
            <label for="password"><b>Password</b></label>
            <input type="password" placeholder="Enter Password" name="password" required>
            <button type="submit">Login</button>
            </div>
          </form>
          </div>
          <script>
          // Get the modal
          var modal = document.getElementById('id01');
          // When the user clicks anywhere outside of the modal, close it
          window.onclick = function(event) {
          if (event.target == modal) {
            modal.style.display = "none";
          }
          }
          </script>
        </div>
      </div>
    </section>
  </div>

</body>

</html> 

//this is the file used to check and store the passes and users: check.php

<?php
// (A) START SESSION
session_start();

// (B) HANDLE LOGIN
if (isset($_POST["user"]) && !isset($_SESSION["user"])) {
  // (B1) USERS & PASSWORDS - SET YOUR OWN !
  $users = [
    "abc" => "123",
    "def" => "456",
    "ghi" => "789"
  ];

  // (B2) CHECK & VERIFY
  if (isset($users[$_POST["user"]])) {
    if ($users[$_POST["user"]] == $_POST["password"]) {
      $_SESSION["user"] = $_POST["user"];
    }
  }

  // (B3) FAILED LOGIN FLAG
  if (!isset($_SESSION["user"])) { $failed = true; }
}

// (C) REDIRECT USER TO HOME PAGE IF SIGNED IN
if (isset($_SESSION["user"])) {
  header("Location: blog.php"); // SET YOUR OWN HOME PAGE!
  exit();
}


//landing page for my blog: blog.php

<?php 
session_start();

//LOGOUT
if (isset($_POST["logout"])) {
  unset($_SESSION["user"]);
} 
//BACK TO LOGIN PAGE IF NOT SIGNED IN
if (!isset($_SESSION["user"])) 
{
  header("Location: login.php"); 
  exit();
}

?>

//html
  
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  <title>Manel's Portfolio</title>

  <link rel="stylesheet" href="reset.css" />
  <link rel="stylesheet" href="portfolio_style.css" />
</head>

<body>
  <div class="container1">
    <header class="header1">
      <h1 class="title1">My name</h1>
      <p class="subtitle"><i>Hello World!</i></p>
    </header>

    <section class="section1">
      <div class="container2">
        <div class="content">
          <h2 class="title2">My Blog</h2>
          <p>
          </p>
        </div>
        <!-- LOGOUT -->
        <form method="post">
        <input type="hidden" name="logout" value="1"/>
        <input type="submit" value="logout"/>
        </form>
      </div>
    </section>
  </div>
</body>

</html>
Comment

PREVIOUS NEXT
Code Example
Php :: PHP DocBlocker current date 
Php :: laravel app service provider why eloquent model error 
Php :: php document append script to head 
Php :: auto complete order paid2 
Php :: PHP strspn — Finds the length of the initial segment of a string consisting entirely of characters contained within a given mask 
Php :: laravel connection timed out 
Php :: laravel pagination prevent duplicate rows 
Php :: php add km to longitude 
Php :: How do I output top readers from MySql table 
Php :: php xpath get all tags under a tag 
Php :: character encoding to remove question marks as apostrophe php code 
Php :: eager loading set limit to relationship 
Php :: share var in a maser layout laravel 
Php :: Change initial country + add top countiries on Caldera forms 
Php :: symfony create form multiple entities 
Php :: How to hide Directory Browsing in WordPress from server? 
Php :: es php query where value is not empty 
Php :: detect mobile device laravel 
Php :: 0.02 eth to php 
Php :: php send to message to mobile number using springedge 
Php :: in connection.php line 664: could not find driver (sql: select * from information_schema.tables where table_schema = news and table_name = migrations) in connector.php line 67: could not find driver 
Php :: how to create a modal in php 
Php :: how to push associative array in php 
Php :: laravel seeder multiple column 
Php :: learn php basic 
Php :: wordpress header.php 
Php :: switch php version ubuntu 
Java :: java create jframe 
Java :: java read file to string 
Java :: java get mouse position on screen 
ADD CONTENT
Topic
Content
Source link
Name
8+1 =