2015-04-06 140 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