在user.php中,我有以下几行,当我登录时,SESSION信息在user.php中从validate.php中获取,但一个用户没有登录,我想在login.php中用 "你必须先登录 "信息重定向他们。
<?php
include('inc/header.php');
if (isset($_SESSION['username'])) {
include('lib/config.php');
define('IMG_PATH', 'https://'.$_SERVER['SERVER_NAME'].'/demo/jobs/images/');
$user = $_SESSION['username'];
$sql = "SELECT * FROM candidates WHERE username='$user'";
//$result = mysqli_result object
$result = $conn->query( $sql );
$row = $result->fetch_assoc();
$imgpath = $row['avatar'];
$img = substr( strrchr( $imgpath, "/" ), 1) ;
?>
<!-- notification message -->
<?php if (isset($_SESSION['message'])) : ?>
<div class="alert alert-success" >
<h3>
<?php
echo $_SESSION['message'];
unset($_SESSION['message']);
?>
</h3>
</div>
<?php endif ?>
<div class="welcome">
<div class="profile_card">
<img class="profile_avatar" src="<?php echo IMG_PATH . $img; ?>">
<h3 class="profile_name"><?php echo $row['fullname']; ?></h3>
<div class="profile_contact phone"><i class="material-icons">phone</i><span><?php echo $row['phone']; ?></span></div>
<div class="profile_contact email"><i class="material-icons">email</i><span><?php echo $row['email']; ?></span></div>
<p> <a href="logout.php?logout='1'" style="color: red;">logout</a> </p>
</div>
</div>
<?php
} else {
$_SESSION['message'] = "You must log in first";
header('location: login.php');
}
include('inc/footer.php');
?>
谢谢你花时间来检查代码,并感谢要求登录表单代码清晰的人,我也是login.php脚本,如下所示。
<?php
// header
include('inc/header.php');
include('lib/validate.php');
?>
<div class="login_form">
<h2>Login your account</h2>
<form class="form" action="login.php" method="post" autocomplete="off">
<div class="alert alert-error"><?= $_SESSION['message'] ?></div>
<div class="username out-group">
<label for="username">Username or Email:</label>
<input type="text" placeholder="ashif" name="username" required />
</div>
<div class="password out-group">
<label for="password">Password:</label>
<input type="password" placeholder="ue$jL382@0l" name="password" required />
</div>
<input type="submit" value="Login" name="login" class="btn btn-block btn-primary" />
<p>
Not yet a member? <a href="register.php">Join</a>
</p>
</form>
</div>
<?php
include('inc/footer.php');
?>