gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 02:51:35 25 4
gpt4 key购买 nike

我试图在为登录系统提供管理员用户名和密码后打开 admin.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”语句之前的代码所需的 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");
}

关于php - 提供管理员用户名和密码后,将用户重定向到 admin.php 页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35127420/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com