我在这里缺少一些东西,我似乎无法调用用户ID并将其保存在会话中。该计划是登录的,以存储用户ID以进行将来的更改。现在,我似乎无法将用户ID存储在会话中。我会注意到:未定义的索引:login.php的USERID在第14行。
<?php
session_start();
if (!isset($_POST['submit'])){
} else {
require_once("db_const.php");
$mysqli = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);
# check connection
if ($mysqli->connect_errno) {
echo "<p>MySQL error no {$mysqli->connect_errno} : {$mysqli->connect_error}</p>";
exit();
}
$userID = $_SESSION["userID"];
$username = $_POST['username'];
$password = $_POST['password'];
$sql = "SELECT * from userinfo WHERE username LIKE '{$username}' AND password LIKE '{$password}' LIMIT 1";
$result = $mysqli->query($sql);
if (!$result->num_rows == 1) {
echo "<p>Invalid username/password combination</p>";
} else {
setcookie("username", time() +60*60*24*30*365);
$_SESSION['userID'] = $userID;
echo "<p>Logged in successfully!, Please close the window</p>";
}
}
?>
从会话本身设置后,您永远不会更新$userID
。要解决此问题,fetch
结果并存储获取的ID:
$result = $mysqli->query($sql);
if (!$result->num_rows == 1) {
echo "<p>Invalid username/password combination</p>";
} else {
$row = $result->fetch_assoc();
$_SESSION['userID'] = $row['userid']; //case sensitive
//etc.
错误在行上
$userID = $_SESSION["userID"];
似乎未设置会话userID
。在分配之前,请务必检查会话变量是否使用isset
语言构造。
if(isset($_SESSION["userID"]))
{
$userID = $_SESSION["userID"];
}
也为您的其他会话变量执行此操作,此测试甚至适用于 $_POST