PHP登录脚本次要问题

时间:2015-04-06 18:25:04

标签: php login-script

所以我使用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>

1 个答案:

答案 0 :(得分:0)

据我所知,你有两行,设置$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警告,控制你的日志;) 如果$y = $n - 语句为true,则在后面的代码中设置if。由于$arrayofdata[$n]不存在,if-clause如果始终为false且$y保持为0。