944,006 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Unsolved
  • Views: 599
  • PHP RSS
Nov 25th, 2008
0

Name and Password login

Expand Post »
I have been trying to find a simple name and password tutorial that works and is easy to follow. Not having much luck.
The name and password will be in a text file or mysql database which the user will be given, i do not need forgot password or email address etc. ( I can add later if need be)
I want members to log-in which will then give them a form to fill in and send. (About 5 members so not a lot)
PHP5 and MySql 5 and apache installed and hopefully working correctly this end.
I have managed to make a form and use php to extract and send the form data to an e-mail address with a thank-you going to the actual persons e-mail address on the form they filled in so I am hoping this will be quite easy to do.
Take it slowly with me as still green at coding all this.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
stealthmode is offline Offline
15 posts
since Apr 2007
Nov 25th, 2008
0

Re: Name and Password login

Hopefully this should get you started. I've commented the code to make understanding easier, let me know if you've any questions; I'm here to help!

PHP Syntax (Toggle Plain Text)
  1. <?php
  2. session_start();
  3.  
  4. if (isset($_POST['submit'])) {
  5. // Connect to the database or die.
  6. $connection = mysql_connect("DB-ADDRESS", "USERNAME", "PASSWORD");
  7. if (!$connection) { die("The Database connection failed. " . "<br />" . mysql_error()); }
  8.  
  9. // Select the table within the database or die.
  10. $database_select = mysql_select_db("TABLENAME", $connection);
  11. if (!$database_select) { die("The Database selection failed. " . "<br />" . mysql_error()); }
  12.  
  13. // Get the POST variables.
  14. $myusername = $_POST['myusername'];
  15. $mypassword = $_POST['mypassword'];
  16.  
  17. // To protect MySQL injection, we stripslashes and use mysql_real_escape_string.
  18. $myusername = stripslashes($myusername);
  19. $mypassword = stripslashes($mypassword);
  20. $myusername = mysql_real_escape_string($myusername);
  21. $mypassword = mysql_real_escape_string($mypassword);
  22.  
  23. // Run a query to bring back the users' record from the database.
  24. $result = mysql_query("SELECT * FROM TABLENAME WHERE username = '$myusername' AND password = '$mypassword' LIMIT 1", $connection);
  25.  
  26. // If the query is not set meaning it failed, then die.
  27. if (!$result) { die("The Database query failed. " . "<br />" . mysql_error()); }
  28.  
  29. // If it successfully returned one row...
  30. if (mysql_num_rows($result) == 1) {
  31. // Bind the returned results to an array.
  32. $row = mysql_fetch_array($result);
  33.  
  34. // Register $_SESSION variables and redirect to "success.php".
  35. $_SESSION["myusername"] = $myusername;
  36. $_SESSION["firstname"] = $firstname;
  37.  
  38. // Redirect the person to the new page.
  39. header("location:success.php");
  40. }
  41. }
  42. ?>
  43. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  44. <html xmlns="http://www.w3.org/1999/xhtml">
  45. <head>
  46. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  47. <title>PHP: Login</title>
  48. </head>
  49.  
  50. <body>
  51. <form name="loginForm" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
  52. <label for="myusername">Username</label> <br />
  53. <input name="myusername" id="myusername" type="text" /> <br /> <br />
  54. <label for="mypassword">Password</label> <br />
  55. <input name="mypassword" id="mypassword" type="text" /> <br /> <br />
  56. <input name="submit" id="submit" type="submit" value="Login" />
  57. </form>
  58. </body>
  59. </html>
Reputation Points: 14
Solved Threads: 8
Junior Poster
antwan1986 is offline Offline
110 posts
since May 2008
Nov 25th, 2008
0

Re: Name and Password login

There is a tutorial that I would recommend. It can be found at
http://www.jdtmmsm.com/tutorials/bas...entication.php

Hoe This Helps
Reputation Points: 10
Solved Threads: 3
Junior Poster in Training
PomonaGrange is offline Offline
67 posts
since Jun 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: Can anybody help me in writing this very simple script
Next Thread in PHP Forum Timeline: mail() custom format?





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


Follow us on Twitter


© 2011 DaniWeb® LLC