PHP登录脚本小问题


PHP Login Script Minor Issue

所以我用PHP为一个网站构建了一个简单的登录脚本。它运行良好,但我最近对它进行了一些更改,似乎阻止了它正常运行。

基本上,当我将表放入数组时,我会使用变量$y来跟踪正在登录的用户的"类型"。然而,当登录成功时,在回显$y和$type时,它们都会返回0。用户可以是类型0或类型1,但当找到用户时,似乎由于某种原因没有分配$y。

为了确认,登录语句等确实有效,如果用户名和密码正确,则会显示正确的用户名和相关详细信息。目前,出于某种原因,它似乎不想给$y赋值。

// If statement that seems to be giving me trouble
global $arrayofdata;
$arrayofdata = array();
$n = 0;
$y = 0;
// Put tables into an array
while ($row = mysql_fetch_array($resource)) {
// If statement to find position of username in array
if($arrayofdata[$n]['username'] == $username){
$y = $n;}
$arrayofdata[$n] = $row;
$n++;
}
// FULL CODE BENEATH HERE
<?php
session_start(); ?>
<html>
<head>
<title>:: clubb3r ::</title>
</head>
<body>
<?php
    loginscript::login();
class loginscript {
    // Login function.. 
    static function login() {
    $host = "gcdsrv.com";
    global $username;
    if(isset($_SESSION['username'])){
    $username = $_SESSION['username'];}
    else{
    $username = $_POST[uname];
    $_SESSION['username'] = $username;} // Store username for later
    if(isset($_SESSION['password'])){
    $password = $_SESSION['password'];}
    else{
    $password = $_POST[pword];
    $_SESSION['password'] = $password;} // Store password for later
    $connect = mysql_connect("gcdsrv.com", "", "");
    if(!$connect) {
    echo    "<h1>500 Server Error</h1>";
    }
    $db_select = mysql_select_db("c2h5oh_database", $connect);
    $resource = mysql_query("SELECT username, password, type, picture, rating FROM accounts;");
    global $arrayofdata;
    $arrayofdata = array();
    $n = 0;
    $y = 0;
    // Put tables into an array
    while ($row = mysql_fetch_array($resource)) {
    // If statement to find position of username in array
    if($arrayofdata[$n]['username'] == $username){
    $y = $n;}
    $arrayofdata[$n] = $row;
    $n++;
    }
    $n = 0;
    // Set user type (normal user or bar/club, 0 for user and 1 for bar/club)
    if(isset($_SESSION['type'])){
    $type = $_SESSION['type'];}
    else{
    $type = $arrayofdata[$y]['type'];
    $_SESSION['type'] = $type;
    }
    // Counts entries
    $count = count($arrayofdata);
    global $count2; 
    // Login check loop, searches array for username and password in POST, also stores balance of that user for later
    for($x = 0; $x < $count; $x++) {
        if($username == $arrayofdata[$x]['username'] && $password == $arrayofdata[$x]['password'] && $username != "" && $password != "") {
            $z = 1;
        }
    }
        // Fail
        if($z != 1) {
        echo    "<h1>Bad Username or Password</h1><br />";
        echo    "<h1><a href='logout.php'>Try Again</a></h1>";
        }
        // Success
        // If for user success
        if($z == 1 && $type == 0) {
        echo    "<h1>Login Successful!</h1><br />";
        echo    "<h1><a href='mainuser.html'>Proceed</a></h1>";
        echo    $type;
        echo    $y;
        }
        //Success
        //If for bar/club success
        if($z == 1 && $type == 1){
        echo    "<h1>Login Successful!</h1><br />";
        echo    "<h1><a href='mainbar.html'>Proceed</a></h1>";
        echo    $type;
        }
    }
}
?>
</body>
</html>

就我所见,您有两行,它们设置了$y

`$y = 0;`

// Put tables into an array
while ($row = mysql_fetch_array($resource)) {
  // If statement to find position of username in array
  if($arrayofdata[$n]['username'] == $username){
    $y = $n;
  }
  $arrayofdata[$n] = $row;
  $n++;
}

再往前看,你会看到这三条线:

$arrayofdata = array(); //create EMPTY array
[...]
if($arrayofdata[$n]['username'] == $username){ //check index $n
[...]
$arrayofdata[$n] = $row; //set index $n

你在设置$arrayofdata[$n]之前访问它。我猜你的if语句会抛出PHP警告,控制你的日志;)如果if-语句为true,则在下面的代码中设置$y = $n。由于$arrayofdata[$n]不存在,if子句if始终为false,$y保持为0。