这是我的PHP代码。在我登录时在If-else语句中,它始终转到第一个php页面。它不会走到另一个。假设如果我输入用户名“ tamluk”,它将重定向到“ welcomemoyna.php”。我希望当用户名匹配时,它将重定向到确定的页面。
我正在在线尝试。用于PHP的000webhost和用于数据库的PHPMyAdmin。
<?php
session_start();
$message = "";
if (count($_POST) > 0) {
$con = mysqli_connect('localhost', 'id8401317_new', 'Deba@212', 'id8401317_abc') or die('Unable To connect');
$result = mysqli_query($con, "SELECT * FROM login WHERE username='" . $_POST["username"] . "' and password = '" . $_POST["password"] . "'");
$row = mysqli_fetch_array($result);
if (is_array($row)) {
$_SESSION["id"] = $row[id];
$_SESSION["username"] = $row[username]; {
if (isset($_POST["username"]) == 'moyna') {
header("Location:welcomemoyna.php");
} elseif (isset($_POST["username"]) == 'ramtarak') {
header("Location:welcomeramtarak.php");
} elseif (isset($_POST["username"]) == 'tamluk') {
header("Location:welcometamluk.php");
} elseif (isset($_POST["username"]) == 'bagnan') {
header("Location:welcomebagnan.php");
}
}
} else {
$message = "Invalid Username or Password!";
}
// }
}
?>
我希望当我输入tamluk
之类的“用户名”时,它将重定向到welcometamluk.php
,依此类推。
答案 0 :(得分:0)
您现在在会话中重视用户。尝试此操作。希望对您有所帮助。
<?php
session_start();
$message="";
if(count($_POST)>0) {
$con = mysqli_connect('localhost','id8401317_new','Deba@212','id8401317_abc') or die('Unable To connect');
$result = mysqli_query($con,"SELECT * FROM login WHERE username='" . $_POST["username"] . "' and password = '". $_POST["password"]."'");
$row = mysqli_fetch_array($result);
if(is_array($row)) {
$_SESSION["id"] = $row[id];
$_SESSION["username"] = $row[username];
if (isset($_SESSION['username']['id'])) {
if($_SESSION["username"]=='moyna') {
header("Location:welcomemoyna.php");
}
elseif($_SESSION['username']=='ramtarak') {
header("Location:welcomeramtarak.php");
}
elseif($_SESSION['username']=='tamluk') {
header("Location:welcometamluk.php");
}
elseif($_SESSION['username']=='bagnan') {
header("Location:welcomebagnan.php");
}
}
else {
$message = "Invalid Username or Password!";
}
}
}
?>