dark_mirage Posted March 2, 2008 Share Posted March 2, 2008 I have created a relatively simple login system linked to a mysql database. The login part is working fine, but what i would like to know is how to hide the login box (and replace it with a welcome message for example) when the user is logged in. My system does use cookies if that helps at all, //Checks if there is a login cookie if(isset($_COOKIE['ID_my_site'])) //if there is, it logs you in and directes you to the members page { $username = $_COOKIE['ID_my_site']; $pass = $_COOKIE['Key_my_site']; $check = mysql_query("SELECT * FROM users WHERE username = '$username'")or die(mysql_error()); while($info = mysql_fetch_array( $check )) { if ($pass != $info['password']) { ?> <form action="login.php" method="post" STYLE="margin: 0px; padding: 0px;"> <input name="username" type="text" id="username" value="Username" onFocus="if (this.value==this.defaultValue) this.value='';" /> <input name="password" type="password" id="password" value="Password" onFocus="if (this.value==this.defaultValue) this.value='';"/><br> <a href="members.php" title="Members Area" target="content">Members Area</a> | <a href="registration.php" title="Register" target="content">Register</a> <input type="submit" id="login" name="login" value="Login" /> </form> <? } else { echo "Welcome"; } } } ?> //html for rest of site down here However, this does not seem to work, the login box is never there, even after clearing all cookies :/ Thanks in advance. Quote Link to comment Share on other sites More sharing options...
pocobueno1388 Posted March 2, 2008 Share Posted March 2, 2008 I'm a little confused by the logic of your script. Your script is saying IF the cookie "ID_my_site" exists check if their login/password are correct. But if that cookie exists, doesn't that mean they are already logged in? So doesn't that mean the IF statement should be: if(!isset($_COOKIE['ID_my_site'])) I may just have it wrong, if I do can you explain where you register those cookies? I promise I have a point to all this =] Quote Link to comment Share on other sites More sharing options...
dark_mirage Posted March 2, 2008 Author Share Posted March 2, 2008 Thanks for the reply, such a simple fix and its been driving me mad, i dont even think u gave me the answer, but reading ur post made me realised i was just over complicating it, which was stopping it from working thanks for your help, you'll probably be hearing from me sometime soon Quote Link to comment Share on other sites More sharing options...
pocobueno1388 Posted March 2, 2008 Share Posted March 2, 2008 I see other problems with your script as well. You don't need a while loop when your only expecting one row, so that needs to be taken out. Also, there is an easier way of checking whether the username/password combination was found in the database by modifying the query a little and just checking how many rows it returned. Here is some modified code, see if it works for you. <?php //Checks if there is a login cookie if (!isset($_COOKIE['ID_my_site'])){ $username = $_COOKIE['ID_my_site']; $pass = $_COOKIE['Key_my_site']; $check = mysql_query("SELECT * FROM users WHERE username = '$username' AND password='$pass'")or die(mysql_error()); if (mysql_num_rows($check) < 1) { ?> <form action="login.php" method="post" STYLE="margin: 0px; padding: 0px;"> <input name="username" type="text" id="username" value="Username" onFocus="if (this.value==this.defaultValue) this.value='';" /> <input name="password" type="password" id="password" value="Password" onFocus="if (this.value==this.defaultValue) this.value='';"/><br> <a href="members.php" title="Members Area" target="content">Members Area</a> | <a href="registration.php" title="Register" target="content">Register</a> <input type="submit" id="login" name="login" value="Login" /> </form> <?php } } else { echo "Welcome"; } ?> You may have to redefine your $username and $pass variable, because if they aren't logged in then those cookies probably aren't set. I would assume you would get those values from the form they use to log in. Quote Link to comment Share on other sites More sharing options...
dark_mirage Posted March 2, 2008 Author Share Posted March 2, 2008 That works perfectly, thanks a lot for your help. I got the majority of the code from some tutorials, i tried not to just copy it straight off, becuse wheres the fun in that and i didnt really understand the mysql_num_rows($check) section, if it isnt too much trouble, would it be possible for you (or anyone) to give me a quick, simple explanation of mysql_num_rows() . Thanks Quote Link to comment Share on other sites More sharing options...
pocobueno1388 Posted March 2, 2008 Share Posted March 2, 2008 mysql_num_rows() returns the number of rows your query found in the database. So look at this line: if (mysql_num_rows($check) < 1) { Thats saying that if the query $check returns less than one row, then they got the wrong login information, because it obviously doesn't exist in the database. And of course, if it returns "1" that means they got the correct login information. Hopefully I explained that clear enough. Remember, if you ever want to know what a function does, just look in the manual. In this case, you would just type in the URL: www.php.net/mysql_num_rows - that will give you all the information you need to know about it. Quote Link to comment Share on other sites More sharing options...
dark_mirage Posted March 2, 2008 Author Share Posted March 2, 2008 That was a great explanation, thanks a lot Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.