php - 如何在 php 中保存 $_SESSION 中的值并检索

标签 php database

我想在登录后显示用户邮箱,登录后的页面应该显示“Welcome alex01@gmail.com” 我的登录页面 php 代码是

<?php
if(isset($_POST['submit']))
{
    $host="localhost";
    $user="root";
    $pass="";
    $db="documentation";
    $usermail=$_POST['email'];
    $password=$_POST['password'];

    $conn=mysqli_connect($host,$user,$pass,$db);
    $query="SELECT * FROM users where
    user_mail='$usermail' and password='$password'";
    $result=mysqli_query($conn,$query);
    if(mysqli_num_rows($result)==1)
    {
        session_start();
        $_SESSION['documentation']='true';
        $_SESSION['user_mail']=$_POST['email'];
        header('location:index.php');
    }
    else{echo 'wrong username or password';}

}
?>

我的index.php的代码(用户登录后看到的页面)

<?php
session_start();
$_SESSION['user_mail']=$_POST['email'];
if(!$_SESSION['documentation'])
{
    header('location:login.php');
}
?>

我正在尝试通过以下方式保存用户邮件

$_SESSION['user_mail']=$_POST['email'];

我正在尝试通过写入来检索 index.php 中的值

$_SESSION['user_mail']=$_POST['email'];

但是没有显示用户名

最佳答案

在你的 index.php 页面中像这样检索;

 $usermail = $_SESSION['user_mail']; 
 echo $usermail;

关于php - 如何在 php 中保存 $_SESSION 中的值并检索,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34349543/

相关文章:

php - Mysql 导出到 csv 组总计

PHP :exif_read_data() not defined

php - 一般错误 : could not call class constructor'

php - 处理数据库查询的最佳方法是什么

mongodb - 搜索数组中的一项包含属性的记录

ruby-on-rails - 将 Rails 应用程序部署到不使用数据库的 Heroku

php - 使用 php 进行 upsert 后获取 mongodb _id 对象

php - 在php/mysql中水平显示记录

mysql - SQL 列值必须存在于两个表中

mysql - 从表中选择具有不同列值的行