提供管理员用户名和密码时,将用户重定向到管理员.php页面


Re-direct user to an admin.php page when admin username and password is provided

我正在尝试在为登录系统提供管理员用户名和密码后打开管理员.php页面。到目前为止,这是我将用户重定向到主页的内容,但我不确定我的代码是否正确。我尝试使用 if 语句,但我不知道我是否正确合并了它。我的数据库是用mySQL编写的,管理员登录名和密码存储在数据库的插槽"1"中。我对PHP和所有mySQL的东西都很陌生。

编辑:

idClients是特定 id 的"数据库"列。

这是我的代码:

<?php
$hostname = 'hostname';
$dbname = 'dbname';
$username = 'username';
$password = 'password';
$con=mysql_connect($hostname,$username,$password) or die("Failed to connect to MySQL: " . mysql_error());
$db=mysql_select_db($dbname,$con) or die("Failed to connect to MySQL: " . mysql_error());
$myusername = $_POST['username'];
$mypassword = $_POST['password'];
$myusername = stripslashes($myusername);
$mypassword = stripslashes($mypassword);
$query = "SELECT * FROM Client_Information WHERE Username='$myusername' and Password='$mypassword'";
$result = mysql_query($query);
$count = mysql_num_rows($result);
mysql_close();
if($count==1){
    $seconds = 5 + time();
    setcookie(loggedin, date("F jS - g:i a"), $seconds);
    header("location: login_success.php");

这是我试图合并的 if 语句:

if ($myusername['idClients'] == '#') {
    header("Location: admin.php");
}  

然后是"if"语句之前代码的 need else 语句。

}else{
    echo '<div class="incorrect">- Incorrect username or password -</div>';
}
?>

非常感谢所有帮助:)

按如下方式编写代码:-

$query = "SELECT * FROM Client_Information WHERE username='$myusername' and password='$mypassword'";
$result = mysql_query($query);
if (!$result) {
        // Debug query result by below code
        //echo 'Could not run query: ' . mysql_error();
        //exit;
        echo '<script language="javascript">';
        echo 'alert("Username / Password Seems Wrong !")';
        echo '</script>';
        // OR
        // echo '<div class="incorrect">- Incorrect username or password -</div>';
    }else{
       $row = mysql_fetch_row($result);
       $idClientValue = $row[0];
       if ($idClientValue == '43') {
          header("Location: admin.php");
       }
       $seconds = 5 + time();
       setcookie(loggedin, date("F jS - g:i a"), $seconds);
       header("location: login_success.php");
    }    

使用脚本而不是标头。

if(true)
{
    echo "<script>
         window.location = 'index.php?q=success';
         </script>";
}else{
    echo "<script>
         window.location = 'index.php?q=failure';
         </script>";
}
<?php
$hostname = 'hostname'; / Write Here Your Hostname For Localhost it is 'localhost'
$dbname = 'dbname'; / Write Here Your Database Name like 'testdb'
$username = 'username'; / Write Here Username For Localhost it is 'root'
$password = 'password'; / Write Here Password of DB for localhost it is Blank ''
$con=mysql_connect($hostname,$username,$password) or die("Failed to connect to MySQL: " . mysql_error());
$db=mysql_select_db($dbname,$con) or die("Failed to connect to MySQL: " . mysql_error());
$myusername = $_POST['username'];
$mypassword = $_POST['password'];
$myusername = stripslashes($myusername);
$mypassword = stripslashes($mypassword);
$query = "SELECT * FROM Client_Information WHERE Username='$myusername' and Password='$mypassword'";
$result = mysql_query($query);
$count = mysql_num_rows($result);
mysql_close();
if($count==1){
    $seconds = 5 + time();
    setcookie(loggedin, date("F jS - g:i a"), $seconds);
    header("location: login_success.php"); // Here is the Redirection Page.
}
?>

您的代码是正确的,但您不必在代码中填写所需的信息。完成该信息并再次运行