它没有登录?

时间:2013-07-13 12:47:57

标签: php

我写的条件是密码和用户名匹配,它根据条件登录。但由于我是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>";
}
}
?>

3 个答案:

答案 0 :(得分: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>

答案 1 :(得分:1)

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

要让每个人进一步评估您的问题,您必须先执行以下操作:

  • 提供错误讯息。无论它是什么级别,它都是一个良好的开端。
  • 如果页面上有require()require_once()include()include_once(),则必须同时显示。
  • 提供您已有的任何结果数据。与您的代码$query4=mysql_fetch_array($result)一样,$query4的价值是多少?所以我们可以看到,如果你正在提取并比较if-else下面的正确值。

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

答案 2 :(得分: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
}
}
?>