Ratee Posted August 9, 2011 Share Posted August 9, 2011 i have a login script but i would like to make it in to 2 files like... login.html and do-login.php i want the login form on 1 page then when you try and login it goes to.. do-login.php tp check if the information is correct below is the code.. could someone please help me.. thanks. <?php session_start(); include "./global.php"; echo "<title>Login</title>\n"; if ($_SESSION['uid']) { echo "You are already logged in, if you wish to log out, please <a href=\"./logout.php\">click here</a>!\n"; } else { if (!$_POST['submit']) { echo "<table border=\"0\" cellspacing=\"3\" cellpadding=\"3\">\n"; echo "<form id=\"login-form\" method=\"post\" action=\"./login.php\">\n"; echo "<tr><td>Username</td><td><input type=\"text\" name=\"username\"></td></tr>\n"; echo "<tr><td>Password</td><td><input type=\"password\" name=\"password\"></td></tr>\n"; echo "<tr><td colspan=\"2\" align=\"right\"><input type=\"submit\" name=\"submit\" value=\"Login\"></td></tr>\n"; echo "</form></table>\n"; }else { $user = mss($_POST['username']); $pass = $_POST['password']; if($user && $pass){ $sql = "SELECT id FROM `users` WHERE `username`='".$user."'"; $res = mysql_query($sql) or die(mysql_error()); if(mysql_num_rows($res) > 0){ $sql2 = "SELECT id FROM `users` WHERE `username`='".$user."' AND `password`='".md5($pass)."'"; $res2 = mysql_query($sql2) or die(mysql_error()); if(mysql_num_rows($res2) > 0){ $row = mysql_fetch_assoc($res2); $_SESSION['uid'] = $row['id']; echo "You have successfully logged in as " . $user . "<br><br><a href=\"./index.php\">Proceed to the Forum Index</a>\n"; }else { echo "Username and password combination are incorrect!\n"; } }else { echo "The username you supplied does not exist!\n"; } }else { echo "You must supply both the username and password field!\n"; } } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/244363-need-help-with-login/ Share on other sites More sharing options...
AyKay47 Posted August 9, 2011 Share Posted August 9, 2011 whatever you set the form action to be is where you want the script that handles your form to be.. Quote Link to comment https://forums.phpfreaks.com/topic/244363-need-help-with-login/#findComment-1255099 Share on other sites More sharing options...
Ratee Posted August 9, 2011 Author Share Posted August 9, 2011 but how do i break up the code to 2 files? Quote Link to comment https://forums.phpfreaks.com/topic/244363-need-help-with-login/#findComment-1255100 Share on other sites More sharing options...
AyKay47 Posted August 10, 2011 Share Posted August 10, 2011 one file will be html.. <html> <head> <body> <table border="0" cellspacing="3" cellpadding="3"> <form id="login-form" method="post" action="login.php"> <tr><td>Username</td><td><input type="text" name="username"></td></tr> <tr><td>Password</td><td><input type="password" name="password"></td></tr> <tr><td colspan="2" align="right"><input type="submit" name="submit" value="Login"></td></tr> </form></table> </body> </head> </html> then in this case, login.php will contain the necessary php to handle the form.. Quote Link to comment https://forums.phpfreaks.com/topic/244363-need-help-with-login/#findComment-1255101 Share on other sites More sharing options...
Ratee Posted August 10, 2011 Author Share Posted August 10, 2011 okaay and what would i need to put in the do-login.php file? with the code i have above? Quote Link to comment https://forums.phpfreaks.com/topic/244363-need-help-with-login/#findComment-1255211 Share on other sites More sharing options...
Ratee Posted August 10, 2011 Author Share Posted August 10, 2011 anyone know? Quote Link to comment https://forums.phpfreaks.com/topic/244363-need-help-with-login/#findComment-1255218 Share on other sites More sharing options...
AyKay47 Posted August 10, 2011 Share Posted August 10, 2011 you will include any PHP that you will use to validate and process the form.. Quote Link to comment https://forums.phpfreaks.com/topic/244363-need-help-with-login/#findComment-1255261 Share on other sites More sharing options...
Ratee Posted August 10, 2011 Author Share Posted August 10, 2011 i really don't get it could you just copy the code and make it into 2 files please? Quote Link to comment https://forums.phpfreaks.com/topic/244363-need-help-with-login/#findComment-1255262 Share on other sites More sharing options...
AyKay47 Posted August 10, 2011 Share Posted August 10, 2011 php will be something like if ($_SESSION['uid']) { echo "You are already logged in, if you wish to log out, please <a href=\"./logout.php\">click here</a>!\n"; } else { if (!$_POST['submit']) { $user = mss($_POST['username']); $pass = $_POST['password']; if($user && $pass){ $sql = "SELECT id FROM `users` WHERE `username`='".$user."'"; $res = mysql_query($sql) or die(mysql_error()); if(mysql_num_rows($res) > 0){ $sql2 = "SELECT id FROM `users` WHERE `username`='".$user."' AND `password`='".md5($pass)."'"; $res2 = mysql_query($sql2) or die(mysql_error()); if(mysql_num_rows($res2) > 0){ $row = mysql_fetch_assoc($res2); $_SESSION['uid'] = $row['id']; echo "You have successfully logged in as " . $user . "<br><br><a href=\"./index.php\">Proceed to the Forum Index</a>\n"; }else { echo "Username and password combination are incorrect!\n"; } }else { echo "The username you supplied does not exist!\n"; } }else { echo "You must supply both the username and password field!\n"; } } } Quote Link to comment https://forums.phpfreaks.com/topic/244363-need-help-with-login/#findComment-1255269 Share on other sites More sharing options...
Ratee Posted August 10, 2011 Author Share Posted August 10, 2011 his does not work:/ Quote Link to comment https://forums.phpfreaks.com/topic/244363-need-help-with-login/#findComment-1255271 Share on other sites More sharing options...
AyKay47 Posted August 10, 2011 Share Posted August 10, 2011 i missed something...change this line if (!$_POST['submit']) { and remove the exclamation.. if (isset($_POST['submit'])) { //exclamation removed Quote Link to comment https://forums.phpfreaks.com/topic/244363-need-help-with-login/#findComment-1255282 Share on other sites More sharing options...
Ratee Posted August 10, 2011 Author Share Posted August 10, 2011 it still aint working when i try login it goes to do-login.php but its just a white screen?:/ Quote Link to comment https://forums.phpfreaks.com/topic/244363-need-help-with-login/#findComment-1255356 Share on other sites More sharing options...
Ratee Posted August 10, 2011 Author Share Posted August 10, 2011 when i use this code it says... "You must supply both the username and password field!" even when the login information is correct? anyone know why? <?php if ($_SESSION['uid']) { echo "You are already logged in, if you wish to log out, please <a href=\"./logout.php\">click here</a>!\n"; } else { if (isset($_POST['submit'])) { //exclamation removed if($user && $pass){ $sql = "SELECT id FROM `users` WHERE `username`='".$user."'"; $res = mysql_query($sql) or die(mysql_error()); if(mysql_num_rows($res) > 0) { $sql2 = "SELECT id FROM `users` WHERE `username`='".$user."' AND `password`='".md5($pass)."'"; $res2 = mysql_query($sql2) or die(mysql_error()); if(mysql_num_rows($res2) > 0){ $row = mysql_fetch_assoc($res2); $_SESSION['uid'] = $row['id']; echo "You have successfully logged in as " . $user . "<br><br><a href=\"./index.php\">Proceed to the Forum Index</a>\n"; }else { echo "Username and password combination are incorrect!\n"; } }else{ echo "The username you supplied does not exist!\n"; } }else { echo "You must supply both the username and password field!\n"; } } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/244363-need-help-with-login/#findComment-1255427 Share on other sites More sharing options...
AyKay47 Posted August 10, 2011 Share Posted August 10, 2011 you havnt set values for $user and $pass , so they will always return false...you will need to set these to your POST values.. e.g $_POST['username'] = $user Quote Link to comment https://forums.phpfreaks.com/topic/244363-need-help-with-login/#findComment-1255458 Share on other sites More sharing options...
Ratee Posted August 10, 2011 Author Share Posted August 10, 2011 i give up now all i wanted to do was modify what the login looked like but i cannot add div id's e.c.t to add style to it:/ because i just got errors. this is the original code.... <?php session_start(); include "./global.php"; echo "<title>Login</title>\n"; if ($_SESSION['uid']) { echo "You are already logged in, if you wish to log out, please <a href=\"./logout.php\">click here</a>!\n"; } else { if (!$_POST['submit']) { echo "<table border=\"0\" cellspacing=\"3\" cellpadding=\"3\">\n"; echo "<form id=\"login-form\" method=\"post\" action=\"./login.php\">\n"; echo "<tr><td>Username</td><td><input type=\"text\" name=\"username\"></td></tr>\n"; echo "<tr><td>Password</td><td><input type=\"password\" name=\"password\"></td></tr>\n"; echo "<tr><td colspan=\"2\" align=\"right\"><input type=\"submit\" name=\"submit\" value=\"Login\"></td></tr>\n"; echo "</form></table>\n"; }else { $user = mss($_POST['username']); $pass = $_POST['password']; if($user && $pass){ $sql = "SELECT id FROM `users` WHERE `username`='".$user."'"; $res = mysql_query($sql) or die(mysql_error()); if(mysql_num_rows($res) > 0){ $sql2 = "SELECT id FROM `users` WHERE `username`='".$user."' AND `password`='".md5($pass)."'"; $res2 = mysql_query($sql2) or die(mysql_error()); if(mysql_num_rows($res2) > 0){ $row = mysql_fetch_assoc($res2); $_SESSION['uid'] = $row['id']; echo "You have successfully logged in as " . $user . "<br><br><a href=\"./index.php\">Proceed to the Forum Index</a>\n"; }else { echo "Username and password combination are incorrect!\n"; } }else { echo "The username you supplied does not exist!\n"; } }else { echo "You must supply both the username and password field!\n"; } } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/244363-need-help-with-login/#findComment-1255464 Share on other sites More sharing options...
AyKay47 Posted August 10, 2011 Share Posted August 10, 2011 whatever works for you Quote Link to comment https://forums.phpfreaks.com/topic/244363-need-help-with-login/#findComment-1255496 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.