7个用户LOGIN帐户将被指向7个不同的页面,这些代码有什么问题?

时间:2012-10-11 03:51:13

标签: php login

这是我在登录(logging.php)页面中使用的代码。我应该怎么做呢?当试图登录第一个帐户/其他帐户时...登录页面已加载但它没有重定向相反,它只显示一个空白页面.. 或者,如果有的话,我的代码是错的,你能帮助我并提供更好的代码来代替吗?任何回复将不胜感激,谢谢! :)我非常需要这个项目..

<?php

session_start();
  $username = $_POST['username'];
  $password = $_POST['password'];

if ( isset ($_POST['username']) )
{
 $connect = mysql_connect("localhost","root","") or die ("could'nt connect to db");
 mysql_select_db("tesda") or die ("cant connect");
 $sql=("SELECT * FROM login WHERE username='$username'");
 $result=mysql_query($sql);
 $row=mysql_fetch_array($result);

 if($result !=0 )
 {
    while($row=mysql_fetch_assoc($result))
    {
        $username = $row['username'];
        $password = $row['password'];
    }

if($row['name'] == $_POST['name'])
{

    if($_POST['username']=='tesda1'){
    header('Location: expi1.php');
    }
    else if($_POST['username']=='tesda2'){ 
    header('Location: expi2.php');
    }
    else if($_POST['username']=='tesda3'){ 
    header('Location: expi3.php');
    }
    else if($_POST['username']=='tesda4'){ 
    header('Location: expi4.php');
    }
    else if($_POST['username']=='tesda5'){ 
    header('Location: expi5.php');
    }
    else if($_POST['username']=='tesda6'){ 
    header('Location: expi6.php');
    }
    else if($_POST['username']=='tesda7'){ 
    header('Location: expi7.php');
    }
    else if($_SESSION['username']="$dbusername"){
    header('Location: attempt1.php');
    }
    else if($_SESSION['username']="$dbusername"){
    header('Location: attempt2.php');
    }
    else if($_SESSION['username']="$dbusername"){
    header('Location: attempt3.php');
    }
}
}
}
?>

1 个答案:

答案 0 :(得分:0)

请查看以下代码是否有效,我做了一些小改动......

 <?php

    session_start();
      $username = $_POST['username'];
      $password = $_POST['password'];

    if ( isset ($_POST['username']) )
    {
     $connect = mysql_connect("localhost","root","") or die ("could'nt connect to db");
     mysql_select_db("tesda") or die ("cant connect");
     $sql="SELECT * FROM login WHERE username='$username' AND password='$password'";
     $result=mysql_query($sql);


     if(mysql_num_rows($result)==1)
     {

        if($_POST['username']=='tesda1'){
        header('Location: expi1.php');
        }
        else if($_POST['username']=='tesda2'){ 
        header('Location: expi2.php');
        }
        else if($_POST['username']=='tesda3'){ 
        header('Location: expi3.php');
        }
        else if($_POST['username']=='tesda4'){ 
        header('Location: expi4.php');
        }
        else if($_POST['username']=='tesda5'){ 
        header('Location: expi5.php');
        }
        else if($_POST['username']=='tesda6'){ 
        header('Location: expi6.php');
        }
        else if($_POST['username']=='tesda7'){ 
        header('Location: expi7.php');
        }

    }else{
        header('Location:wrongattempt.php');
    }
    }

?>

创建一个seprate文件来处理不成功的尝试并显示失败尝试次数。

<强> wrongAttempt.php

<?php
session_start();
$attempt = ++$_SESSION['count'];
echo 'Number of unsuccessful attempts = '.$attempt;

echo '<a href="login.php">Try again</a>';


?>

在您的登录页面中,在开始时添加以下行。我假设你的登录页面是login.php。 的的login.php

session start();
if(!isset($_SESSION['count']))
    $SESSION['count']=0; //initialize count session variable.