1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

PHP: Display data of logged on person

Discussion in 'PHP' started by SpyJoe, Jan 19, 2008.

  1. #1
    Hi everyone, I'm creating a social network website, like MySpace or Facebook, but a simple one. I used a code I found on a website for login, register and logout. What I'm trying to do now is display member's data (like name, gender, etc) that member has registered with. So just like a profile of someone who is logged in.
    SEMrush
    This is part of the code that I have already:

    
    <?
    $conn = mysql_connect("###", "###", "###") or die(mysql_error());
    mysql_select_db('###', $conn) or die(mysql_error());
    
    $result = mysql_query("SELECT * FROM users",$conn);
    printf("First Name: %s<br>\n", mysql_result($result,0,"firstname"));
    printf("Last Name: %s<br>\n", mysql_result($result,0,"lastname"));
    printf("Gender: %s<br>\n", mysql_result($result,0,"gender"));
    mysql_close($conn);
    ?>
    
    PHP:
    But this only displays the first row from my database. I need to do something like this:

    
    $result = mysql_query("SELECT * FROM users WHERE username=$username",$conn);
    
    PHP:
    I'm not sure if the username of person who is logged in is stored in a cookie or somewhere else, because code is quite complicated.

    What do I need to put, so that it displays information of a person who is logged in? Can someone reply asap plz. Thank you!

    P.S. Sorry forgot the most important thing. This is where I got the code from:

    
    http://www.evolt.org/article/comment/17/60265/index.html
    
    Code (markup):
     
    SpyJoe, Jan 19, 2008 IP
    SEMrush
  2. barts2108

    barts2108 Guest

    Messages:
    18
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    If you only want to show the username to the logged in user
    you can add a session variable to his/her current session

    if (login($_POST['username'],$_POST['password']) == TRUE)
    {
    	// User logged in. store username in session variables
    	session_register("username");
    	$_SESSION['username'] = $_POST['username'];
    }
    Code (markup):
    Anywhere you want to show the username you can echo it assuming
    all your pages have .php extension

    example:

    <p><?php echo($_SESSION['username']); ?></p>

    If you want to show to all users who's online, you must make sure that
    active sessions will expire automatically within reasonable time.

    Upon login of a user, in the login table you can have a column "loggedin"
    and keep track of users who logged in or logged off. If a user not logs
    off but the session expires, I am not sure how to handle the loggedin column,
    but I hope it gives you an idea
     
    barts2108, Feb 9, 2008 IP
  3. Alley Cat

    Alley Cat Peon

    Messages:
    41
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #3
    This is the code that I use to welcome a member to each page that they view.
    
    
    if (!isset($_SESSION['first_name'])) {
    
    	$url = 'http://' . $_SERVER['HTTP_HOST'] . dirname($_SERVER['PHP_SELF']);
    	if ((substr($url, -1) == '/') OR (substr($url, -1) == '\\') ) {
    		$url = substr ($url, 0, -1);
    	}
    	$url .= '/login.php';
    	
    	ob_end_clean();
    	header("Location: $url");
    	exit();
    	
    } else {
    	};
    
    echo '<h1>Welcome';
    if (isset($_SESSION['first_name'])) {
    	echo ", {$_SESSION['first_name']}!";
    };
    echo '</h1>';
    
    Code (markup):
     
    Alley Cat, Feb 10, 2008 IP
  4. Toya-Chan

    Toya-Chan Peon

    Messages:
    9
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Hi. I got my php log in script from Xentrick.net a while back and I was trying for a long time to make it say, "Welcome, YOUR_NAME_HERE" when you get to the members page.

    It's completely made me batty.

    here are the log in and member's page, if you guys can help me, it'd be great because while I have edited php before, I have not scripted anything from scratch and have tried for the last three days to add some sort of session system to no avail.


    Login.php

    Members.php

     
    Toya-Chan, Mar 6, 2011 IP
  5. property

    property Peon

    Messages:
    134
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    echo $SESSION['user_name']
     
    property, Mar 6, 2011 IP
  6. awood969

    awood969 Member

    Messages:
    186
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    40
    #6
    I tend to get multiple data rows like this

    $query = "THIS IS MY QUERY";
    $result = mysql_query($query, $conn);
    $num = mysql_num_rows($result)
    if($num<1) echo("Sorry, nobody loves you");
    else
    {
    while($details=mysql_fetch_array($result))
    {
    //Stuff goes in here
    echo("My username is: $details[username] <br />");
    }
     
    awood969, Mar 7, 2011 IP
  7. Toya-Chan

    Toya-Chan Peon

    Messages:
    9
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    okay, "echo $SESSION['user_name']" didn't work.

    Maybe I wasn't very clear on what help I needed and what for so let me try again. I need more details because I am unsure how to make it work with the script I listed above.

    My table is "users" and in that table is "id", "username", and "password" only, with there being 3 users.

    I want the script that I previously posted (the one I got from Xentrick.net) to be able to tell me the name of the user who's session is currently in use once logged into the member.php page.

    How do I get the quote below to work with that information?

     
    Toya-Chan, Mar 7, 2011 IP
  8. awood969

    awood969 Member

    Messages:
    186
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    40
    #8
    You can't with that information. PHP Session data has a low linux permission level so nobody can access it apart from "self". The best way to do this would be;

    - Add a "timestamp" field to your database with a varchar type length 30....ish
    - Create a new MySQL query in the header of every page to update that field for that user using PHP's time() function.

    Then where ever you want to find out who is online do a query similar to this;

    The 300 being in seconds.

    This will get the data of any users who have clicked somewhere on your site in the past 300 seconds, thus is safe to assume that these people are "online" however you can change that value as you wish.
     
    awood969, Mar 7, 2011 IP
  9. Toya-Chan

    Toya-Chan Peon

    Messages:
    9
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #9
    It's not working.

    ok so this is how you make a time stamp, correct?

    So this is what I did to my own member page.

    where am I going wrong, or am I supposed to write the query because I have no idea how. **points at self and says "noob"**
     
    Last edited: Mar 7, 2011
    Toya-Chan, Mar 7, 2011 IP
  10. awood969

    awood969 Member

    Messages:
    186
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    40
    #10
    no literally $timestamp=time();

    This will give a UNIX clock time, the strtotime function will make that time look pretty, but rather unusable.

    But once you have done this then yet just do;

    And then the code I showed you earlier to display a list of "online" users.
     
    awood969, Mar 7, 2011 IP
  11. Toya-Chan

    Toya-Chan Peon

    Messages:
    9
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #11
    says there is a syntax error on line 32:

     
    Toya-Chan, Mar 7, 2011 IP
  12. awood969

    awood969 Member

    Messages:
    186
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    40
    #12
    I was only giving an example you will need to modify this code so that it fits in with your database and variable names.
     
    awood969, Mar 7, 2011 IP
  13. Toya-Chan

    Toya-Chan Peon

    Messages:
    9
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #13
    One question, what does $conn represent or do?
     
    Toya-Chan, Mar 7, 2011 IP
  14. awood969

    awood969 Member

    Messages:
    186
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    40
    #14
    $conn is the connection variable attatched to the mysql_connect() function.
     
    awood969, Mar 7, 2011 IP
  15. Toya-Chan

    Toya-Chan Peon

    Messages:
    9
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #15
    Weird, still not working.

    Something with the result and while statements.
     
    Toya-Chan, Mar 7, 2011 IP
  16. gilanib

    gilanib Peon

    Messages:
    1
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #16
    Dear you didn't show us that how to display logged in user details..???
     
    gilanib, Mar 20, 2011 IP
  17. Toya-Chan

    Toya-Chan Peon

    Messages:
    9
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #17
    Got fixed already, sorry forgot to come back.
     
    Toya-Chan, Mar 25, 2011 IP
  18. fighapilotdean

    fighapilotdean Peon

    Messages:
    1
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #18
    i have had a read of this piece of code and i am wondering, will this definitly display the logged in user and how if it does can i get my pages to display 'logged in as guest' when there is nobody logged in?
     
    fighapilotdean, Mar 29, 2012 IP