所以我爲使用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>
你的問題是什麼? –
不應該$ arrayofdata [$ n] = $ row;如果條件成立,那麼在關閉狀態下? – MrTechie
我的問題是,爲什麼$ y總是爲0,如果我在$ n = 5找到正確的用戶名爲 Techie先生我認爲你可能會做些什麼,我沒有把$行放入數組中,所以'數組的用戶名'點還不存在。 – I2obiN