-1

我有一个用户和管理员的登录表单,我希望我的用户在登录后能被那里的名字打招呼。

我有一个名为 users 的表,其中包含以下列ID, username, password, admin,fnamelname

我有如下登录脚本代码,如果你们/gals可以提供任何帮助,因为我对php很陌生

//Connect to db
include("conndb.php");
session_start();
//Get variables from login form
$username = $_POST['username'];
$password = $_POST['password'];
echo $username;
echo $password;
$password = MD5($password);

//query to find if user and password exist and match the password.
$query = "SELECT username, admin FROM users WHERE (username = '$username' AND password      
= '$password')";
$result = mysql_query($query)
or die (mysql_error($connect));

if (mysql_num_rows($result) !=1) {
$_SESSION['loginfail'] = 1;
header ('location: index.php');
}                    

elseif (mysql_num_rows($result) ==1)
{
// check for admin flag

$a = mysql_fetch_array($result);

if ($a[admin] == Y)
{
 $_SESSION['admin'] = 1;
header('location: admin.php');
}
else
{
 $_SESSION['user'] = $fname;
header('location: index2.php');
}

}
4

3 回答 3

1

将此更改为

if ($a[admin] == Y)
{
    $_SESSION['admin'] = 1;
    $_SESSION['username'] = $a['username'];
    header('location: admin.php');
}
else
{
   $_SESSION['user'] = 1;
   $_SESSION['username'] = $a['username'];
   header('location: index2.php');
}

现在使用 $_SESSION['username'] 在您的网页上显示用户名。

于 2013-04-18T10:28:38.783 回答
0

您需要将username或保存id到会话($_SESSION)和中,admin.php并且index2.php您需要查询fname用户并相应地使用它

于 2013-04-18T10:28:46.900 回答
0

您可能尚未开始会话。因此,通过在页面顶部添加以下行来开始您的会话。

session_start();

此外,将代码的最后一部分更改如下:

$_SESSION['username'] = $a['username'];
if ($a['admin'] == 'Y') {
     $_SESSION['admin'] = true;
     header('location: admin.php');
}else{
     $_SESSION['user'] = true;
     header('location: index2.php');
}
于 2013-04-18T10:48:23.430 回答