密码仅保护一页



我正在寻求密码保护我的网站上的特定页面(index.html)使用PHP代码。

这是我现在拥有的(此代码是此处的另一个问题的C/P):

<?php
session_start();
$username="admin";
$password="jATPqhu9";
if(isset($_POST['username']) && $_POST['username'] == $username && $_POST['password'] == $password)
{
    header("Location: ecks.html");
    exit;
}
else
{
?>
<html>
    <head>
    </head>
    <body>
        <div style='text-align:center'>
        <h3>Welcome To ---</h3>
        </div>
        <hr /><br />
        <form id='login' action="" method='post' accept-charset='UTF-8'>
            <fieldset style="width:550px">
            <legend>Admin Login</legend>
            <input type='hidden' name='submitted' id='submitted' value='1'/>
            <label for='username' >UserName:</label>
            <input type='text' name='username' id='username'  maxlength="50" />

            <label for='password' >Password:</label>
            <input type='password' name='password' id='password' maxlength="50" />
            <input type='submit' name='submit' value='Submit' />
            </fieldset>
        </form>
    </body>
</html>
<?php
}
?>

由于某种原因,重定向部分(标题("位置:ecks.html");)不会重定向到ECKS.HTML。

我认为在HTML或其他某些页面中设置用户名和密码没有任何用途,最好保护它免受服务器端的影响。为此,在服务器中,直接朝着Leach Protection或Directory Protection或Hot Link之类的内容进行设置,并在此处设置密码,无论访问该页面时,您都会在网上访问文件时需要用户名和密码。<<<<<<<<<<<<<<

<?php
session_start();
$username="admin";
$password="jATPqhu9";
if(isset($_POST['username']) && $_POST['username'] == $username && $_POST['password'] == $password)
{
$_SESSION['username'] = $username;
header("Location: ecks.html");
exit;
}
else
{
    echo "incorrect info.";
}
?>
Put this in the ecks.html page 
<?php 
session_start();
if (!isset($_SESSION['username'])) {
    //login html
}
?>

希望这有效

最新更新