如何在php中使用用户和管理员创建登录

时间:2017-03-20 05:57:47

标签: php mysql

您好我们正在开发一个项目,我们差不多完成但是login.php无法正常工作,当我以用户t登录时登录,但当我以管理员身份登录时,它只会重定向到用户页面。就像它没有读作“管理员”作为管理员

'<?php
 session_start();
 ?>

  <?php
   $servername = "localhost";
   $username = "root";
   $password = "";
   $dbname = "dbnew";

   $iusername = $_POST['username'];
   $ipassword = $_POST['password'];



    // Create connection
    $conn = mysqli_connect($servername, $username, $password, $dbname);
    // Check connection
    if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
    }
    $sql = "Select * FROM acc WHERE username='$iusername';";
    $query = mysqli_query ($conn,$sql);

    $numrows = mysqli_num_rows($query);

        if ($numrows!=0)
        {
        //login
            while ($row = mysqli_fetch_assoc($query))
            {

                $dbusername = $row['Username'];
                $dbpassword = $row['Password'];

            }

        //matching
            if($iusername==$dbusername&&$ipassword==$dbpassword)
            {
                echo "Welcome " . $dbusername . "!";
                echo "<a href=user.php>Go to Profile</a>";
                $_SESSION['Current'] = $dbusername ;
            }
            elseif($iusername=='admin'&&$ipassword=='admin')
            {
                echo "Welcome admin";
                echo "<a href=admin.php> Go to Admin Page</a>";
                $_SESSION['Current'] = "admin" ;


            }
            else
            {
            echo  $ipassword;
            echo "incorrect username/password";
            }
        }



         else {
        echo "Database Error 404"  . "<br>" . mysqli_error($conn);

        echo "<a href=accounts.html>Back</a>";
        }

        mysqli_close($conn);
         ?>'

1 个答案:

答案 0 :(得分:0)

您的代码中的错误很少,请尝试使用此代码

<?php
session_start();

$servername = "localhost";
$username = "root";
$password = "";
$dbname = "dbnew";

$iusername = $_POST['username'];
$ipassword = $_POST['password'];

// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}

$sql = "Select * FROM acc WHERE username='$iusername' AND password='$ipassword'";
$query = mysqli_query($conn,$sql);

$numrows = mysqli_num_rows($query);

    if ($numrows!=0) // if numrows is not 0 it means username and password is correct
    {       
        if ($iusername=='admin' && $ipassword=='admin') // if user is found and user is admin then redirect to admin page
        {
            $_SESSION['Current'] = "admin" ;
            echo "Welcome admin";
            echo "<a href=admin.php> Go to Admin Page</a>";
        }
        else //if user is found and it is not admnin this code will run
        {
            $_SESSION['Current'] = $dbusername ;
            echo "Welcome " . $dbusername . "!";
            echo "<a href=user.php>Go to Profile</a>";
        }
    }
    else // if username or password is incorrect this code will run
    {
        echo  $ipassword; //if you are using for testing purpose then echo this otherwise remove it
        echo "incorrect username/password <br>";
        echo "<a href=accounts.html>Back</a>";
    }

mysqli_close($conn);
?>