Whats wrong with this PHP login script? I keep having to login twice and logout twice?!?
-
FORM.PHP http://www.aplusequalsb.com/leesville/form.txt INDEX.PHP http://www.aplusequalsb.com/leesville/index.txt LOGIN.PHP (login SQL Script) http://www.aplusequalsb.com/leesville/login.txt here is the link www.aplusequalsb.com/leesville/index.p… USERNAME:DREW PASSWORD:DREW The fields in the database look like this: http://dc117.2shared.com/download/5496791/69b0f261/problem.bmp?tsid=20090425-210524-ec19eb1 also, it only recognizes Matt, not drew. For some reason it skips over the first username. Any Ideas? Thanks
-
Answer:
<? /*************************** login.class.php ***************************/ class user{ //To track the switch/errors through out var $login_error; //constructor function user($UserInput){ $this->login_form(); }//End function //Just a simple login form function login_form(){ echo "<div align=\"center\"><form name=\"LoginForm\" action=\"$_SERVER[PHP_SELF]\" method=\"POST\"><table><tr><td><tr><td>U… Name:</td><td><input type=\"text\" name=\"FLU\"></td><td>Password:</td><td>… type=\"password\" name=\"FLP\"></td></tr><tr><td colspan=\"2\"><input type=\"submit\" value=\"Submit\"> <input type=\"reset\" value=\"Reset\"></td></tr></table></div>… }//End Function function validate_user($UserInput){ //my chosen user name and password pattern $this->user_name_pattern = ("^[a-zA-Z0-9]{5,15}$"); $this->password_pattern = ("^[a-zA-Z0-9\!\@\#\$\%\^\&\*\`\~\_]… //JavaScript History -1 $this->go_back = <a href=javascript:history.go(-1)><font color=red size=-2>(Go Back)</font></a>; $this->success_login_redirect = "http://dzsoundnirvana.com/"; switch($UserInput){ case ($UserInput['FLU'] == "" || !ereg($this->user_name_pattern,$UserInpu… $this->login_form(); echo "<div align=\"center\"><font color=\"red\">Invalid user name. Try again or $this->go_back!</font></div>"; $this->login_error == false; break; case ($UserInput['FLP'] == "" || !ereg($this->password_pattern,$UserInput… $this->login_form(); echo "<div align=\"center\"><font color=\"red\">Invalid password. Try again or $this->go_back!</font></div>"; $this->login_error == false; break; } if($this->login_error === false){ //Or you can redirect to a "Forgot password/user name" page or leave it alone. This will kill the script. No output after though! exit; } else{ //Now we go to the Database and validate the user $this->db(); $this->query_string = "SELECT * FROM users WHERE user_name='$UserInput[FLU]' && password='" . md5($UserInput['password']) . "'"; $this->query = mysql_query($this->query_string); //Error check the query if(!$this->query){ echo "System error! Contact the system administrator!<br>or<br>$this->go_back"; //use this for debugging (below), Delete the // at the beginning //echo mysql_error(); } else{ //Need to check if more than 1 user exists if so....throw HACKING error (not supported here)..another class $this->num_rows = mysql_num_rows($this->query); if($this->num_rows > 1){ echo "Hacking warning"; exit; } else{ //Get the user information and set into the $_SESSION and then redirect to the directed page $this->user_information = mysql_fetch_assoc($this->query); //Put all user data into $_SESSION foreach($this->user_information as $key => $value){ $_SESSION[$key] = $value; //Now we redirect to the page specified echo "<script type=\"text/javascript\">window.location… echo "If you are not redirected <a href=\"this->success_login_redirect\" target=\"_parent\">Click here to continue</a>"; } } }//else }//if($this->login_error === false)/else{ }//End function function db(){ $this->db_link = mysql_connect("data_base_host","data_bas… //Select the DB $this->db_select = mysql_select_db("data_base_name"); } }//End Class ?> <? /******************** login.php ********************/ //Start the $_SESSION before any output to the browser session_start(); if((isset($_SESSION['user_ID']) && (isset($_SESSION['token']) && (isset($_SESSION['access_level'])){ //this is where you would put the content you want only validate users to access echo "You are logged in already"; } else{ //Work out of the global scope $UserInput = array(); foreach($_POST as $key => $value){ $UserInput[$key] = $value; } require_once('login.class.php'); //Call the user class $user = new user($UserInput); } ?>
drew at Yahoo! Answers Visit the source
Other answers
Well, the login twice and logout twice thing is a sign that you're not updating your session properly before you do the login/password check. As for the skipping of Drew, that's because you do a row fetch before you get to the while loop, and then do another fetch for the next row.
You should select only the row required from the credentials given "SELECT * FROM school WHERE `username` = '" .mysql_real_escape_string( $_POST['newusername']) ."' AND `password`='" .mysql_real_escape_string( $_POST['newpassword']) ."'" (I had to add line breaks so it outputs in Yahoo Answers but it 'll work with the breaks anyway) As for the session problem, I've had this problem before but can't recall how it was resolved.
Related Q & A:
- What's wrong with this PHP Twitter API POST?Best solution by Stack Overflow
- Whats wrong with my msn?Best solution by Yahoo! Answers
- Whats wrong with my TI-84 calculator?Best solution by Yahoo! Answers
- Whats wrong with my lawn mower?Best solution by Yahoo! Answers
- Whats wrong with my DVD recorder?Best solution by Yahoo! Answers
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.