dongtanliefang8765 2016-10-03 14:35
浏览 44

强制登录页面首先显示

I have my login page working perfectly.

It doesn't use a database of users, It uses predefined username and password within the code. The only thing I'm unable to do is make the login page show first before the index.php. Anyone can type index.php and the site opens so it's not using the login page.

Here is my login.php

<?php
ob_start();
session_start();
?>

<html lang = "en">

<head>
  <title>Alpine VW Extension List</title>
  <link href = "css/bootstrap.min.css" rel = "stylesheet">

  <style>
     body {
        padding-top: 40px;
        padding-bottom: 40px;
        background-color: #ADABAB;
     }

     .form-signin {
        max-width: 330px;
        padding: 15px;
        margin: 0 auto;
        color: #017572;
     }

     .form-signin .form-signin-heading,
     .form-signin .checkbox {
        margin-bottom: 10px;
     }

     .form-signin .checkbox {
        font-weight: normal;
     }

     .form-signin .form-control {
        position: relative;
        height: auto;
        -webkit-box-sizing: border-box;
        -moz-box-sizing: border-box;
        box-sizing: border-box;
        padding: 10px;
        font-size: 16px;
     }

     .form-signin .form-control:focus {
        z-index: 2;
     }

     .form-signin input[type="email"] {
        margin-bottom: -1px;
        border-bottom-right-radius: 0;
        border-bottom-left-radius: 0;
        border-color:#017572;
     }

     .form-signin input[type="password"] {
        margin-bottom: 10px;
        border-top-left-radius: 0;
        border-top-right-radius: 0;
        border-color:#017572;
     }

     h2{
        text-align: center;
        color: #017572;
     }
  </style>

</head>

<body>

  <h2>Welcome, Please Login</h2> 
  <div class = "container form-signin">

     <?php
        $msg = '';

        if (isset($_POST["login"]) && !empty($_POST["username"]) 
           && !empty($_POST['password'])) {

           if ($_POST["username"] == "admin" && 
              $_POST["password"] == "admin") {
              $_SESSION["valid"] = true;
              $_SESSION["timeout"] = time();
              $_SESSION["username"] = "admin";

              header("location:index.php");
           }else {
              $msg = 'Wrong username or password';
           }
        }
     ?>
  </div> <!-- /container -->

  <div class = "container">

     <form class = "form-signin" role = "form" 
        action = "<?php echo htmlspecialchars($_SERVER['PHP_SELF']); 
        ?>" method = "post">
        <h4 class = "form-signin-heading"><?php echo $msg; ?></h4>
        <input type = "text" class = "form-control" 
           name = "username" placeholder = "username = tutorialspoint" 
           required autofocus></br>
        <input type = "password" class = "form-control"
           name = "password" placeholder = "password = 1234" required>
        <button class = "btn btn-lg btn-primary btn-block" type = "submit" 
           name = "login">Login</button>
     </form>


  </div> 

</body>
</html>
  • 写回答

1条回答 默认 最新

  • dongmo8943 2016-10-03 14:37
    关注

    You need to set a session variable something like you have done in the above code is fine.

    $_SESSION["valid"] = true;
    

    Then on your index.php page you need to start the session and check if the valid session variable is set to true and if not redirect back to the login page

    session_start();
    if($_SESSION["valid"] != true){
        header("location: login.php");
    }
    

    To clear this up from your comment, you simply put this at the top of your index.php file (in PHP tags obviously) and it will check every time your index page is loaded.

    评论

报告相同问题?

悬赏问题

  • ¥15 目详情-五一模拟赛详情页
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line