[英]How To Access Variables in A PHP Session
當用戶使用電子郵件和密碼登錄時,我使用Php Sessions來識別用戶。 工作正常。 作為Php的新手,我需要知道如何通過Session訪問其ProfileName。 我在header.php中使用的代碼返回空白,沒有名稱。
header.php
<?php
$info = myprofile($_SESSION['memberid']);
$title = $myprofile.' - '.stripslashes($info['ProfileName']);
echo $_SESSION[$title];?>
profile.php
<?php
require_once "../includes/config.php";
if(!$_SESSION['memberid'] || $_SESSION['memberid']==0){
header('Location: '.$base_url.'signin.php');
exit();
}
require_once "../includes/database.php";
require_once "../includes/functions.php";
$info = myprofile($_SESSION['memberid']);
$title = $myprofile.' - '.stripslashes($info['ProfileName']);
require_once '../includes/header.php';
$ismenu = 2;
?>
signin.php
<?php
require_once "includes/config.php";
require_once "includes/database.php";
require_once "includes/functions.php";
if($_SERVER['REQUEST_METHOD']=='POST' && isset($_POST['smsignin'])){
$err = 0;
if(empty($_POST['email'])){
$err_e = $emailnull;
$err = 1;
}
elseif(!valid_email(trim($_POST['email']))){
$err_e = $emailinvalid;
$err = 1;
}
elseif(empty($_POST['password'])){
$err_p = $passnull;
$err = 1;
}
elseif($_SESSION['loginfalse']>=$totallogin){
if(empty($_POST['captcha'])){
$err_c = $captchanull;
$err = 1;
}
elseif(strlen($_POST['captcha'])<6){
$err_c = $datashort;
$err = 1;
}
elseif($_POST['captcha'] != $_SESSION['encoded_captcha']){
$err_c = $captnomatch;
$err = 1;
}
}
if(intval($err)==0){
$data = array(mysql_real_escape_string(trim($_POST['email'])), mysql_real_escape_string(trim($_POST['password'])));
$result = UserLogin($data);
if($result==0)
$error = $errordata;
elseif($result==-1){
$_SESSION['loginfalse'] = isset($_SESSION['loginfalse'])?$_SESSION['loginfalse']+1:1;
$error = $mailpassnotmatch;
}
else{
$banned = chkBannedMe($result);
if(count($banned)>0)
$error = str_replace('<lydo>', $banned[0], str_replace('<ngay>', date('H:i:s d-m-Y', strtotime($banned[1])), str_replace('<link>', '<a href="'.$base_url.'contact.php">', str_replace('</link>', '</a>', $bannedacc))));
else{
$date = date("Y-m-d H:i:s");
updLogon($result);
UpdateVIPStatus($result, $date);
IsVIP($result);
mysql_close();
unset($_SESSION['loginfalse']);
$_SESSION['memberid'] = $result;
$_SESSION['memberemail'] = $_POST['email'];
$direct = $_SESSION['direct']?$_SESSION['direct']:$base_url.'members/myaccount.php';
header('Location: '.$direct);
exit();
}
}
}
}
$title = $memberlogin;
?>
在您的php文件頂部添加以下代碼
if (session_status() == PHP_SESSION_NONE) {
session_start();
}
那么您應該可以通過訪問所有內容
$_SESSION['ProfileName'];
查看所有可用變量
var_dump($_SESSION);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.