我一直在查看过去三天的其他示例,检查文档等。我想要做的就是在登录后将会话变量设置为用户信息。我的所有连接都很好,因为我可以保持登录状态并轻松返回我的用户名的会话变量。其他信息不起作用。我觉得这可能是非常愚蠢的事情。
这是我的代码:
$user_data = array();
$get_user_data = "SELECT
user_id
, user_level
, user_type
FROM users
WHERE user_name = :user_name";
$query = $pdo->prepare($get_user_data);
$query->bindParam(":user_name", $username);
$query->execute();
while($row = $query->fetch(PDO::FETCH_ASSOC)){
$user_id =$row['user_id'];
$user_level =$row['user_level'];
$user_type =$row['user_type'];
//do something with the variables
$_SESSION['user_id'] = $user_id;
$_SESSION['user_level'] = $user_level;
$_SESSION['user_type'] = $user_type;
// var_dump inside the brackets here returns the variable values fine
}
// var_dump outside the brackets here returns the variables as NULL
这是该页面的完整代码:
<?php
session_start();
error_reporting(E_ERROR | E_WARNING | E_PARSE | E_NOTICE);
//include config
require_once('includes/config.php');
//include header template
require('includes/header.php');
//check if already logged in move to home page
if( $user->is_logged_in() ){
echo '<div id="errors">You are already logged in. </div>';
}
//process login form if submitted
if(isset($_POST['submit'])){
$username = $_POST['user_name'];
$password = $_POST['user_password'];
if($user->login($username,$password)){
$_SESSION['user_name'] = $username; // set session variable of username to that of logged in user
$user_data = array();
$get_user_data = "SELECT
user_id
, user_level
, user_type
FROM users
WHERE user_name = :user_name";
$query = $pdo->prepare($get_user_data);
$query->bindParam(":user_name", $username);
$query->execute();
while($row = $query->fetch(PDO::FETCH_ASSOC)){
$user_id =$row['user_id'];
$user_level =$row['user_level'];
$user_type =$row['user_type'];
//do something with the variables
$_SESSION['user_id'] = $user_id;
$_SESSION['user_level'] = $user_level;
$_SESSION['user_type'] = $user_type;
// var_dump inside the brackets here returns the variable values fine
}
// var_dump outside the brackets here returns the variables as NULL
// ordinarily I redirect the user, but for testing this is turned off.
//header('Location: chapter.php');
//exit;
} else {
$error[] = 'Wrong username or password or your account has not been activated.';
}
}//end if submit
//define page title
$title = 'Login';
?>
<div id = "wrapper">
<div id = "errors">
<?php
var_dump($row['user_id']);
var_dump($row['user_level']);
var_dump($row['user_type']);
var_dump($_SESSION['user_name']);
?></div>
<form role="form" method="post" action="" autocomplete="off">
<?php
//check for any errors
if(isset($error)){
foreach($error as $error){
echo '<p class="bg-danger">'.$error.'</p>';
}
}
if(isset($_GET['action'])){
//check the action
switch ($_GET['action']) {
case 'active':
echo "<h2 class='bg-success'>Your account is now active you may now log in.</h2>";
break;
case 'reset':
echo "<h2 class='bg-success'>Please check your inbox for a reset link.</h2>";
break;
case 'resetAccount':
echo "<h2 class='bg-success'>Password changed, you may now login.</h2>";
break;
}
}
?>
<div id = "form-section">
<div id = "form-header"><img src="img/ds-icon20px.png"> Log in.</div><HR SIZE = "1" WIDTH="100%" NOSHADE>
<small>Username: </small><br />
<input type="text" name="user_name" id="username" placeholder="User Name" value="<?php if(isset($error)){ echo $_POST['user_name']; } ?>" tabindex="1">
<small>Password: </small><br />
<input type="password" name="user_password" id="password" placeholder="Password" tabindex="2">
<br /><br />
<input type="submit" name="submit" value="Login" tabindex="3"> <a href='reset.php'>Forgot your Password?</a>
</div>
</form>
</div>
<?php
//include footer template
require('includes/footer.php');
?>
答案 0 :(得分:1)
抱歉,不适合发表评论
忽略会话一秒钟。如果你在while循环之前声明一个变量,比如$ testdata =&#39; beforewhile&#39 ;;然后将其设置为$ user_id。它会在一段时间后存在吗?
$testdata='beforewhile';
while($row = $query->fetch(PDO::FETCH_ASSOC)){
echo 'FIRST '.$testdata;
$user_id =$row['user_id'];
$user_level =$row['user_level'];
$user_type =$row['user_type'];
//do something with the variables
$_SESSION['user_id'] = $user_id;
$_SESSION['user_level'] = $user_level;
$_SESSION['user_type'] = $user_type;
$testdata=$user_id;
// var_dump inside the brackets here returns the variable values fine
echo 'LAST '.$testdata;
}
echo 'after '.$testdata;
答案 1 :(得分:0)
您的变量仅存在于while
循环内,尝试在循环外和循环之前定义它们。
您要多次设置每个变量的值,我认为您需要使用数组来存储所有行:
$rowList = array();
while($row = $query->fetch(PDO::FETCH_ASSOC)){
$rowList[] = $row;
}
然后你可以显示这样的值:
echo $rowList[0]['user_id'];
echo $rowList[0]['user_level'];
如果您的查询只返回一行,则可以使用if
代替while
:
$user_id = null;
$user_level = null;
$user_type = null;
if($row = $query->fetch(PDO::FETCH_ASSOC)){
$user_id =$row['user_id'];
$user_level =$row['user_level'];
$user_type =$row['user_type'];
//do something with the variables
$_SESSION['user_id'] = $user_id;
$_SESSION['user_level'] = $user_level;
$_SESSION['user_type'] = $user_type;
}
var_dump($user_id);
var_dump($user_level);
var_dump($user_type);