942,517 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Unsolved
  • Views: 81
  • PHP RSS
Jul 25th, 2010
0

PHP log in help!

Expand Post »
Hi I am quite new to PHP. All of today I have been working on a PHP user login script. I got it working fine, then I added some HTML to make it look better. The page shows up, but when i write my username and password in it just refreshes the screen. Can anyone help me. The code should be below. If you need the whole project, say.
PHP Syntax (Toggle Plain Text)
  1.  
  2. <?php
  3. include("conf.inc.php"); // Includes the db and form info.
  4. session_start(); // Starts the session.
  5. if ($_SESSION['logged'] == 1) { // User is already logged in.
  6. header("Location: index.php"); // Goes to main page.
  7. exit(); // Stops the rest of the script.
  8. } else {
  9. if (!isset($_POST['submit'])) { // The form has not been submitted.
  10. echo "<html><body bgcolor='#f1f1f1' text='#000000' link='#33cc00'>
  11. <table align='center' width='40%'>
  12. <br><br><td><font face=\"arial\" size=\"2\">Log in</font></td>
  13. </table>
  14. <table align='center' width='40%' bgcolor='#FFFFFF' style='borger-color:#999999;border-style:solid;border-width:1px'>
  15. <tr>
  16. <td>
  17. <br>
  18. <center>
  19. <form action=\"login.php\" method=\"POST\">";
  20. echo "
  21. <input name=\"username\" type=\"text\" size=\"25\" /><br>
  22. <input name=\"password\" type=\"password\" size=\"25\" /><br>
  23. <input type=\"submit\" value=\"Submit\" />
  24. </form>
  25. <br></font>
  26.  
  27. </center>
  28. </td>
  29. </tr>
  30.  
  31. </table>
  32. </body>
  33. </html>
  34. ";
  35.  
  36.  
  37. } else {
  38. $username = form($_POST['username']);
  39. $password = md5($_POST['password']); // Encrypts the password.
  40.  
  41. $q = mysql_query("SELECT * FROM `users` WHERE username = '$username' AND password = '$password'") or die (mysql_error()); // mySQL query
  42. $r = mysql_num_rows($q); // Checks to see if anything is in the db.
  43.  
  44. if ($r == 1) { // There is something in the db. The username/password match up.
  45. $_SESSION['logged'] = 1; // Sets the session.
  46. header("Location: index.php"); // Goes to main page.
  47. exit(); // Stops the rest of the script.
  48. } else { // Invalid username/password.
  49. exit("Incorrect username/password!"); // Stops the script with an error message.
  50. }
  51. }
  52. }
  53. mysql_close($db_connect); // Closes the connection.
  54. ?>
Here is the working code.
PHP Syntax (Toggle Plain Text)
  1.  
  2. <?php
  3. include("conf.inc.php"); // Includes the db and form info.
  4. session_start(); // Starts the session.
  5. if ($_SESSION['logged'] == 1) { // User is already logged in.
  6. header("Location: index.php"); // Goes to main page.
  7. exit(); // Stops the rest of the script.
  8. } else {
  9. if (!isset($_POST['submit'])) { // The form has not been submitted.
  10. echo "<form action=\"login.php\" method=\"POST\">";
  11. echo "<table>";
  12. echo "<tr>";
  13. echo "<td colspan=\"2\">Login:</td>";
  14. echo "</tr>";
  15. echo "<tr>";
  16. echo "<td width=\"50%\">Username:</td><td width=\"50%\"><input name=\"username\" size=\"18\" type=\"text\" />";
  17. echo "</tr>";
  18. echo "<tr>";
  19. echo "<td width=\"50%\">Password:</td><td width=\"50%\"><input name=\"password\" size=\"18\" type=\"text\" />";
  20. echo "</tr>";
  21. echo "<tr>";
  22. echo "<td colspan=\"2\"><input type=\"submit\" name=\"submit\" value=\"submit\"</td>";
  23. echo "</tr>";
  24. echo "</table>";
  25. echo "</form>";
  26. } else {
  27. $username = form($_POST['username']);
  28. $password = md5($_POST['password']); // Encrypts the password.
  29.  
  30. $q = mysql_query("SELECT * FROM `users` WHERE username = '$username' AND password = '$password'") or die (mysql_error()); // mySQL query
  31. $r = mysql_num_rows($q); // Checks to see if anything is in the db.
  32.  
  33. if ($r == 1) { // There is something in the db. The username/password match up.
  34. $_SESSION['logged'] = 1; // Sets the session.
  35. header("Location: index.php"); // Goes to main page.
  36. exit(); // Stops the rest of the script.
  37. } else { // Invalid username/password.
  38. exit("Incorrect username/password!"); // Stops the script with an error message.
  39. }
  40. }
  41. }
  42. mysql_close($db_connect); // Closes the connection.
  43. ?>
Sorry, it is quite messy
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
stu9954 is offline Offline
3 posts
since Apr 2010
Jul 26th, 2010
0
Re: PHP log in help!
you need to have your trailing slashes on all " and '
Reputation Points: 15
Solved Threads: 15
Posting Pro in Training
whiteyoh is offline Offline
474 posts
since Jun 2009
Jul 26th, 2010
0
Re: PHP log in help!
You need to debug the code to see why it is doing what it is doing. When I started in computers they filled a good size room and computer time was so valuable that you had to desk check your code for problems because there was no other option. I think that we are well past that. That is not to say that you shouldn't have a look for an obvious problem but if it isn't obvious then you can let the machine help you. If you are going to be a (PHP) programmer then you need to develop a debugging approach and you will probably want to get a debugging tool to help you. I built my own but there are open source debugging tools that you can get.

So back to your problem! For starters, you have a version of your form in both modules. The first thing that I would want to know (if I were you) is which module am I in when the screen "refreshes". It is re-displaying your form but which version is getting displayed. If you are in the Login module and it just keeps re-displaying the form when you click on the Submit button then your IF statement isn't working the way you expected. In that case, i'd want to see what the actual data value is. That can be as simple as echo'ing the value to see if it what you expected. You just keep narrowing it down until the problem is obvious or until you see that the data isn't what you expected but you can't figure out why. That might be the point at which it would make sense to post something and ask for help.

Someone else may look at your code and pick out the problem for you but that doesn't change the fact that dumping a bunch of code without going through some debugging steps on your own isn't the right way to go.
Reputation Points: 210
Solved Threads: 228
Nearly a Posting Virtuoso
chrishea is offline Offline
1,389 posts
since Sep 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: PHP Shortlist
Next Thread in PHP Forum Timeline: Store pdf content to mysql





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC