简体   繁体   English

调试PHP验证脚本

[英]Debugging PHP validation script

I'm building a micro-site, and am having trouble with the password reset behavior. 我正在建立一个微型网站,密码重置行为有麻烦。

There is a form that asks the user for their password twice. 有一种表格要求用户输入两次密码。 I'm not being a password nazi, and the only requirement is that the password be greater than 5 characters. 我不是nazi密码,唯一的要求是密码必须大于5个字符。 On submit, the form data is added to the $_POST array and is sent to a setPass function in my site-wide php function script. 提交后,表单数据将添加到$ _POST数组中,并发送到我的站点范围php函数脚本中的setPass函数。

The function is 功能是

function setPass(){
    $link= connectDB();

    $query= "select * from People where Username='" . $_SESSION['name'] . "' Limit 1";
    $result= $link->query($query);

    if ($result->num_rows==0){
        $_SESSION['status']= 'invaliduser';
        header("location: ../index.php");
    } else {
        $first = $_POST['firstPass'];
        $second = $_POST['secondPass'];

        if (($first == $second) && (strlen($first) > 5)){
            $password = sha1($first);
        }
    }
}

I'm leaving out the database insertion code in this example. 在此示例中,我省略了数据库插入代码。

My issue is that this script echo $_SESSION['name'] . " and password: " . $first; 我的问题是此脚本echo $_SESSION['name'] . " and password: " . $first; echo $_SESSION['name'] . " and password: " . $first; included in the page body prints out the username, but returns an unidentified variable: first warning. 页面正文中包含的内容会打印出用户名,但会返回一个unidentified variable: first警告。 This also happens when I try to access the variable $password . 当我尝试访问变量$password时,也会发生这种情况。

Earlier testing has shown that the first conditional is true, as the page is not redirected. 较早的测试表明,第一个条件为true,因为页面未重定向。

So what is causing the failure of execution in the else block? 那么,是什么导致else块中的执行失败?

Since you set $first and $password inside the setPass function, they are not available outside the body of same. 由于您在setPass函数中设置了$first$password ,因此它们不能在其主体之外使用。

You should use the global keyword or, possibly better, return the values from the function if you wish to use them outside. 您应该使用global关键字,或者,如果希望在外部使用它们,则可能最好从函数返回值。

This concept is called the scoping of variables. 这个概念称为变量作用域 These variables have local scope within the function. 这些变量在函数中具有局部作用域。 See here for a comprehensive description of scoping in PHP. 有关PHP范围定义的全面说明,请参见此处

You say that you're echoing in the body of the script? 您说您在脚本正文中回显吗? You defined the variable inside a function, so unless you set it as global(generally not a good idea, see PHP global in functions ), $first really doesn't exist. 您在函数内部定义了变量,因此除非将其设置为global(通常不是一个好主意,请参见PHP global in functions ),否则$ first确实不存在。

It looks to me like a problem with scopes. 在我看来,这是一个范围问题。 You might want to try to use either session or more preferably global variables. 您可能想尝试使用会话或更佳地使用全局变量。

Check this out for more info: http://php.net/manual/en/language.variables.scope.php 签出更多信息: http : //php.net/manual/en/language.variables.scope.php

[EDIT] [编辑]

Or even better, return $first from the function like Borealid suggested. 甚至更好的是,从Borealid建议的函数中返回$ first。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM