我创建了用户需要登录的网页。我希望用户看到他/她的信息。那打招呼user1,然后单击此处注销。在我的代码中,我什至看不到注销按钮。
更新:我看到我没有使用该用户名登录的用户名。我以用户名“ student”进入该网站。但是它显示了不同的用户名。参见下图。it supposed to write welcome student not yasemin
登录代码:
// LOGIN USER
if (isset($_POST['login_user'])) {
$username = mysqli_real_escape_string($db, $_POST['username']);
$password = mysqli_real_escape_string($db, $_POST['password']);
if (empty($username)) {
array_push($errors, "Username is required");
}
if (empty($password)) {
array_push($errors, "Password is required");
}
if (count($errors) == 0) {
$password = md5($password);
$query = "SELECT * FROM users WHERE username='$username' AND password='$password'";
$results = mysqli_query($db, $query);
$row = mysqli_fetch_assoc($results);
if('student' == $row['user_type']) {
header('Location: ogrenciarayuzu.php');
exit();
} elseif ('department' == $row['user_type']) {
header('Location: bolumsekreterligiarayuzu.php');
exit();
} elseif ('institute' == $row['user_type']) {
header('Location: enstituarayuzu.php');
exit();
}
if (mysqli_num_rows($results) == 1) {
$_SESSION['username'] = $username;
$_SESSION['success'] = "You are now logged in";
header('location: index.php');
}else {
array_push($errors, "Wrong username/password combination");
}
}
}
?>
我将此代码放在要查看登录信息的位置。
<?php session_start(); ?> // at the beginning of my code
.
.
.
<?php
if (isset($_SESSION['username'])) :
?>
<p>Welcome <strong><?php echo $_SESSION['username']; ?></strong></p>
<p ><a href="index.php" style="color: black;"> <input type="submit" value="Logout" name="logout"
class="button"/></a> </p>
<?php endif ?>
为了使其成功注销,我将此代码放入了index.php
文件中
<?php
session_start();
if (isset($_GET['logout'])) {
session_destroy();
unset($_SESSION['username']);
header("location: login1.php");
}
?>
[请帮助我为什么我仍然看不到。这里有什么问题?
用这个替换您的登录代码。
if (isset($_POST['login_user'])) {
$username = mysqli_real_escape_string($db, $_POST['username']);
$password = mysqli_real_escape_string($db, $_POST['password']);
if (empty($username)) {
array_push($errors, "Username is required");
}
if (empty($password)) {
array_push($errors, "Password is required");
}
if (count($errors) == 0) {
$password = md5($password);
$query = "SELECT * FROM users WHERE username='$username' AND password='$password'";
$results = mysqli_query($db, $query);
if (mysqli_num_rows($results) == 1) {
$row = mysqli_fetch_assoc($results);
$_SESSION['username'] = $username;
$_SESSION['success'] = "You are now logged in";
//if you want to redirect user as per its type, you can use this condition
if('student' == $row['user_type']) {
header('Location: ogrenciarayuzu.php');
exit();
} elseif ('department' == $row['user_type']) {
header('Location: bolumsekreterligiarayuzu.php');
exit();
} elseif ('institute' == $row['user_type']) {
header('Location: enstituarayuzu.php');
exit();
}
else
{
header('location: index.php');
}
}else {
array_push($errors, "Wrong username/password combination");
}
}
}