我一直在寻找答案,但我没有尝试过任何工作,我尝试在标题链接中添加一个变量,将php添加到html中,添加会话变量但没有任何效果。
我试图在用户首次登录时将其定向到landing_first,并在该页面上显示welcome" username"然后当他们退出并重新登录时,他们将被重定向到landing_second.html,这将表示欢迎回来"用户名"。但我似乎没有尝试任何工作!真的很感激一些帮助
以下是我在login.php中使用的代码
if(!empty($_POST['username']) && !empty($_POST['password'])) {
$username = mysqli_real_escape_string($conn, $_POST['username']);
// this creates a hashed password which will need to be inserted into the `zz_login` table
// $password = password_hash($_POST['password'], PASSWORD_BCRYPT);
$sql = "SELECT user_id, `password` FROM zz_login WHERE username='$username'";
$result = mysqli_query($conn, $sql, MYSQLI_USE_RESULT);
if(!is_bool($result)) {
$user = mysqli_fetch_assoc($result);
mysqli_free_result($result);
if(password_verify($_POST['password'], $user['password'])) {
if(session_start()) {
/* expire in 1 hour */
// needs to be ahead of header - only add cookie when user is logged in
setcookie("user_id", $user['user_id'], time() + 4600,'/~B00657633/Final%20Year/COM559/Major%20Project/startbootstrap-creative-gh-pages/');
$_SESSION['username'] = $username;
// Check if whether a person has made any holiday reservations
$userId = $user['user_id'];
$sql = "SELECT * FROM zz_upload_itinerary WHERE user_id=$userId";
$result = mysqli_query($conn, $sql);
if(mysqli_num_rows($result) > 0){
header('Location: ../landing_second.html?message=welcome back ' . $_SESSION["$username"]);// Redirect to the second page
}else{
header('Location: ../landing_first.html?message=welcome ' . $_SESSION["$username"]);// redirect to the first page
}
if(!empty($_GET['message'])) echo htmlspecialchars($_GET['message']);
} else {
die('error session couldn`t be initialised');
}
} else {
$fmsg = "Invalid Username/Password";
}
}
}
答案 0 :(得分:1)
使用$_SESSION['userId'] = $user['userId']
会在数据库中显示用户的ID,但是如果您想在页面上显示用户的用户名,那么您应该说$_SESSION['username'] = $username;
然后,您可以在此处或会话实例运行的任何地方调用会话
即
echo '<li><a href="#">Welcome'. $_SESSION["username"] . '</a></li>'
答案 1 :(得分:0)
您重定向到html页面并向这些页面发送欢迎消息。 我建议这是你的问题(除非那些页面是通过htaccess处理的,但我怀疑不是基于你的代码)。 那些欢迎页面应该是PHP页面,应该有代码;
<?PHP
echo "Welcome back ".$_SESSION["username"];
?>