PHP登录错误"未定义的变量" [英] PHP login error "Undefined Variable"

查看:145
本文介绍了PHP登录错误"未定义的变量"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

先生,我在我的剧本的Andr​​oid application.Everything使用的login.php脚本运行的罚款,如果我张贴有效的用户名和密码,但是当我离开这两个领域的空白或我输入一些不正确的信息我我得到一个错误未定义的变量login_ok。请帮助我,因为我不知道很多关于PHP scripts.Thanks提前

我的login.php脚本是

 < PHP//装载并连接到MySQL数据库的东西
要求(config.php文件);如果(!空($ _ POST)){
    //获取基于掀起了用户名用户的信息。
    $查询=
            选择
                ID,
                用户名,
                密码
            来自用户的
            哪里
                用户名=:用户名
            $ query_params =阵列(
        :用户名=> $ _ POST [用户名]
    );    尝试{
        $语句= $ DB-GT&; prepare($查询);
        $结果= $ stmt->执行($ query_params);
    }
    赶上(PDOException $前){
        //为了测试,你可以使用模具和消息。
        //模具(无法运行的问题:$ EX->的getMessage());        //或使用此用这个产品JSON数据:
        $响应[成功] = 0;
        $响应[消息] =数据库ERROR1请重试!
        死亡(json_en code($响应));    }    //这将是变量来确定用户的信息是否是正确的。
    //我们初始化为假。
    $ validated_info = FALSE;    //从查询获取所有的行
    $行= $ stmt->取();
    如果($行){
        //如果我们加密的口令,我们就在这里解密,但在我们的例子中,我们只
        //比较两个密码
        如果($ _ POST ['密码'] === $行['密码']){
            $ login_ok = TRUE;
        }
    }    //如果用户登录成功,那么我们将它们发送到私人的会员制页
    //否则,我们显示登录失败的消息,并再次显示登录表单
    如果($ login_ok){
        $响应[成功] = 1;
        $响应[消息] =登录成功!;
        死亡(json_en code($响应));
    }其他{
        $响应[成功] = 0;
        $响应[消息] =!无效的凭证;
        死亡(json_en code($响应));
    }
}其他{
?>
        < H1>登录和LT; / H1>
        <形式的行动=login.php中方法=后>
            用户名:其中; BR />
            <输入类型=文本名称=用户名占位符=用户名/>
            < BR />< BR />
            密码:LT; BR />
            <输入类型=密码NAME =密码占位符=密码值=/>
            < BR />< BR />
            <输入类型=提交值=登录/>
        < /表及GT;
        &所述; A HREF =register.php>寄存器&下; / A>
    < PHP
}?>


解决方案

当你没有行,你不设置$ login_ok变量,所以,当你做

 如果($ login_ok){

您正在访问未被定义的变量。

 如果(使用isset($ login_ok)及和放大器; $ login_ok)){

,而不是你是否或者设置$ login_ok = FALSE;当你没有行。

Sir I am using a login.php script in my android application.Everything with the script is running fine if I am posting a valid username and password but when I am leaving both the fields blank or I am entering some incorrect information I am getting an error "Undefined variable login_ok". Kindly help me as I don't know much about php scripts.Thanks in advance

My login.php script is

<?php

//load and connect to MySQL database stuff
require("config.php");

if (!empty($_POST)) {
    //gets user's info based off of a username.
    $query = " 
            SELECT 
                id, 
                username, 
                password
            FROM users 
            WHERE 
                username = :username 
        ";

    $query_params = array(
        ':username' => $_POST['username']
    );

    try {
        $stmt   = $db->prepare($query);
        $result = $stmt->execute($query_params);
    }
    catch (PDOException $ex) {
        // For testing, you could use a die and message. 
        //die("Failed to run query: " . $ex->getMessage());

        //or just use this use this one to product JSON data:
        $response["success"] = 0;
        $response["message"] = "Database Error1. Please Try Again!";
        die(json_encode($response));

    }

    //This will be the variable to determine whether or not the user's information is correct.
    //we initialize it as false.
    $validated_info = false;

    //fetching all the rows from the query
    $row = $stmt->fetch();
    if ($row) {
        //if we encrypted the password, we would unencrypt it here, but in our case we just
        //compare the two passwords
        if ($_POST['password'] === $row['password']) {
            $login_ok = true;
        }
    }

    // If the user logged in successfully, then we send them to the private members-only page 
    // Otherwise, we display a login failed message and show the login form again 
    if ($login_ok) {
        $response["success"] = 1;
        $response["message"] = "Login successful!";
        die(json_encode($response));
    } else {
        $response["success"] = 0;
        $response["message"] = "Invalid Credentials!";
        die(json_encode($response));
    }
} else {
?>
        <h1>Login</h1> 
        <form action="login.php" method="post"> 
            Username:<br /> 
            <input type="text" name="username" placeholder="username" /> 
            <br /><br /> 
            Password:<br /> 
            <input type="password" name="password" placeholder="password" value="" /> 
            <br /><br /> 
            <input type="submit" value="Login" /> 
        </form> 
        <a href="register.php">Register</a>
    <?php
}

?> 

解决方案

when you have no rows, you are not setting the $login_ok variable, so, when you do

if ($login_ok) {

you are accessing a variable which is not defined. use

if(isset($login_ok) && $login_ok)) {

instead of your if, or set $login_ok = false; when you have no rows.

这篇关于PHP登录错误&QUOT;未定义的变量&QUOT;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