0

我在 php 代码中遇到错误.. 致命错误:调用未定义的函数 msg() 但该函数已在此处的代码中定义,每当我单击登录按钮时,此登录脚本就会运行

<?php
mysql_select_db("elunika", $con);
$a = $_POST['em'];
$b = $_POST['pwd'];
$c = $_POST['log'];
$b = md5($b);

if (isset($c)) {
    $q = mysql_query("select * from registeration where email='$a' and password='$b'");
    $r = mysql_num_rows($q);
    if ($r) {
        $_SESSION["Authenticated"] = 1;
        $_SESSION['id'] = $a;
    }
    else {
        $_SESSION["Authenticated"] = 0;
    }

    if ($_SESSION["Authenticated"] === 0) {
        die(msg(0, "Incorrect Information"));
    }
    else {
        session_write_close();
        echo msg(1, "profile.php");
    }

    function msg($status, $txt)
    {
        return '{"status":' . $status . ',"txt":"' . $txt . '"}';
    }
}

?>
4

1 回答 1

3

在编译时(执行之前)只定义了无作用域的函数。if (isset(…))但是,如果输入 if 分支(第9 行),则只会定义您的 msg 函数;所以它只会在执行者到达它的那一刻被定义。

但是在msg()运行时遇到函数声明之前已经调用了您的代码。向上移动函数声明(= 在msg()调用之前)应该会有所帮助:

function msg ($status, $txt) {
    return '{"status":'.$status.',"txt":"'.$txt.'"}';
}

if($_SESSION["Authenticated"] === 0) {
    die(msg(0,"Incorrect Information"));
} else {
    session_write_close();
    echo msg(1,"profile.php");
}
于 2013-10-15T16:49:03.633 回答