Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/276.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
Can';我不明白我是什么';我用这个php做得不对_Php_Html_Login_Credentials - Fatal编程技术网

Can';我不明白我是什么';我用这个php做得不对

Can';我不明白我是什么';我用这个php做得不对,php,html,login,credentials,Php,Html,Login,Credentials,我正在尝试使用php实现一个非常简单的登录系统。目前我有两个文件:index.php和verifyCredentials.php index.php <!DOCTYPE html> <html lang="en"> <head> <title>Test Site</title> </head> <body> <h1>Welcome to My

我正在尝试使用php实现一个非常简单的登录系统。目前我有两个文件:index.php和verifyCredentials.php

index.php

<!DOCTYPE html>
<html lang="en">
    <head>
        <title>Test Site</title>
    </head>

    <body>
        <h1>Welcome to My Test Homepage!</h1>

        <?php if ($_SESSION['logged_in'] != "true") : ?>
            <form method="post" action="verifyCredentials.php">
                Username: <input type="text" name="username" value=""/><br>
                Password: <input type="password" name="password" value=""/><br>
                <input type="submit" name="submit" value="Submit"/><br>
            </form>
        <?php else : ?>
            <h2>You're logged in! :)</h2>
        <?php endif ?>

        <?php if($_GET['verferr']==1){echo "<b>Login failed: incorrect username or password.</b>";} ?>

    </body>
</html>
<?php
    $username = $_POST['username'];
    $password = $_POST['password'];

    if($username == "myusername" && $password == "letmein")
    {
        $_SESSION['logged_in'] = "true";
        header("Location: index.php");
        exit;
    }
    else
    {
        $loginfailed_param = "?verferr=1";
        header("Location: index.php" . $loginfailed_param);
        exit;
    }
?>

试验场地
欢迎来到我的考试主页!
用户名:
密码:

您已登录!:)
verifyCredentials.php

<!DOCTYPE html>
<html lang="en">
    <head>
        <title>Test Site</title>
    </head>

    <body>
        <h1>Welcome to My Test Homepage!</h1>

        <?php if ($_SESSION['logged_in'] != "true") : ?>
            <form method="post" action="verifyCredentials.php">
                Username: <input type="text" name="username" value=""/><br>
                Password: <input type="password" name="password" value=""/><br>
                <input type="submit" name="submit" value="Submit"/><br>
            </form>
        <?php else : ?>
            <h2>You're logged in! :)</h2>
        <?php endif ?>

        <?php if($_GET['verferr']==1){echo "<b>Login failed: incorrect username or password.</b>";} ?>

    </body>
</html>
<?php
    $username = $_POST['username'];
    $password = $_POST['password'];

    if($username == "myusername" && $password == "letmein")
    {
        $_SESSION['logged_in'] = "true";
        header("Location: index.php");
        exit;
    }
    else
    {
        $loginfailed_param = "?verferr=1";
        header("Location: index.php" . $loginfailed_param);
        exit;
    }
?>

我成功地做到了这一点,因此如果您的用户名/密码不正确(即不等于
myusername
letmein
),那么它将重定向到登录页面,并在表单下显示错误消息

我试图这样做,当他们在index.php上验证表单时,表单将消失,并替换为一些成功文本。但是,当我输入myusername和letmein时,它只是重定向到登录,没有错误,表单仍然显示

根据我所做的研究,如果我想在php节点之间使用html,我需要使用index.php文件中所示的if-else php结构,但我这样做是否有误


有人能告诉我这里做错了什么吗?

如果要使用会话来存储数据,请确保您有
session\u start()。否则,它将不会读取会话标识符,并将假定您想要启动一个新的会话标识符

因此,在
index.php
verifyCredentials.php
的顶部添加命令。但是请确保将其作为页面上的第一行代码。然后,您需要将其添加到直接请求的任何页面


例如,如果你有
index.php
,它包括
form.php
nav.php
,那么只有
index.php
需要
session\u start()
,但是如果你有
form\u processing.php
的链接,那么
form\u processing.php
需要
session\u start()
同样如此。

如果要使用会话存储数据,请确保有
会话启动()。否则,它将不会读取会话标识符,并将假定您想要启动一个新的会话标识符

因此,在
index.php
verifyCredentials.php
的顶部添加命令。但是请确保将其作为页面上的第一行代码。然后,您需要将其添加到直接请求的任何页面


例如,如果你有
index.php
,它包括
form.php
nav.php
,那么只有
index.php
需要
session\u start()
,但是如果你有
form\u processing.php
的链接,那么
form\u processing.php
需要
session\u start()
同样如此。

要求在使用
$\u session
的每个页面顶部调用
session\u start()
。我在您的示例中没有看到它。

要求您在使用
$\u session
的每个页面顶部调用
session\u start()
。我在你的例子中看不到这一点。

