PHP会话变量问题
我遇到的问题是,我们在项目中使用PHP会话变量。为用户名存储变量的方式是,当用户使用用户名登录时,用户名存储在会话变量中。我需要从数据库中获取该用户名的id,并在登录用户的另一个页面上使用它。我甚至无法从会话变量中打印用户名。我在这方面一直很努力,不知道该怎么做,我非常感谢你的帮助。我在第二个文件中添加了注释。我也不确定是否需要添加session_start();在我要使用会话变量的每个文件上?一旦我在一个文件中收集了一个变量,我可以在另一个文件中访问它吗 此文件是登录文件:PHP会话变量问题,php,mysql,Php,Mysql,我遇到的问题是,我们在项目中使用PHP会话变量。为用户名存储变量的方式是,当用户使用用户名登录时,用户名存储在会话变量中。我需要从数据库中获取该用户名的id,并在登录用户的另一个页面上使用它。我甚至无法从会话变量中打印用户名。我在这方面一直很努力,不知道该怎么做,我非常感谢你的帮助。我在第二个文件中添加了注释。我也不确定是否需要添加session_start();在我要使用会话变量的每个文件上?一旦我在一个文件中收集了一个变量,我可以在另一个文件中访问它吗 此文件是登录文件: <body&
<body>
<h1>Login</h1>
<form action="VerifyUser.php" method="post">
Username: <input type="text" name="userName"><br>
Password: <input type="password" name="pwd"><br><br>
<input type="submit" name="loginButton" value="Login"><br><br>
<a href="forgotPassword.php">
Forgot Password? Click Here!</a>
<a href="Registration.php"><br><br>
Need to Register? Click Here!</a>
</form>
</body>
登录
用户名:
密码:
这是它在verify user上发布到的文件:(当我登录新页面时,仅在提取帖子之前从echo打印“Working”
<?php
require_once('dbBaseDao.php');
require_once('dbUserDao.php');
$userDao = new dbUserDao();
echo "<p>Working...</p>";
extract($_POST);
if( $userDao->{'verifyUser'}($userName, $pwd))
{
session_start();
$_SESSION['userName'] = $userName;
$result = $userDao->{'getUserByWhere'}("username ='" . $userName . "'");
$user = mysql_fetch_array($result, MYSQL_ASSOC);
print $userName; //This is not printing
$_SESSION['userId'] = $user['userId']; //I think to get this I need
//to query the database for whatever the username is here it is equal to that
//in the database and get the userId from that, but I have no idea how to do this.
header('Refresh: 1;url=LoggedIn.php');
exit();
}
else
{
header('Refresh: 1;url=LogIn.php');
exit();
}
?>
我还不确定是否需要在我要使用会话变量的每个文件上添加会话_start()
是的,您必须在需要访问这些会话的每个页面上使用
session_start();
。@user25220055:尝试ob_stat()方法。$user['userId'];您听起来好像不确定应该如何处理查询结果。我可以建议使用var_dump($user)吗;这将有两个目的,一个是显示对象中的所有数据,以便您可以看到它的结构,等等。基本上,您将获得与“print”语句相同的效果,但有更多的信息。print表示您在方法中得到了,var_dump表示您在方法中得到了,并表示您的数据是什么(即使为空/空)@Culyx是的,这是我测试过的东西,我想我会自动获取用户ID,但对于我来说,要在那一行之前获取我知道的用户ID,我需要匹配表中的用户名并获取用户ID。因此,如果我对var_dump($user)理解正确,我会在我的会话之后放置它所以我可以得到它将返回的内容?@Pank我在谷歌上查找ob_stat()的描述,但除了ob_start之外没有找到任何其他内容。该方法是做什么的,以便我知道在哪里使用它?您可以将var_dump直接放在这行下:$user=mysql_fetch_array($result,mysql_ASSOC);一旦您从SQL查询中为$user变量分配了一个值,您的var_转储将显示该值中的内容感谢您的澄清!
<?php include('header.php'); ?>
<?php
require_once('dbBaseDao.php');
require_once('dbUserDao.php');
require_once('dbBotDao.php');
$userDao = new dbUserDao();
$botDao = new dbBotDao();
session_start();
$userID = $_SESSION['userId'];
print $userID;
/*
* Description of code below: This code checks the name checkbox[] from the form in UpcomingGames.php for any
* selected games made by clicking on the checkbox and submitting the form. The first echo actually prints all
* the games selected by the user from UpcomingGames.php.
*/
if(!empty($_POST['checkbox']))
{
foreach($_POST['checkbox'] as $check)
{
echo "</br>";
echo ($check);
$userID = 2;
$sql= "INSERT INTO UserPicks (userID, game) VALUES ('$userID','$check')";
$result = mysql_query($sql);
if (!$result) {
$message = 'Invalid query: ' . mysql_error() . "\n";
$message .= 'Whole query: ' . $sql;
die($message);
}
}
echo "</br>";
echo "</br>";
echo "You predicted ";
echo sizeof($_POST['checkbox']);
echo " games to have upsets";
echo "</br>";
echo "</br>";
}
?>