如何在PHP会话中访问变量

时间:2017-02-01 21:51:55

标签: 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;
?>

1 个答案:

答案 0 :(得分:2)

在php文件上添加以下代码

$_SESSION['ProfileName'];

那么你应该可以通过

访问所有内容
var_dump($_SESSION);

查看所有可用变量

@Entity
class Book {

  ...

  @Size(min = 1)
  @ManyToMany
  private List<Author> authors;

   ...
}

@Entity
class Author {

  ...

  @Size(min = 1)
  @ManyToMany(mappedBy = "authors")
  private List<Book> books;

  ...
}