View Single Post
Join Date: Jun 2007
Posts: 211
Reputation: macneato is an unknown quantity at this point 
Solved Threads: 18
macneato's Avatar
macneato macneato is offline Offline
Posting Whiz in Training

Re: To create a registration page and login page

 
0
  #3
Nov 22nd, 2007
Simple PHP login:

Create a database (mysqladmin)

Name the table "dbUsers." It will need 4 fields:

Name Type Addition
id int(10) Primary Key, AUTO_INCREMENT
username varchar(16) Unique
password char(16)
email varchar(25)


Create a new file and name it dbConfig.php This will file will connect to the database

HTML and CSS Syntax (Toggle Plain Text)
  1. <?
  2. // Replace the variable values below
  3. // with your specific database information.
  4. $host = "localhost";
  5. $user = "UserName";
  6. $pass = "Password";
  7. $db = "dbName";
  8.  
  9. // This part sets up the connection to the
  10. // database (so you don't need to reopen the connection
  11. // again on the same page).
  12. $ms = mysql_pconnect($host, $user, $pass);
  13. if ( !$ms )
  14. {
  15. echo "Error connecting to database.\n";
  16. }
  17.  
  18. // Then you need to make sure the database you want
  19. // is selected.
  20. mysql_select_db($db);
  21. ?>

Registration name this file "register.php"

HTML and CSS Syntax (Toggle Plain Text)
  1. <?php
  2.  
  3. // dbConfig.php is a file that contains your
  4. // database connection information. This
  5. // tutorial assumes a connection is made from
  6. // this existing file.
  7. include ("dbConfig.php");
  8.  
  9.  
  10. //Input vaildation and the dbase code
  11. if ( $_GET["op"] == "reg" )
  12. {
  13. $bInputFlag = false;
  14. foreach ( $_POST as $field )
  15. {
  16. if ($field == "")
  17. {
  18. $bInputFlag = false;
  19. }
  20. else
  21. {
  22. $bInputFlag = true;
  23. }
  24. }
  25. // If we had problems with the input, exit with error
  26. if ($bInputFlag == false)
  27. {
  28. die( "Problem with your registration info. "
  29. ."Please go back and try again.");
  30. }
  31.  
  32. // Fields are clear, add user to database
  33. // Setup query
  34. $q = "INSERT INTO `dbUsers` (`username`,`password`,`email`) "
  35. ."VALUES ('".$_POST["username"]."', "
  36. ."PASSWORD('".$_POST["password"]."'), "
  37. ."'".$_POST["email"]."')";
  38. // Run query
  39. $r = mysql_query($q);
  40.  
  41. // Make sure query inserted user successfully
  42. if ( !mysql_insert_id() )
  43. {
  44. die("Error: User not added to database.");
  45. }
  46. else
  47. {
  48. // Redirect to thank you page.
  49. Header("Location: register.php?op=thanks");
  50. }
  51. } // end if
  52.  
  53.  
  54. //The thank you page
  55. elseif ( $_GET["op"] == "thanks" )
  56. {
  57. echo "<h2> Thanks for registering!</h2> ";
  58. }
  59.  
  60. //The web form for input ability
  61. else
  62. {
  63. echo "<form action=\"?op=reg\" method=\"POST\"> \n";
  64. echo "Username: <input name=\"username\" MAXLENGTH=\"16\"> <br /> \n";
  65. echo "Password: <input type=\"password\" name=\"password\" MAXLENGTH=\"16\"> <br /> \n";
  66. echo "Email Address: <input name=\"email\" MAXLENGTH=\"25\"> <br /> \n";
  67. echo "<input type=\"submit\"> \n";
  68. echo "</form> \n";
  69. }
  70. // EOF
  71. ?>

Login name this file "login.php"
HTML and CSS Syntax (Toggle Plain Text)
  1. <?php
  2. session_start();
  3. // dBase file
  4. include "dbConfig.php";
  5.  
  6. if ($_GET["op"] == "login")
  7. {
  8. if (!$_POST["username"] || !$_POST["password"])
  9. {
  10. die("You need to provide a username and password.");
  11. }
  12.  
  13. // Create query
  14. $q = "SELECT * FROM `dbUsers` "
  15. ."WHERE `username`='".$_POST["username"]."' "
  16. ."AND `password`=PASSWORD('".$_POST["password"]."') "
  17. ."LIMIT 1";
  18. // Run query
  19. $r = mysql_query($q);
  20.  
  21. if ( $obj = @mysql_fetch_object($r) )
  22. {
  23. // Login good, create session variables
  24. $_SESSION["valid_id"] = $obj->id;
  25. $_SESSION["valid_user"] = $_POST["username"];
  26. $_SESSION["valid_time"] = time();
  27.  
  28. // Redirect to member page
  29. Header("Location: members.php");
  30. }
  31. else
  32. {
  33. // Login not successful
  34. die("Sorry, could not log you in. Wrong login information.");
  35. }
  36. }
  37. else
  38. {
  39. //If all went right the Web form appears and users can log in
  40. echo "<form action=\"?op=login\" method=\"POST\">";
  41. echo "Username: <input name=\"username\" size=\"15\"><br />";
  42. echo "Password: <input type=\"password\" name=\"password\" size=\"8\"><br />";
  43. echo "<input type=\"submit\" value=\"Login\">";
  44. echo "</form>";
  45. }
  46. ?>

Members Area name this file "members.php", and include on pages that are only for registered users
HTML and CSS Syntax (Toggle Plain Text)
  1. ?php
  2. session_start();
  3.  
  4. if (!$_SESSION["valid_user"])
  5. {
  6. // User not logged in, redirect to login page
  7. Header("Location: login.php");
  8. }
  9.  
  10. // Member only content
  11. // ...
  12. // ...
  13. // ...
  14.  
  15. // Display Member information
  16. echo "<p>User ID: " . $_SESSION["valid_id"];
  17. echo "<p>Username: " . $_SESSION["valid_user"];
  18. echo "<p>Logged in: " . date("m/d/Y", $_SESSION["valid_time"]);
  19.  
  20. // Display logout link
  21. echo "<p><a href=\"logout.php\">Click here to logout!</a></p>";
  22. ?>

logout name this file "logout.php"
HTML and CSS Syntax (Toggle Plain Text)
  1. <?php
  2. session_start();
  3. session_unset();
  4.  
  5. session_destroy();
  6. // Logged out, return home.
  7. Header("Location: index.php");
  8. ?>

There you go... need more help or confused, just ask.

If this information solved your problem. Please add to my reputation.
Thanks
macneato
Last edited by macneato; Nov 22nd, 2007 at 8:10 am. Reason: typo
Reply With Quote