当我仅使用用户名和密码登录时,如何在会话中使用$ email和$ image之类的东西。看来我只能使用$ username()
我不知道如何收集会话的最后用户信息?
server.php
<?php
session_start();
// variable declaration
$username = "";
$email = "";
$errors = array();
$_SESSION['success'] = "";
// connect to database
$db = mysqli_connect('Localhost', 'user', 'pass', 'db');
// REGISTER USER
if (isset($_POST['reg_user'])) {
// receive all input values from the form
$username = mysqli_real_escape_string($db, $_POST['username']);
$email = mysqli_real_escape_string($db, $_POST['email']);
$password_1 = mysqli_real_escape_string($db, $_POST['password_1']);
$password_2 = mysqli_real_escape_string($db, $_POST['password_2']);
$uploaddir = 'assets/images/users/';
$image = $uploaddir . basename($_FILES['userfile']['name']);
move_uploaded_file($_FILES['userfile']['tmp_name'], $image);
index.php
<?php
session_start();
if (!isset($_SESSION['username'])) {
$_SESSION['msg'] = "You must log in first";
header('location: pages-login.php');
}
if (isset($_GET['logout'])) {
session_destroy();
unset($_SESSION['username']);
header("location: pages-login.php");
}
?>
...
<?php if (isset($_SESSION['success'])) : ?>
<div class="success" >
<h3>
<?php
echo $_SESSION['success'];
unset($_SESSION['success']);
?>
</h3>
</div>
<?php endif ?>
<ol class="breadcrumb">
<li class="breadcrumb-item active">
<!-- logged in user information -->
<?php if (isset($_SESSION['username'])) : ?>
<p>Hej <strong><?php echo $_SESSION['username']; ?>! </strong>Velkommen til Kommandocentralen</p>
<?php endif ?>
<?php echo $_SESSION['email']; ?>!
</li>
</ol>
我无法像给我$ username一样给$ email。
我在php位之间切出了很多代码
答案 0 :(得分:1)
遵循与教程相同的指导,在server.php
页上,当用户注册或登录时,在显示$_SESSION['username'] = $username;
的行之后,您可以按以下方式添加其他会话数据: / p>
注册用户时:
// REGISTER USER
if (isset($_POST['reg_user'])) {
...
$_SESSION['username'] = $username;
$_SESSION['email'] = $email;
...
}
用户登录时:
// LOGIN USER
if (isset($_POST['login_user'])) {
...
$row = $results->fetch_array();
$_SESSION['username'] = $username;
$_SESSION['email'] = $row['email'];
...
}
对于用户登录名$results[0]['email']
,引用数据库表的email
列,因此,要提供的任何数据都将使用表中的相同数据。例如,您将image
列称为$results[0]['image']
。
可以按照上述模式设置所有其他会话变量。
希望这会有所帮助。
答案 1 :(得分:0)
是的,它可以工作-将图像存储在我请求的文件夹中。但是现在我不确定如何显示吗?
// REGISTER USER
...
$uploaddir = 'assets/images/users/';
$image = $uploaddir . basename($_FILES['userfile']['name']);
move_uploaded_file($_FILES['userfile']['tmp_name'], $image);
...
$_SESSION['username'] = $username;
$_SESSION['email'] = $email;
$_SESSION['userfile'] = $image;
...
// LOGIN USER
...
$row = $results->fetch_array();
...
$_SESSION['username'] = $username;
$_SESSION['email'] = $row['email'];
$_SESSION['userfile'] = $row['userfile'];
...
然后在index.php
...
<img src="<?php echo $_SESSION['userfile']; ?>" />
...
但是什么也没显示-您能知道我的错误是什么吗?