0

I use Php Sessions to recognize users when they log in with email and password. It works fine. Being new to Php I need to know how to access their ProfileName with Session. The code I use in header.php returns blank, no name.

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;
?>
  • See also [Why shouldn't I use mysql_* functions in PHP?](http://stackoverflow.com/q/12859942/1255289) – miken32 Feb 01 '17 at 21:57
  • 1
    You need to call `session_start()` before using sessions. – miken32 Feb 01 '17 at 21:58
  • If an answer solved your problem, consider accepting the answer. Here's how http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work then return here and do the same with the tick/checkmark till it turns green. This informs the community, a solution was found. Otherwise, others may think the question is still open and may want to post (more) answers. You'll earn points and others will be encouraged to help you. *Welcome to Stack!* – Jay Blanchard Feb 02 '17 at 18:09

1 Answers1

2

add the following code on top of your php files

if (session_status() == PHP_SESSION_NONE) {
    session_start();
}

then you should be able to access everything through

$_SESSION['ProfileName'];

to view all available variables do

var_dump($_SESSION);
Dimi
  • 1,255
  • 11
  • 20