我有一个用户系统。用户以method = post的形式输入电子邮件和密码。在另一个文件中检索发布值,在该文件中检查数据库中的这些数据。如果有用户,则检索用户ID并将其另存为$user = $_SESSION["user"];
。
为了使所有这些操作更加简单,我们在这里仅关注四个页面。我已经描述了其中两个,即登录页面,log-user-in.php页面。
将uid保存为$user
中的会话后,用户将被重定向($uid
来自数据库):
$_SESSION["user"] = $uid;
header("Location: user/card.php");
exit;
在login.php和card.php中,我已经回显了session_id()
,并且在这两页上,它们最初是相同的。问题是,例如,当我单击subscription.php时,$ _ SESSION [“ user”]为空白,而session_id();
是一个完全不同的id。在单击subscription.php并单击card.php之后,该页面现在显示subscription.php中的session_id();
,这意味着该用户名不再保存在$ _SESSION [“ user”]中。
如果我注销并重新开始,则一切正常。我可以单击所有页面,并且session_id()
在所有页面上都是相同的,并且$ _SESSION [“ user”]在所有页面上都有一个用户ID。
但是每次我都首先尝试这样做,问题从头开始。
我检查了每一页,并且在每一页的开头都有session_start()
。我真的不知道如何查看该问题的某种错误日志。
log-user-in.php
<?
session_start();
include('includingThis.php');
$email = addslashes($_POST[email]);
$password = addslashes($_POST[pass]);
$email = strtolower($email);
// TJEK OM BRUGEREN FINDES
if ($stmt = $con->prepare("SELECT email, pass, uid FROM stnd_users WHERE email=?")) {
/* bind parameters for markers */
$stmt->bind_param("s", $email);
$stmt->execute();
$stmt->bind_result($em, $pa, $u);
// Loop through each row in the result set
while ($stmt->fetch()) {
$gottenEmail = $em;
$gottenPass = $pa;
$uid = $u;
}
$stmt->close();
}
// NÅR BRUGEREN FINDES, OG KODEN ER KORREKT
if ($uid != "") {
$_SESSION["user"] = $uid;
// Bruger har betalt
if ($profile_pic == "true") {
header("Location: user/card.php");
exit;
} else {
header("Location: user/profilepic.php");
exit;
}
}
?>
card.php和subscription.php相同,只是内容不同
<?
session_start();
$user = $_SESSION["user"];
include('../includingThis.php');
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1,user-scalable=0"/>
<title></title>
<?
include("../logged-header.php");
?>
<div id="container">
<div id="headline">
<h1>HEADLINE</h1>
</div>
<div align="center">
TEXT
<br />
<? echo ini_get('session.cookie_domain') . "<br>"; ?>
<? echo session_id(); ?>
</div>
<?
include("../footer.php");
?>
<script>
alert(<? echo session_id(); ?>);
</script>
</body>
</html>
logged-header.php
<?php
$style_param = 1;
if ($stmt = $con->prepare("SELECT styles, login_button, register_button, greeting_logged_in, logout_button, leavingHeadline, leavingText, accept, cancel FROM stnd_generel WHERE id=?")) {
/* bind parameters for markers */
$stmt->bind_param("i", $style_param);
$stmt->execute();
$stmt->bind_result($style, $login, $register, $greet, $logout, $leavHead, $leavTxt, $ac, $canc);
// Loop through each row in the result set
while ($stmt->fetch()) {
$styles = $style;
$login_button = $login;
$register_button = $register;
$greeting = $greet;
$logout_button = $logout;
$leaving_headline = $leavHead;
$leaving_text = $leavTxt;
$leaving_accept = $ac;
$leaving_cancel = $canc;
}
$stmt->close();
}
?>
<? echo $styles; ?>
<style>
<?
if ($user == "") {
?>
#logged-in-menu {
display: none;
}
#login-menu {
display: block;
}
<?
} else {
?>
#logged-in-menu {
display: block;
}
#login-menu {
display: none;
}
<?
}
?>
.user-mobile-logged {
display: none;
}
@media only screen and (max-width: 780px) {
<?
if ($user != "") {
?>
.user-mobile-logged {
display: block;
}
<?
} else {
?>
.user-mobile-logged {
display: none;
}
<?
}
?>
#logged-in-menu, #login-menu {
display: none;
}
}
</style>
</head>
<body>
<div id="top-nav">
<a href="../index.php"><div id="logo"></div></a>
<div id="menu">
<?
$sql = "SELECT * FROM stnd_loggedin_menu ORDER BY position ASC";
// Check if there are results
if ($result = mysqli_query($con, $sql)) {
// Loop through each row in the result set
while($row = mysqli_fetch_assoc($result)) {
?>
<li id="menuDesktop">
<a href="http://www.OOOO.com/standard/user/<? echo $row[site_link]; ?>"><? echo $row[site_name]; ?></a>
</li>
<?
}
}
?>
<li class="droping">
<div class="dropdown">
<button class="dropbtn"><img id="dropImg" src="http://www.OOOO.com/standard/images/nav.png" /></button>
<div class="dropdown-content">
<?
$sql = "SELECT * FROM stnd_menu ORDER BY position ASC";
// Check if there are results
if ($result = mysqli_query($con, $sql)) {
// Loop through each row in the result set
while($row = mysqli_fetch_assoc($result)) {
?>
<a href="../<? echo $row[site_link]; ?>"><? echo $row[site_name]; ?></a>
<?
}
}
?>
</div>
</div>
</li>
<div id="login-menu">
<div id="no-user-login">
<a href="login.php">
<div id="login-button">
<img src="http://www.OOOO.com/standard/images/icons/user-icon.png" /> <? echo strtoupper($login_button); ?>
</div>
</a>
<a href="register.php">
<div id="register-button">
<img src="http://www.OOOO.com/standard/images/icons/register-icon.png" /> <? echo strtoupper($register_button); ?>
</div>
</a>
</div>
</div>
<div id="logged-in-menu">
<div id="user-logged">
<a href="card.php">
<ul>
<li id="user_image">
<img src="http://www.OOOO.com/standard/images/<? if ($profile_pic == "true") { echo "user/" . $user . ".png"; } else { echo "icons/user_icon.png"; } ?>" />
<div id="logout-with-image">
<div style="height: 5px; width: 100%;"> </div>
<a href="logout.php"><div id="logout_button"><? echo strtoupper($logout_button); ?></div></a>
</div>
</li>
<li id="user_info">
<? echo $greeting; ?><br />
<font id="user_name"><? echo $firstname; ?></font>
<br /><br />
<a href="logout.php"><div id="logout_button"><? echo strtoupper($logout_button); ?></div></a>
</li>
</ul>
</a>
</div>
</div>
</div>