通过会话跨页面存储和访问变量

I have this function

$username = PSF::requestGetPOST('username');

It has the email of user that is being logged in. it is working fine.

now, i want to store this $username into a session so that i can access it on another page.

how i tried

$_SESSION["email"] = $username ; 

and access on the other page

$email = $_SESSION["email"] ;

doesn't seem to work, what am i doing wrong?

You Need To Put

session_start();

before any Output is start.

Otherwise You will get header already sent error Then You can Assign variable to session like below

$_SESSION["email"] = $username ;

If you wanna use sessions, at the beginning of your page you have to use session_start();

For example :

session_start();
$_SESSION["email"] = $username ; 

http://php.net/manual/en/function.session-start.php

You need to add this first before using $_SESSION

session_start();

See documentation

In order to make session work you should do

session_start()

somewhere in your script or set session.auto_start to '1' in php.ini