Getting user information, need some assistance.

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
synical21
Forum Contributor
Posts: 150
Joined: Tue Jul 28, 2009 8:44 am
Location: London UK

Getting user information, need some assistance.

Post by synical21 »

Ive done my user managment system for my site and now i want to bring back some information from the database to the webpage. So far i have only managed to get one to work which is:

Code: Select all

<? echo $_SESSION['user_name'];?>


Obviously that displayed the current username.

Now i want to do the same method but with the ID of the user. In the MySQL table its under "id" so i tried:

Code: Select all

<? echo $_SESSION['id'];?>
but no luck, i have a feeling it has something to do with this piece of code in the script

Code: Select all

 
<? if (isset($_SESSION['user_id'])) {?>
Im still learning stuff like this from tutorials which is why im really sketchy on the code. Any assistance not only helps me but also allows me to understand php more.
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: Getting user information, need some assistance.

Post by califdon »

Just to make sure you interpret McInfo's explanation correctly, let me rephrase it: $_SESSION variables only contain what YOUR SCRIPT stores there. Until and unless you assign a $_SESSION variable, it doesn't exist. Other global variables, such as $_SERVER, for instance, have values set by the server or other sources, but $_SESSION is your baby. It will only contain what YOU put there.
reddurango
Forum Newbie
Posts: 13
Joined: Sun Aug 16, 2009 9:17 am

Re: Getting user information, need some assistance.

Post by reddurango »

I think you can do a query in the database with the user name, and then with a while loop assign the value of the $_SESSION variable. And i got a question for you, i tried to use this method for my user management system but i can get the SESSION variable surf trough my pages when i try to use it agan it is on 0.
synical21
Forum Contributor
Posts: 150
Joined: Tue Jul 28, 2009 8:44 am
Location: London UK

Re: Getting user information, need some assistance.

Post by synical21 »

This is what ive done, this is the query(in login.php):

Code: Select all

$sql = "SELECT `id`,`full_name`,`approved` FROM users WHERE 
           $user_cond
            AND `pwd` = '$md5pass' AND `banned` = '0'
            "; 
 
and here is the variables:

Code: Select all

session_start(); 
       // this sets variables in the session 
        $_SESSION['user_id']= $id;  
        $_SESSION['user_name'] = $full_name;
then on another page (account.php) i enter

Code: Select all

<? echo $_SESSION['user_id'] ;?>
to hopefully show the current user ID, but for somereason it shows the username :S


... 2min later as i recheck my scripts inbetween writing this post it works. Weird lol ill post it anyways.

Thanks for the help.
Post Reply