dachshund Posted August 25, 2009 Share Posted August 25, 2009 Hi, In the header of my page I have a section that says "Log In | Register". But when someone tries to log in, all the if statements for 'Register' are run as well, for example, it checks if all data to register has been submitted even if someone is just logging in. So when you log in it returns: ACCOUNT | LOGOUT PLEASE SUPPLY ALL VALID FIELDS. (The Please supply all valid fields part is returned from the register part). I hope i've explained myself well enough that you will be able to help. Here's my code: <li> <?php if($session_name){ echo "<span class=\"login_text\"><a href=\"/phptesting/account/\">ACCOUNT</a> | <a href=\"/phptesting/logout.php\">LOGOUT</a></span>\n"; }else { if(!$_POST['submit'] == 'login'){ echo "<div id=\"blanket\" style=\"display:none;\"></div>\n"; echo "<div id=\"popUpDiv\" style=\"display:none;\">\n"; echo "<div id=\"close\">\n"; echo "<a href=\"#\" onclick=\"popup('popUpDiv')\">\n"; echo "<img src=\"/images/login/close.png\">\n"; echo "</a>\n"; echo "</div>\n"; echo "<div id=\"loginform\">\n"; echo "<form name=\"login\" method=\"post\" action=\"/phptesting/account/index.php?\n"; echo $rows['id']; echo "\">\n"; echo "<span class=\"loginformtext\">Username</span>\n"; echo "<input name=\"username\" type=\"text\" class=\"formtext\" id=\"username\" size=\"28\">\n"; echo "<br>\n"; echo "<span class=\"passwordformtext\">Password</span>\n"; echo "<input name=\"password\" type=\"password\" class=\"formtext\" id=\"password\" size=\"28\">\n"; echo "<br><br>\n"; echo "<div align=\"left\"\n"; echo "<input type=\"submit\" name=\"submit\" class=\"loginformtext\" value=\"Submit\">\n"; echo "</div>\n"; echo "</div>\n"; echo "</form>\n"; echo "</div>\n"; echo "<span class=\"login_text\"><a href=\"#\" onclick=\"popup('popUpDiv')\">LOG IN</a> |\n"; }else { $user= $_POST['username']; $pass= $_POST['password']; if($user && $pass){ $sql = "SELECT * FROM `users` WHERE `username`='$user'"; $res = mysql_query($sql) or die(mysql_error()); if(mysql_num_rows($res) == 1){ $epass = md5($password); $sql2 = "SELECT * FROM `users` WHERE `username` ='$user' AND `password`='$epass'"; $res2 = mysql_query($sql2) or die(mysql_error()); if(mysql_num_rows($res2) ==1){ //success $row = mysql_fetch_assoc($res2); $_SESSION['uid'] = $row['id']; echo "<span class=\"login_text\"><a href=\"/phptesting/account/\">ACCOUNT</a> | <a href=\"logout.php\">LOGOUT</a></span>\n"; }else { echo "<span class=\"login_text\">USERNAME OR PASSWORD INCORRECT | <a href=\"/phptesting\">HOME</a></span>\n"; } }else { echo "<span class=\"login_text\">USERNAME OR PASSWORD INCORRECT | <a href=\"/phptesting\">HOME</a></span>\n"; } }else { echo "<span class=\"login_text\">PLEASE SUPPLY ALL VALID FIELDS | <a href=\"/phptesting\">HOME</a></span>\n"; } } ?> </li> <li> <?php if(!$_POST['submit'] == 'register'){ echo "<div id=\"blanket\" style=\"display:none;\"></div>\n"; echo "<div id=\"popUpDiv2\" style=\"display:none;\">\n"; echo "<div id=\"close\">\n"; echo "<a href=\"#\" onclick=\"popup('popUpDiv2')\">\n"; echo "<img src=\"/phptesting/images/login/close.png\">\n"; echo "</a>\n"; echo "</div>\n"; echo "<div id=\"loginform\">\n"; echo "<form name=\"register\" method=\"post\" action=\"/phptesting/registered.php\">\n"; echo "<span class=\"nameformtext\">Firt Name</span>\n"; echo "<input name=\"firstname\" type=\"text\" class=\"formtext\" id=\"firstname\" size=\"25\">\n"; echo "<br>\n"; echo "<span class=\"lastnameformtext\">Last Name</span>\n"; echo "<input name=\"lastname\" type=\"text\" class=\"formtext\" id=\"lastname\" size=\"25\">\n"; echo "<br>\n"; echo "<span class=\"emailformtext\">Email</span>\n"; echo "<input name=\"email\" type=\"text\" class=\"formtext\" id=\"email\" size=\"25\">\n"; echo "<br>\n"; echo "<span class=\"usernameformtext\">Username</span>\n"; echo "<input name=\"username\" type=\"text\" class=\"formtext\" id=\"username\" size=\"25\">\n"; echo "<span class=\"password2formtext\">Password</span>\n"; echo "<input name=\"password\" type=\"password\" class=\"formtext\" id=\"password\" size=\"19\">\n"; echo "<br>\n"; echo "<span class=\"confirmpasswordformtext\">Confirm Password</span>\n"; echo "<input name=\"passconf\" type=\"password\" class=\"formtext\" id=\"passconf\" size=\"19\">\n"; echo "<br><br>\n"; echo "<div align=\"left\"\n"; echo "<input type=\"submit\" name=\"submit\" class=\"loginformtext\" value=\"Submit\">\n"; echo "</div>\n"; echo "</div>\n"; echo "</form>\n"; echo "</div>\n"; echo "<span class=\"login_text\"><a href=\"#\" onclick=\"popup('popUpDiv2')\">REGISTER</a></span>\n"; }else { $firstname = $_POST['firstname']; $lastname = $_POST['lastname']; $email = $_POST['email']; $username = $_POST['username']; $password = $_POST['password']; $passconf = $_POST['passconf']; if($firstname && $lastname && $email && $username && $password && $passconf){ if(strlen($username) <= 32){ if(strlen($username) >= 3){ if(strlen($password) >=3){ if($password == $passconf){ $sql = "SELECT * FROM `users` WHERE `username`='$username'"; $res = mysql_query($sql) or die(mysql_error()); if (mysql_num_rows($res) ==0){ $pass = md5($password); $sql2 = "INSERT INTO `users` (`firstname`, `lastname`, `email`, `username`,`password`) VALUES('$firstname', '$lastname', '$email','$username','$pass');"; $res2 = mysql_query($sql2) or die(mysql_error()); //IF one row was inserted if($res2 == 1){ //get last inserted id $user_id = mysql_insert_id(); $make_directory = mkdir("images/upload/profiles/".$user_id , 0755); echo "<span class=\"login_text\">HELLO <b>$username</b>. YOU CAN NOW LOG IN.</span>\n"; } } else { echo "<span class=\"login_text\">SORRY. THAT USERNAME ALREADY EXISTS.</span>\n"; } }else { echo "<span class=\"login_text\">YOUR TWO PASSWORDS DID NOT MATCH.</span>\n"; } }else { echo "<span class=\"login_text\">YOUR PASSWORD IS TOO SHORT.</span>\n"; } }else { echo "<span class=\"login_text\">YOUR USERNAME IS TOO SHORT.</span>\n"; } }else { echo "<span class=\"login_text\">YOUR USERNAME IS TOO LONG.</span>\n"; } }else { echo "<span class=\"login_text\">PLEASE SUPPLY ALL VALID FIELDS.</span>\n"; } } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/171804-solved-help-with-log-in-form/ Share on other sites More sharing options...
mikesta707 Posted August 25, 2009 Share Posted August 25, 2009 The problem is, since the if statement for the login and for the register are both on the same page, and are not linked in any way, they will always both run. Your best bet would probably be to change that registers if statement to an else if, and make it part of the login if block. Quote Link to comment https://forums.phpfreaks.com/topic/171804-solved-help-with-log-in-form/#findComment-905915 Share on other sites More sharing options...
dachshund Posted August 25, 2009 Author Share Posted August 25, 2009 one way i did manage to get round it was by putting <?php include "loginform.php"; include "registerform.php"; ?> and having them in two seperate php files. the problem with this is that when i went to another page in a different folder, it needed to be "../loginform.php" so the session closed if they were logged in. is there anyway to set the link in relation to the root folder? "/loginform.php" doesn't seem to work. Quote Link to comment https://forums.phpfreaks.com/topic/171804-solved-help-with-log-in-form/#findComment-905917 Share on other sites More sharing options...
mikesta707 Posted August 25, 2009 Share Posted August 25, 2009 yep! <?php include $_SERVER['DOCUMENT_ROOT'] . "loginform.php"; include $_SERVER['DOCUMENT_ROOT'] . "registerform.php"; ?> EDIT: Hold on thats wrong I think, lemme check EDIT AGAIN: Wait yeah I think thats right, nevermind Quote Link to comment https://forums.phpfreaks.com/topic/171804-solved-help-with-log-in-form/#findComment-905932 Share on other sites More sharing options...
dachshund Posted August 25, 2009 Author Share Posted August 25, 2009 ah many thanks my friend. think thats solved it. Quote Link to comment https://forums.phpfreaks.com/topic/171804-solved-help-with-log-in-form/#findComment-905974 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.