2015-04-06 202 views
0

所以我爲使用PHP的網站構建了一個簡單的登錄腳本。它運行良好,但我已經做了一些最近的改變,似乎阻止它正常運行。PHP登錄腳本小問題

基本上,當我把表放到數組中時,我使用變量$ y來跟蹤正在登錄的用戶的「類型」。但是,當登錄成功時,在回顯$ y和$類型,它們都返回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> 
+3

你的問題是什麼? –

+0

不應該$ arrayofdata [$ n] = $ row;如果條件成立,那麼在關閉狀態下? – MrTechie

+0

我的問題是,爲什麼$ y總是爲0,如果我在$ n = 5找到正確的用戶名爲 Techie先生我認爲你可能會做些什麼,我沒有把$行放入數組中,所以'數組的用戶名'點還不存在。 – I2obiN

回答

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警告,控制你的日誌;) 如果你的if -statement爲true,那麼在後面的代碼中設置$y = $n。由於$arrayofdata[$n]不存在,所以if-clause如果始終爲false並且$y保持爲0.

+0

正是這個!這樣一個愚蠢的錯誤代表我,謝謝:) – I2obiN