[英]How to save value in $_SESSION in php and retrieve
I want to show the usermail
once I login and the page after login should show "Welcome alex01@gmail.com" my login page php code is 我想在登录后显示
usermail
,登录后的页面应显示“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';}
}
?>
The code of my index.php (the page user see after login) 我的index.php的代码(登录后用户看到的页面)
<?php
session_start();
$_SESSION['user_mail']=$_POST['email'];
if(!$_SESSION['documentation'])
{
header('location:login.php');
}
?>
I am trying to save the usermail
by 我正在尝试保存用户
usermail
$_SESSION['user_mail']=$_POST['email'];
and I am trying to retrieve the value in index.php by writing 我试图通过写入检索index.php中的值
$_SESSION['user_mail']=$_POST['email'];
but it is not showing the user name 但它没有显示用户名
In your index.php
page retrive like this; 在你的
index.php
页面中像这样重写;
$usermail = $_SESSION['user_mail'];
echo $usermail;
通过echo $_SESSION['user_mail'];
检索会话echo $_SESSION['user_mail'];
to display session data, just write 显示会话数据,只需写入
echo $_SESSION['user_mail'];
Also ensure to start session on page where session is used. 还要确保在使用会话的页面上启动会话。
You are overwriting the value of $_SESSION['user_mail] in index page with $_POST['email]. 您正在使用$ _POST ['email]覆盖索引页中$ _SESSION ['user_mail]的值。 The problem is that $_POST['email'] is not set on index page, its only set on the login page.
问题是$ _POST ['email']没有在索引页面上设置,它只在登录页面上设置。
So on index.php page you simply have to either 所以在index.php页面上你只需要
echo $_SESSION['user_mail'];
or 要么
$var = $_SESSION['user_mail']; echo $var;
For save in session variable:- 对于保存在会话变量中: -
$usermail = $_SESSION['user_mail'];
and for show in same page . 并在同一页面显示。
echo $usermail;
in other page :- 在其他页面: -
//first start session
session_start();
echo $_SESSION["username"];
//or
session_start();
$loginname=$_SESSION["username"];
echo $loginname;
Please, make sure the session is started on the page where session is being used. 请确保在使用会话的页面上启动会话。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.