starlight22 Posted June 29, 2010 Share Posted June 29, 2010 My membership script works fine, except I can't login, because this check.php script doesn't work. Login form action is check.php. Once I login with username and password, check.php give me an error message "Please enter ALL of the information". The only information on login form is username and password. The password is random md5. I did a copy and paste of this script from a php tutortial, because I don't know php. Everything works except this file. How do I direct it to my member page once its working? Please help. <? /* Check Username Script */ session_start(); // Start Session include 'db.php'; // Conver to simple variables $username = $_POST['username']; $password = $_POST['password']; if((!$username) || (!$password)){ echo "Please enter ALL of the information! <br />"; include 'login_form.html'; exit(); } // Convert password to md5 hash $password = md5($password); // check if the user info validates the db $sql = mysql_query("SELECT * FROM users WHERE username='$username' AND password='$password' AND activated='1'"); $login_check = mysql_num_rows($sql); if($login_check > 0){ while($row = mysql_fetch_array($sql)){ foreach( $row AS $key => $val ){ $$key = stripslashes( $val ); } // Register some session variables! session_register('first_name'); $_SESSION['first_name'] = $first_name; session_register('last_name'); $_SESSION['last_name'] = $last_name; session_register('email_address'); $_SESSION['email_address'] = $email_address; session_register('special_user'); $_SESSION['user_level'] = $user_level; mysql_query("UPDATE users SET last_login=now() WHERE userid='$userid'"); header("Location: members.php"); } } else { echo "You could not be logged in! Either the username and password do not match or you have not validated your membership!<br /> Please try again!<br />"; include 'login_form.html'; } ?> +++++++++++++++++++++++++ Here is the login script <html> <head> <title>Login</title> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> </head> <body> <form action="check.php" method="post" name="" id=""> <table width="50%" border="0" align="center" cellpadding="4" cellspacing="0"> <tr> <td width="22%">Username</td> <td width="78%"><input name="username" type="text" id="username"></td> </tr> <tr> <td>Password</td> <td><input name="password" type="password" id="password"></td> </tr> <tr> <td> </td> <td><input type="submit" name="Submit" value="Submit"></td> </tr> </table> </form> </body> </html> Quote Link to comment Share on other sites More sharing options...
murdocsvan Posted June 29, 2010 Share Posted June 29, 2010 if((!$username) || (!$password)){ echo "Please enter ALL of the information! <br />"; include 'login_form.html'; exit(); } Try changing the first line in this 'if then else' statement on line 10 to: if(!isset($username) || !isset($password)) Quote Link to comment Share on other sites More sharing options...
Psycho Posted June 29, 2010 Share Posted June 29, 2010 if((!$username) || (!$password)){ echo "Please enter ALL of the information! <br />"; include 'login_form.html'; exit(); } Try changing the first line in this 'if then else' statement on line 10 to: if(!isset($username) || !isset($password)) That would do absolutely nothing since those two values are being set right before that IF condition. Even using isset() on the POST values would be pointless because even if the user left them blank, they will still be set. Here is a better approach: $username = trim($_POST['username']); $password = trim($_POST['password']); if(empty($username) || empty($password)){ However, because of the current logic, the user would always see that error message on the first loading of the page. You should wrap all the logic for processing the POST data in an if statement such as: if(isset($_POST) { //PHP logic for processing form data } That way, the first time the user accesses the page the PHP logic won't run and only the form will be displayed. Quote Link to comment Share on other sites More sharing options...
murdocsvan Posted June 29, 2010 Share Posted June 29, 2010 Thanks for clearing that up, I keep getting confused between the functionality of empty() and isset(). So empty() is for checking if a variable != NULL and isset() checks that a $_POST or $_GET or similar has been set in the previous form? Quote Link to comment Share on other sites More sharing options...
Psycho Posted June 29, 2010 Share Posted June 29, 2010 Thanks for clearing that up, I keep getting confused between the functionality of empty() and isset(). So empty() is for checking if a variable != NULL and isset() checks that a $_POST or $_GET or similar has been set in the previous form? Just read the manual. I'm sure it will do a better job of explaining the functions than I can. 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.