I can't display session variables

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
cap2cap10
Forum Contributor
Posts: 158
Joined: Mon Apr 14, 2008 11:06 pm

I can't display session variables

Post by cap2cap10 »

OK so I know the session array works because if i use print r, I see all data that has been passed to the new page. Now how do I access specific data in the array to place in specific parts of my html.

Here is the sessions array code:

Code: Select all

    $q = "SELECT * FROM `login` WHERE `username` = '".$_SESSION['login_username']."' AND `password` = '".$_SESSION['login_password']."'";
                    $q = mysql_query($q);
                    if(mysql_num_rows($q) == 0) {
                        $_SESSION = array();
                    } else {
                        $ar = mysql_fetch_array($q);
                        $_SESSION['login'] = $ar;
                        $this->LOGGEDIN = true;
                        $this->FAILED = false;
:banghead: Can someone enlighten me on how to display specific data from sessions array.

This:

Code: Select all

<?php print " <b>".$ar['FName'] . "</b> ";?>
does not work
User avatar
deeessay
Forum Commoner
Posts: 55
Joined: Sat May 24, 2008 1:02 am

Re: I can't display session variables

Post by deeessay »

Hi the solution to your problem is quite simple. I encountered this when I was just starting out. Just enclose them session variables in curly braces like so:


$q = "SELECT * FROM `login` WHERE `username` = '{$_SESSION['login_username']}' AND `password` = '{$_SESSION['login_password']}'";


and voila! Life's beautiful again! :D
User avatar
starram
Forum Commoner
Posts: 58
Joined: Thu Apr 10, 2008 1:27 am
Location: India
Contact:

Re: I can't display session variables

Post by starram »

Hello try this

echo $_SESSION['login']['username'];
echo $_SESSION['login']['password'];

When you assign an array to another array it becomes multi-dimentional array.

$_SESSION is an Global Variable and when you store something like this

$_SESSION['login']=1;
Then it becomes an array & when you assign an array to it it becomes multi-dimentional array.

This will surely help you and clear your concept about it.
Post Reply