-2

我写了密码和用户名匹配的条件,它根据条件登录。但由于我是 PHP 新手,我不知道为什么它没有登录。

<html>
<head>
</head>
<body>
<table width="300" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<form name="form1" method="post" action="">
<td>
<table width="100%" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF">
<tr>
<td colspan="3"><strong>Member Login </strong></td>
</tr>
<tr>
<td width="78">Username</td>
<td width="6">:</td>
<td width="294"><input name="username" type="text" id="username"></td>
</tr>
<tr>
<td>Password</td>
<td>:</td>
<td><input name="password" type="password" id="password"></td>
</tr>
<tr>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td><input type="submit" name="submit" value="Login"></td>
</tr>
</table>
</td>
</form>
</tr>
</table>
</body>
</html>
<?php
session_start();
if(isset($_POST['submit']))
{
include('connect.php');

// username and password sent from form 
$myusername=$_POST['username']; 
$mypassword=$_POST['password']; 

// To protect MySQL injection (more detail about MySQL injection)
$myusername = stripslashes($myusername);
$mypassword = stripslashes($mypassword);
$myusername = mysql_real_escape_string($myusername);
$mypassword = mysql_real_escape_string($mypassword);

$sql="SELECT * FROM $tbl_name WHERE username='$myusername' and password='$mypassword'";
$result=mysql_query($sql);
$count=mysql_num_rows($result);
if($count==1)
{
while($query4=mysql_fetch_array($result))
{
extract($query4);
$ul=$userlevel;

if ($ul=='A')
{
$_SESSION["username"]=$myusername;
$_SESSION["password"]=$mypassword;
header("location:projectmanager_main.php");
}
else if ($ul=='B')
{
$_SESSION["username"]=$myusername;
$_SESSION["password"]=$mypassword;
header("location:teamlead_main.php");
}
else if ($ul=='C')
{
$_SESSION["username"]=$myusername;
$_SESSION["password"]=$mypassword;
header("location:employees.php");
}
else 
{
$_SESSION["username"]=$myusername;
$_SESSION["password"]=$mypassword;
header("location:admin_main.html");
}

}

}

else
{
echo "<p align='center'>"."<font color='red'>"."Wrong Username or Password. Please Try Again"."</font>"."</p>";
}
}
?>
4

3 回答 3

1

您的问题是header()在打印页面的任何内容之前必须调用它。

你应该将你的代码重新组织成这样的:

<?php
if(isset($_POST['submit']))
{
    // Try to log in the user and redirect with header if successful
}
?>
<html>
<!-- display the login page -->
    <?php
    // if user has not been redirected
    if(isset($_POST['submit'])){echo "login failed";}
    ?>
    <!-- display login form -->
</html>
于 2013-07-13T12:59:06.500 回答
1

我还不能发表评论,所以我会回答(虽然,我认为这更适合作为评论)

为了让大家进一步评估您的问题,您必须首先执行以下操作:

  • 提供错误消息。不管它是什么水平,它都是一个很好的开始。
  • 如果页面上有任何require(), require_once(), include(),include_once()也必须显示。
  • 提供您已有的任何结果数据。就像在您的代码中一样,$query4=mysql_fetch_array($result)的值是$query4什么?所以我们可以看到,如果你在下面的 if-else 上提取和比较正确的值。

由于我无法对您的问题提供准确的答案,通过提供上述至少一个,我们可以进一步分解这个问题。

于 2013-07-13T13:12:04.060 回答
0

尝试这个:

<?php
if(!isset($_POST['submit'])){
?>

--your table in html here--

<?php 
} else {
include('connect.php');
$myusername= mysql_real_escape_string(stripslashes($_POST['username'])); 
$mypassword= mysql_real_escape_string(stripslashes($_POST['password'])); 
$num = mysql_num_rows(mysql_query("SELECT * FROM tablename WHERE username='$myusername'   AND password='$mypassword'"));
if ($num >= 1) {
session_start();
$_SESSION['loggedin'] = 'true';
header('Location:admin_home.html');
} else {
?>

-- table with error in html here --

<?php
}
}
?>
于 2013-07-13T12:57:40.670 回答