你好家伙已经阅读了几篇帖子和搜索谷歌的答案,但没有运气。
基本上我的项目一切都很顺利。我想显示登录用户的名字,姓氏和头像。这是我试图存档的内容,http://prntscr.com/cs4xhx目前在html中。
这是我的 header.php
中已添加的内容 <?php if(isset($_SESSION['user_type'])){ ?>
<a href class="dropdown-toggle" data-toggle="dropdown">
<img src="<?php echo $_SESSION['avatar']; ?>" alt="" class="img-circle size-30x30">
<span><?php echo $_SESSION['firstname'];?> <i class="fa fa-angle-down"></i></span>
</a>
<?php //} ?>
以下是我在 profile.php
中的内容 <?php
//error_reporting(E_ALL); ini_set('display_errors', 1);
session_start();
if(!isset($_SESSION['user_type'])){
header('Location: index.php');
}
// include file
require_once('include/connection.php');
// page title
$title = "Welcome to your Profile";
// define user session
$userId = $_SESSION['user_id'];
$userName = $_SESSION['user_name'];
$firstname = $_SESSION['firstname'];
$lastname = $_SESSION['lastname'];
//include header layout
require_once('include/header.php');
?>
<?php
if($_SESSION['user_type'] == 'admin' || $_SESSION['user_type'] == 'leader'){
// include admin header layout
require_once('include/admin-header.php');
}elseif($_SESSION['user_type'] == 'member'){
// include member header layout
include_once('include/member-header.php');
}else{
session_destroy();
header('Location: index.php');
}
?>
<!-- check if user id is found, if not throw error ---->
<?php
$firstname = trim($_POST['firstname']);
$lastname = trim($_POST['lastname']);
$email = trim($_POST['email']);
$user_name = trim($_POST['user_name']);
$profession = trim($_POST['profession']);
$phone = trim($_POST['phone']);
$address = trim($_POST['address']);
$bio = trim($_POST['bio']);
$dob = trim($_POST['dob']);
$gender = trim($_POST['gender']);
$country = trim($_POST['country']);
$stmt = $con->prepare("SELECT firstname, lastname, user_name, avatar, profession, email, dob, gender, country, phone, bio, address, created_at FROM user WHERE id = ?");
$stmt->bind_param('s', $userId);
$stmt->execute();
$stmt->store_result();
if($stmt->num_rows == 0) {
echo 'No Data Found for this user';
}else {
$stmt->bind_result($firstname, $lastname, $user_name, $avatar, $profession, $email, $dob, $gender, $country, $phone, $bio, $address, $created_at);
$stmt->fetch();
//echo $stmt->num_rows
// $getUserQuery = "select * from user where id = $userId";
// $resultData = mysqli_query($con, $getUserQuery);
// if(!mysqli_num_rows($resultData)){
// echo 'No Data Found for this user';
// }else{
// $row = mysqli_fetch_array($resultData);
?>
<!-- self post back url -->
<?php
$url = 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF'];
?>
<table>
<tr>
<td>
<center>
<img src="userfiles/avatars/<?php echo $avatar ;?>" width="150" height="150">
</center>
</td>
</tr>
<tr>
<td><label><strong>First Name</strong></label></td>
<td></td>
<td><label><?php echo $firstname; ?> </td>
</tr>
<tr>
<td><label><strong>Last Name</strong></label></td>
<td></td>
<td><label><?php echo $lastname;?> </td>
</tr>
<tr>
<td><label><strong>User Name</strong></label></td>
<td></td>
<td><label><?php echo $user_name;?> </td>
</tr>
<tr>
<td><label><strong>Profession</strong></label></td>
<td></td>
<td><label><?php echo $profession;?> </td>
</tr>
<tr>
<tr>
<td><label><strong>Phone</strong></label></td>
<td></td>
<td><label><?php echo $phone;?> </td>
</tr>
<tr>
<td><label><strong>Gender</strong></label></td>
<td></td>
<td><label><?php echo $gender;?> </td>
</tr>
<tr>
<td><label><strong>Date Of Birth</strong></label></td>
<td></td>
<td><label><?php echo $dob;?> </td>
</tr>
<tr>
<td><label><strong>Email</strong></label></td>
<td></td>
<td><label><?php echo $email;?> </td>
</tr>
<tr>
<td><label><strong>Country</strong></label></td>
<td></td>
<td><label><?php echo $country;?> </td>
</tr>
<tr>
<td><label><strong>Address</strong></label></td>
<td></td>
<td><label><?php echo $address;?> </td>
</tr>
<tr>
<td><label><strong>Biography</strong></label></td>
<td></td>
<td><label><?php echo $bio;?> </td>
</tr>
<tr>
<td><label><strong>Join Date</strong></label></td>
<td></td>
<td><label><?php echo $created_at;?> </td>
</tr>
</table>
<?php
}
$mysqli->close();
?>
<?php
// include footer layout
require_once('include/footer.php');
?>
将<{1}}替换为<?php echo $_SESSION['firstname'];?>
时, header.php 中的非常有趣。它工作,但当我尝试显示名称和头像时,它确实没有定义会话变量或?
我希望有些人可以在这里分享一下。提前谢谢。
更新 确定我设置会话的位置和方式 的的header.php
<?php echo $_SESSION['user_name'];?>
这是我的 index.php
<script type="text/javascript" src="include/plugins/ckeditor/ckeditor.js"></script>
<link rel="stylesheet" href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css">
<script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
<body>
<?php if(isset($_SESSION['user_type'])){ ?>
<div class="logout-link">
<p><?php echo $_SESSION['firstname'];?></p>
<img src="userfiles/avatars/<?php echo $_SESSION['avatar'] ;?>">
<p><a href="include/logout.php">Logout</a></p>
</div>
<?php } ?>
<?php if(isset($_SESSION['main_notice'])) { ?>
<div class="main-notice">
<p>
<?php
echo $_SESSION['main_notice'];
unset($_SESSION['main_notice']);
?>
</p>
</div>
<?php } ?>
<ul class="menu">
<li><a href="admin-dashboard-home.php">Home</a></li>
<li><a href="profile.php">Profile</a></li>
<li><a href="edit-profile.php">Edit Profile</a></li>
<li><a href="admin-post-task.php">Post Task</a></li>
<li><a href='user-profile.php'>Users Profile</a></li>
<!-- <li><a href="reset.php">Reset Password</a></li> -->
</ul>
答案 0 :(得分:0)
请参阅 index.php 页面
上的以下行$_SESSION["firstname"] = $firstname;
$_SESSION["lastname"] = $lastname;
您的代码中没有任何地方声明$firstname
和$lastname
,您只是神奇地使用它们。所以删除这两行,它们就没用了。
现在谈到你的问题。请看以下声明,
$stmt = $con->prepare("SELECT id, user_name, password, user_type FROM user ...
您在firstname
查询中未选择lastname
,SELECT
,也未在->bind_result()
方法中限定适当的变量。所以你的代码应该是这样的:
// your code
$stmt = $con->prepare("SELECT id, user_name, firstname, lastname, password, user_type FROM user WHERE user_name = ? ");
$stmt->bind_param('s', $user_name);
$stmt->execute();
$stmt->store_result();
if($stmt->num_rows){
// username exists
$stmt->bind_result($id, $username, $firstname, $lastname, $dbPassword, $userType);
$stmt->fetch();
if (password_verify($password, $dbPassword)) {
$_SESSION['user_id'] = $id;
$_SESSION['user_name'] = strtoupper($username);
$_SESSION["firstname"] = $firstname;
$_SESSION["lastname"] = $lastname;
$user_type = strtolower($userType);
if(strtolower($user_type) == 'member'){
$_SESSION['user_type'] = 'member';
//header('Location: member-dashboard-home.php');
header('Location: profile.php');
exit();
}elseif(strtolower($user_type) == 'admin' || strtolower($user_type) == 'leader'){
$_SESSION['user_type'] = strtolower($user_type);
//header('Location: admin-dashboard-home.php');
header('Location: profile.php');
exit();
}
}else{
$_SESSION['main_notice'] = "Invalid login details!";
header('Location: '.$_SERVER['PHP_SELF']);
exit();
}
}else{
// username doesn't exist
$_SESSION['main_notice'] = "Invalid username OR password details, please try again!";
header('Location: '.$_SERVER['PHP_SELF']);
exit();
}
// your code
注意:要调试这些问题,请始终在PHP脚本的最顶部添加这两个错误报告行。
ini_set('display_errors', 1);
error_reporting(E_ALL);