PHP Print Session Variable to screen

Posted on 2007-10-04
Last Modified: 2008-01-09
I'm just trying to echo the current session variables. Believe me Ive been reading and reading and still can get results. What am I missing?

session_start();  // Do I need to start a session if one is already started by the log in??
echo "The value of the session variable is: " . $_SESSION['username'];   // I get nothing with this.
print_r ($_SESSION);  // this give me --->   Array( )
Question by:Russelauto
    LVL 13

    Expert Comment

    echo $_SESSION['username'];

    or if you want every value in the sessions:
    LVL 14

    Expert Comment

    you need to set the path as to where the session values would be stored in the php.ini file

    in linux, you would normally see this in the /etc/ directory and in windows within the php directory itself

    the directory should be writeable by the httpd or the apache process.  check for owner and write permissions in that directory.  if that is there then you need to set some values first and then call it.  otherwise the session_start(); function normally initialises the $_SESSION array but nothing is stored.

    in your php page  do the following

    echo $_SESSION['username'] . "is the user name from cookie<br>";
    $_SESSION['username'] = "mickey mouse";

    echo $_SESSION['username'] . "is the user name from cookie after setting the value<br>";  

    and you would see the difference.  you can use anything like this in the $_SESSION variable

    $_SESSION['myvariablename'] = "xxx_yyy_zzz";

    $somevariable = $_SESSION['myvariablename'];  

    and proceed to use that.  the variable values set through the session are also available in the second and subsequent pages till the session is closed.

    You have to set the variables in the session and call them back.  they are not set by default over there
    LVL 13

    Expert Comment

    Sorry - I didn't read the question closely before posting.

    Author Comment

    I don't want to assign the variables, I want to display the variables that have already been assigned when the user logged in. It may be a matter of just knowing the variable name, but I've tried username, user, and id. I thought there may be a way to echo everything out to see what variables were actually assigned.
    LVL 13

    Accepted Solution

    Yes there is. That would be "var_dump($_SESSION);"

    If it comes back with "array(0)" then it means nothing is there.

    Author Comment

    That exactly right... I got    array(0)    I fail to understand why there are none. I assumed that the site worked off of session variables.

    Expert Comment

    Check whether session_start() is given in the page where u assigned the session values.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    What Security Threats Are You Missing?

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    The Client Need Led Us to RSS I recently had an investment company ask me how they might notify their constituents about their newsworthy publications.  Probably you would think "Facebook" or "Twitter" but this is an interesting client.  Their cons…
    Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
    The viewer will learn how to count occurrences of each item in an array.
    The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

    759 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    10 Experts available now in Live!

    Get 1:1 Help Now