olishoey Posted October 13, 2011 Share Posted October 13, 2011 Hi Guys I am having problems with my login scripts. I have adapted them slightly towards my own site so I'll post the bits that are relevant. Basically when I submit the login form it keeps coming back and telling me that the username has not been entered. Using an echo i can see that the username field is indeed blank, what I cant understand is why this is happening. the password field is fine. I'll post the relevent parts of the code, hopefully someone can help cus I'm a bit stuck here. This is the relevent part of the login_scripts file -> login_functions.inc.php from the book. function check_login($dbc, $username='', $pass='') { // setup the array that will handle the errors, errors will be passed to this array and then printed at the login screen $errors = array(); if (empty($username)) { // make sure that the username field has been filled in $errors[] = 'You forgot to enter your user name.' . "$username" . "$pass"; // if it hasnt then place this string into the errors array at the next location } else { $u = mysqli_real_escape_string($dbc, trim($username)); // make sure that characters that could disrupt the SQL statement are escaped and then whitespace is trimmed } i know that by the time it gets to the $errors[] bit that the username field is already empty this is the login form <div id='loginForm'> <form action="login.php" method="post"> <p>Username : <input type="text" name="username" size="20" maxlength="40" /></p> <p>Password : <input style="margin-left:7px;" type="password" name="pass" size="20" maxlength="20" /></p> <p><input type="submit" name="submit" value="Login" /></p> <p><input type="hidden" value="TRUE" name="submitted" /></p> </form> </div> and this is the login.php file if (isset($_POST['submitted'])) { require_once('login_scripts.php'); // the file containing the scripts that control the login process require_once('../lowisoconnect.php'); // the database connection file // check the login list ($check, $data) = check_login($dbc, $_POST['username'], $_POST['pass']); can anyone see any reason why my username field would be coming back as blank? thanks in advance for any feedback. its probably something incredibly simple. Link to comment Share on other sites More sharing options...
Josee Posted October 13, 2011 Share Posted October 13, 2011 function check_login($dbc, $username='', $pass='') { else { $u = mysqli_real_escape_string($dbc, trim($username)); // make sure that characters that could disrupt the SQL statement are escaped and then whitespace is trimmed } Shouldn't it be trim($_POST['username'])? As it is, I think you are applying trim() and mysqli_real_escape_string() to the default value of $username, i.e. an empty string. Link to comment Share on other sites More sharing options...
olishoey Posted October 13, 2011 Author Share Posted October 13, 2011 Hi Josee Thanks for the reply, this is correct. the IF statement has actually already returned $username as being empty in the first part of the statement, my script doesn't get to the piece of code you highlighted because it has already failed the first condition Link to comment Share on other sites More sharing options...
olishoey Posted October 13, 2011 Author Share Posted October 13, 2011 I seem to have solved this issue. Now. I found a stray 'email' on one of the pages that needed to be a username. im getting something different now but hopefully can solve it myself Link to comment Share on other sites More sharing options...
Recommended Posts