Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/230.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
帮助使用PHP Cookie和一些基本的身份验证_Php_Html - Fatal编程技术网

帮助使用PHP Cookie和一些基本的身份验证

帮助使用PHP Cookie和一些基本的身份验证,php,html,Php,Html,我正在尝试使用一些真正基本的身份验证。。。基本上,我想把我的网站上线,但有一个密码,我可以发送给一些人,让他们可以访问它,而不是其他任何人 我一直在尝试使用饼干,但我对它们相对缺乏经验。下面的代码是我到目前为止的代码 在我的标题顶部。出于某种原因,这不会重定向到authenticate.php <?php if(!isset($_COOKIE["user"])) header("Location: authenticate.php?invalid=0"); ?> authent

我正在尝试使用一些真正基本的身份验证。。。基本上,我想把我的网站上线,但有一个密码,我可以发送给一些人,让他们可以访问它,而不是其他任何人

我一直在尝试使用饼干,但我对它们相对缺乏经验。下面的代码是我到目前为止的代码

在我的标题顶部。出于某种原因,这不会重定向到authenticate.php

<?php if(!isset($_COOKIE["user"])) header("Location: authenticate.php?invalid=0"); ?>

authenticate.php

<?php
    if($_GET["invalid"] == 1)   {
        echo '<p> Invalid password. </p>';
    }
        echo '<form action="enter.php" method="post">

                <span>Enter the password</span><br />
                <textarea name="mainPass"></textarea>
                <input type="submit" value="Submit!" />
            </form>';

?>
<?php
    $pass = $_POST['mainPass'];
    if(strstr($pass, "<password removed>")) {
        setcookie("user", time()+3600);
        header("Location: index.php");
    } else {
        header("Location: authenticate.php?invalid=1    ");
    }
?>

enter.php

<?php
    if($_GET["invalid"] == 1)   {
        echo '<p> Invalid password. </p>';
    }
        echo '<form action="enter.php" method="post">

                <span>Enter the password</span><br />
                <textarea name="mainPass"></textarea>
                <input type="submit" value="Submit!" />
            </form>';

?>
<?php
    $pass = $_POST['mainPass'];
    if(strstr($pass, "<password removed>")) {
        setcookie("user", time()+3600);
        header("Location: index.php");
    } else {
        header("Location: authenticate.php?invalid=1    ");
    }
?>

又快又脏。不要用这个太久。不需要任何表格。设置硬编码到功能代码中的用户名和密码。您必须在每页的顶部运行该函数,才能使其正常工作

    // quick and dirty http authentication
    function authenticateHttp() {
        @session_start();
        // credential check
        if (!isset($_SESSION['auth_realm']) || !isset($_SERVER['PHP_AUTH_USER'])) {
            // credentials not supplied yet
            // send http credential request
            $_SESSION['auth_realm'] = uniqid('', true);
            header('WWW-Authenticate: Basic realm="' . $_SESSION['auth_realm'] . '"');
            header('HTTP/1.0 401 Unauthorized');
            // if cancel button clicked, show this
            exit('You must authenticate before accessing this area.');
        } else {
            // credentials supplied
            if ($_SERVER['PHP_AUTH_USER'] !== '{your chosen username}' || $_SERVER['PHP_AUTH_PW'] !== '{your chosen password}') {
                unset($_SESSION['auth_realm']);
                exit('You have entered an invalid login.');
            }
        }
    }

将exit()放在标题后面。那么它应该正确重定向。如果php标记放在页面的末尾,最好避免结束?>如果它没有导致任何事情发生,那么它根本不会重定向。编辑:在函数的开头添加了@session_start()。差点忘了。@dqhendricks您的函数导致一个错误:
Parse error:syntax error,在第一个IF时出现意外的T\u IF
statement@dqhendricks在function@Tory沃特曼在@session_start()之后有分号吗@dqhendricks现在可以了,但如果他们一次登录错误,就不会再显示登录信息。。。我在哪里可以放置一个会话来解决这个问题?