php - 无法在 session 中存储用户 ID

标签 php html mysql session-variables

我正在尝试获取登录用户的用户 ID 并将其存储在 session 中,以便当用户提交某些内容时我可以将他们的用户 ID 添加到数据库的提交中。我的登录页面非常粗糙,请友善,因为我对这一切都很陌生!我已经尝试了很多不同的组合,但根据我在网上看到的答案,我目前所拥有的应该非常接近。如果有人能给我任何指示,那将是一个很大的帮助。谢谢!

登录页面

<?php

session_start();

//Connection and select database go in here

// username and password sent from form
$myusername=$_POST['Email'];
$mypassword=$_POST['User_Password'];

// To protect MySQL injection 
$myusername = stripslashes($myusername);
$mypassword = stripslashes($mypassword);
$myusername = mysql_real_escape_string($myusername);
$mypassword = mysql_real_escape_string($mypassword);

$sql="SELECT User_ID, Email, User_Password FROM $tbl_name WHERE Email='$myusername' and User_Password='$mypassword'";
$result=mysql_query($sql);

// Mysql_num_row counts table row
$count=mysql_num_rows($result);

// If result matched $myusername and $mypassword, table row must be 1 row
if($count==1){

// is_auth to make sure they can view other pages that need credentials.
$_SESSION['is_auth'] = true;
$_SESSION['User_ID'] = $result->User_ID;

// Once the sessions variables have been set, redirect them to the landing page / home page.
header('location: ../View/main.php');
exit;
 }

else {
$error = "Please enter an email and password to login.";
}
header("location:../View/mainUnauthenticated.php");

检查用户是否通过身份验证的页面。我在每个相关页面的开头调用它

<?php

    session_start();
echo $_SESSION['User_ID'];

// Test the session to see if is_auth flag was set (meaning they logged in successfully)

// If test fails, send the user to homepage and prevent rest of page being shown.

if (!isset($_SESSION["is_auth"])) {
header("location: ../View/mainUnauthenticated.php");
exit;
    }
else if (isset($_REQUEST['logout']) && $_REQUEST['logout'] == "true") {
// At any time we can logout by sending a "logout" value which will unset the "is_auth" flag.
// We can also destroy the session if so desired.
unset($_SESSION['is_auth']);
session_destroy();
// After logout, send them back to homepage
header("location: ../View/mainUnauthenticated.php");
exit;
}
?>

这是我希望能够获取我(以为我已经拥有!)存储的用户 ID 的页面之一的片段。现在我只是想让它显示在文本字段中以表明它正在工作。

<?php include('../Controller/is_auth.php') 

?>


<p>
<input type="text" name="User_ID" id="User_ID" value="<?php echo $_SESSION['User_ID'];?>"/>
</p>

最佳答案

首先,我推荐您使用PDO而不是旧的 mysql_query。我比较安全的是SQL injection

对于错误:创建一些简单的“调试”函数来查看变量的输出是个好主意。例如,在您的登录脚本中,您使用 var $result->User_ID。您确定该变量存在吗?这是我可以使用的简单调试功能:

function debug ($var, $name = "Debug") {
    echo "<h2>$name</h2><pre>";
    var_dump($var);
    echo "</pre>";
}

使用此功能,您可以简单地检查是否得到了异常并发现了问题。只需调用电话

debug($result, "Database output");//or without 2nd parameter

PS:创建一些您将包含在每个代码中的functions.php。当您更改某些功能时,您不需要重写每个文件。

关于php - 无法在 session 中存储用户 ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40828775/

相关文章:

php - 加密 MySQL 表中数据的好方法?

mysql - UNION查询合并两个表的结果按源表排序

php - 无效参数和非法偏移量

php - 将两个值从 doInBackground 传递到 onPostExecute

javascript - 无法动态操作多边形的坐标

html - 如何做一个绝对定位(溢出:hidden) container's absolutely positioned children show outside of their parent's area?

html - Fontawesome 在最新的 IE 中不起作用

php - 如何制作带有联系时间限制的联系页面

php - 使用 Codeigniter 更新 MySql 中的数据不起作用

c++ - SOCI 外部符号未解析 "struct soci::mysql_backend_factory const soci::mysql"