啊,你接受了,因为我已经准备好了(

这是你需要使用的东西。不管怎样。。 (另外,您应该使用jQuery以获得更好的过渡效果,请参见下文)

PHP

myAjax.php

newSelect($params);
$table='users';
$select='*';
$where='`username`=?限制1';
if($dbi->exec($table,$select,$where)){
$result=$dbi->result[0];
返回passwordVerify($result['password']);//真/假
}否则{
//用于调试
//回显“上次错误:”。$dbi->get('lastError')。
。“\r\n”; //回显“上次查询:”。$dbi->get('lastQuery')。
。“\r\n”; 返回false; } } $args=数组( “用户名”=>筛选\u消毒\u特殊\u字符, “密码”=>过滤器(清除特殊字符); $post=过滤器\输入\数组(输入\ post$args); 如果有的话(员额){ if(validUser(员额)){ $response['status']='success'; $response['username']=$username; echo json_编码($response); 出口 }否则{ $response['status']='Failed'; $response['message']=“用户名/密码无效”; echo json_编码($response); 出口 } } $response['status']='Error'; $response['message']='POST Data Invalid'; echo json_编码($response); 出口
啊,你接受了,因为我已经准备好了(

这是你需要使用的东西。不管怎样。。 (另外,您应该使用jQuery以获得更好的过渡效果,请参见下文)

PHP

myAjax.php

newSelect($params);
$table='users';
$select='*';
$where='`username`=?限制1';
if($dbi->exec($table,$select,$where)){
$result=$dbi->result[0];
返回passwordVerify($result['password']);//真/假
}否则{
//用于调试
//回显“上次错误:”。$dbi->get('lastError')。
。“\r\n”; //回显“上次查询:”。$dbi->get('lastQuery')。
。“\r\n”; 返回false; } } $args=数组( “用户名”=>筛选\u消毒\u特殊\u字符, “密码”=>过滤器(清除特殊字符); $post=过滤器\输入\数组(输入\ post$args); 如果有的话(员额){ if(validUser(员额)){ $response['status']='success'; $response['username']=$username; echo json_编码($response); 出口 }否则{ $response['status']='Failed'; $response['message']=“用户名/密码无效”; echo json_编码($response); 出口 } } $response['status']='Error'; $response['message']='POST Data Invalid'; echo json_编码($response); 出口
(function($){
    $(function(){

        $("#formSubmit").on('click', function() {



            var username= $("#username").val();
            var password = $("#password").val();
            var data = {username: username, password: password};
            delegateAjax('../myAjax.php', data, 'POST');

        });
    });

function delegateAjax(url, data, responseType, dataType, callback) {

    function successHandler(data) {
        console.log("Ajax Success");
        var responseData = $.parseJSON(data);
        if (responseData.status === 'Success') {

            $("#loginForm").fadeOut(1500, function() {
                 $("#successPage").fadeIn(1500, function() {
                     $(this).fadeOut(1500, function() {

                         $("#displayUsername").html(responseData.username);
                         $("#loginHome").fadeIn(1500);
                     });
                 });
            });
        }
    };

    function failureHandler(xhr, status, error) {
        console.log("Ajax Error");
        console.log(status);
        console.log(error);
        console.dir(xhr);
    };

    function handler404(xhr, status, error) {
        console.log("404 Error");
        console.log(status);
        console.log(error);
        console.dir(xhr);
    };

    function handler500(xhr, status, error) {
        console.log("500 Error");
        console.log(status);
        console.log(error);
        console.dir(xhr); 
    };

    url = typeof url !== 'undefined' ? url : 'js/ajaxDefault.php';
    data = typeof data !== 'undefined' ? data : new Object();
    responseType = typeof responseType !== 'undefined' ? responseType : 'GET';
    dataType = typeof dataType !== 'undefined' ? dataType : 'json';
    callback = typeof callback !== 'undefined' ? callback : 'callback';

    var jqxhr = $.ajax({url: url, type: responseType, cache: true, data: data, dataType: dataType, jsonp: callback, 
                        statusCode: { 404: handler404, 500: handler500 }});
    jqxhr.done(successHandler);
    jqxhr.fail(failureHandler);
};

})(jQuery);
<?php

define('IS_AJAX', isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest');
if (!IS_AJAX) {
    $response['status'] = 'Error';
    $response['message'] = 'Same Origin Policy Error';
    echo json_encode($response);
    exit;
}
$pos = strpos($_SERVER['HTTP_REFERER'], getenv('HTTP_HOST'));
if ($pos === false) {
    $response['status'] = 'Error';
    $response['message'] = 'Same Origin Policy Error';
    echo json_encode($response);
    exit;
}

function validUser($data) {
    //connect to db and validate user
    $dbi = new mysqliObject();
    $params['string'] = $data['username'];
    $dbi->newSelect($params);
    $table = 'users';
    $select = '*';
    $where = '`username` = ? LIMIT 1';
    if ($dbi->exec($table, $select, $where)) {
        $result = $dbi->result[0];
        return passwordVerify($result['password']); //true/false
    } else {

        //for debugging
        //echo 'Last Error: '.$dbi->get('lastError').'<br>'."\r\n";
        //echo 'Last Query: '.$dbi->get('lastQuery').'<br>'."\r\n";
        return false;
    }
}

$args = array(
    'username'   => FILTER_SANITIZE_SPECIAL_CHARS,
    'password'    => FILTER_SANITIZE_SPECIAL_CHARS);

$post = filter_input_array(INPUT_POST, $args);

if ($post) {



    if (validUser($post)) {


        $response['status'] = 'success';
        $response['username'] = $username;

        echo json_encode($response);
        exit;

    } else {

        $response['status'] = 'Failed';
        $response['message'] = 'Username/Password Invalid';
        echo json_encode($response);
        exit;
    }
}

$response['status'] = 'Error';
$response['message'] = 'POST Data Invalid';
echo json_encode($response);
exit;