Jump to content

Trying to get Welcome Message when logged in


Yugiohwiz

Recommended Posts

<?php$query = "SELECT * FROM members WHERE status = '$status'";$result = mysql_query($query)     or die(mysql_error());while($row = mysql_fetch_array($result)){extract($row);if($status == "0") { $oo = "<form action='console.php' method='post'><td class='main'><b>Username:</b> <input type='text' name='user' value='' class='form'> <b>Password:</b> <input type='password' name='pword' class='form'> <input type='submit' value='Login' class='lp'></td></form> "; }if($status == "1") { $oo = "Weclome!"; }echo "$oo";?>

Hi I am looking for to change the user login area when a user is logged in but it won't change please help.

Link to comment
Share on other sites

You are using mysql_fetch_array. That produces an array like $row[0], $row[1], $row[2] for all of your fields. They are indexed by number, not by name. You need to use mysql_fetch_assoc to get an array like $row['status']. You also don't need the while loop, and I think extract is generally a bad idea unless you can be sure all your field names are globally unique. Try this:

<?php$query = "SELECT * FROM members WHERE status = '$status'";$result = mysql_query($query)    or die(mysql_error());$row = mysql_fetch_assoc($result);if($row['status'] == "0") {   echo "<form action='console.php' method='post'><td class='main'><b>Username:</b> <input type='text' name='user' value='' class='form'> <b>Password:</b> <input type='password' name='pword' class='form'> <input type='submit' value='Login' class='lp'></td></form> "; }if($row['status'] == "1") {   echo "Weclome!"; }?>

Link to comment
Share on other sites

Well, the code does work. The question is does it do what you were expecting. If you are checking for a logged in user, I don't understand why you are querying the database at all, you should be checking in the session. If you have some field in the database (status) that keeps track of people who are logged in, you will always get the welcome page whenever anyone at all is logged in, even if you aren't. You are checking for any member who has status = 1, I assume, so it will show welcome if anyone at all has status set to 1. If you want to check if someone is logged in, look in the session where you store their user ID. Then you can check the database and see if they are allowed to log in.

Link to comment
Share on other sites

Right. In order for anything to change when someone is logged in, first you have to know if they are logged in.When someone logs in, you need to store their user ID (and maybe an encrypted password) in a cookie. Then later you can check the cookie to see if they are logged in (if there is a user ID in there), and then you can look them up in the database to see if the password is correct or if they are otherwise not allowed to log in for whatever reason.

Link to comment
Share on other sites

Well, that is several pages of code, and also some database design. If someone else wants to do that then go ahead, but I can't do your work for you.I suggest reading up and taking a look at the examples on the php.net session reference and cookie reference.Programming anything isn't trivial work, you really need to understand the what and why if you want to be a web programmer, not just be able to copy and paste code.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...