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. 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):
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
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):
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
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 />"); }
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?
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.
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"**
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.
I was only giving an example you will need to modify this code so that it fits in with your database and variable names.
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?