iEntry 10th Anniversary Webforumz RegistrationAnnouncements Contact Webforumz StaffContact
Home Resources Blogs Meet the Team Contact Register
 

Go Back   WebForumz.com > The Code > PHP

Reply
 
LinkBack Thread Tools
Old July 4th, 2009, 05:37 PM   #1
New Member
 

Join Date: Jul 2009
Location: NY
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Rep Altering Power: 0 TechRunner is on a distinguished road
Create a Registration page and Login page

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
Code:
<?
// Replace the variable values below
// <strong class="highlight">with</strong> your specific database information.
$host = "localhost";
$user = "UserName";
$pass = "Password";
$db   = "dbName";

// This part sets up the connection to the 
// database (so you don't need to reopen the connection
// again on the same page).
$ms = mysql_pconnect($host, $user, $pass);
if ( !$ms )
{
echo "Error connecting to database.\n";
}

// Then you need to <strong class="highlight">make</strong> sure the database you want
// is selected.
mysql_select_db($db);
?>
Registration name this file "register.php"
Code:
<?php

// dbConfig.php is <strong class="highlight">a</strong> file that contains your
// database connection information. This
// tutorial assumes <strong class="highlight">a</strong> connection is made from
// this existing file.
include ("dbConfig.php");


//Input vaildation and the dbase code
if ( $_GET["op"] == "reg" )
 {
 $bInputFlag = false;
 foreach ( $_POST as $field )
  {
  if ($field == "")
   {
   $bInputFlag = false;
   }
  else
   {
   $bInputFlag = true;
   }
  }
 // If we had problems <strong class="highlight">with</strong> the input, exit <strong class="highlight">with</strong> error
 if ($bInputFlag == false)
  {
  die( "Problem <strong class="highlight">with</strong> your registration info. "
   ."Please go back and try again.");
  }

 // Fields are clear, add user to database
 //  Setup query
 $q = "INSERT INTO `dbUsers` (`username`,`password`,`email`) "
  ."VALUES ('".$_POST["username"]."', "
  ."PASSWORD('".$_POST["password"]."'), "
  ."'".$_POST["email"]."')";
 //  Run query
 $r = mysql_query($q);
 
 // <strong class="highlight">Make</strong> sure query inserted user successfully
 if ( !mysql_insert_id() )
  {
  die("Error: User not added to database.");
  }
 else
  {
  // Redirect to thank you page.
  Header("Location: register.php?op=thanks");
  }
 } // end if


//The thank you page
elseif ( $_GET["op"] == "thanks" )
 {
 echo "<h2>Thanks for registering!</h2>";
 }
 
//The web form for input ability
else
 {
 echo "<form action=\"?op=reg\" method=\"POST\">\n";
 echo "Username: <input name=\"username\" MAXLENGTH=\"16\"><br />\n";
 echo "Password: <input type=\"password\" name=\"password\" MAXLENGTH=\"16\"><br />\n";
 echo "Email Address: <input name=\"email\" MAXLENGTH=\"25\"><br />\n";
 echo "<input type=\"submit\">\n";
 echo "</form>\n";
 }
// EOF
?>
Login name this file "login.php"
Code:
<?php
session_start();
// dBase file
include "dbConfig.php";

if ($_GET["op"] == "login")
 {
 if (!$_POST["username"] || !$_POST["password"])
  {
  die("You need to provide <strong class="highlight">a</strong> username and password.");
  }
 
 // Create query
 $q = "SELECT * FROM `dbUsers` "
  ."WHERE `username`='".$_POST["username"]."' "
  ."AND `password`=PASSWORD('".$_POST["password"]."') "
  ."LIMIT 1";
 // Run query
 $r = mysql_query($q);

 if ( $obj = @mysql_fetch_object($r) )
  {
  // <strong class="highlight">Login</strong> good, create session variables
  $_SESSION["valid_id"] = $obj->id;
  $_SESSION["valid_user"] = $_POST["username"];
  $_SESSION["valid_time"] = time();

  // Redirect to member page
  Header("Location: members.php");
  }
 else
  {
  // <strong class="highlight">Login</strong> not successful
  die("Sorry, could not log you in. Wrong <strong class="highlight">login</strong> information.");
  }
 }
else
 {
//If all went right the Web form appears and users can log in
 echo "<form action=\"?op=login\" method=\"POST\">";
 echo "Username: <input name=\"username\" size=\"15\"><br />";
 echo "Password: <input type=\"password\" name=\"password\" size=\"8\"><br />";
 echo "<input type=\"submit\" value=\"Login\">";
 echo "</form>";
 }
?>
Members Area name this file "members.php", and include on pages that are only for registered users
Code:
<?php
session_start();

if (!$_SESSION["valid_user"])
{
// User not logged in, redirect to <strong class="highlight">login</strong> page
Header("Location: <strong class="highlight">login</strong>.php");
}

// Member only content
// ...
// ...
// ...

// Display Member information
echo "<p>User ID: " . $_SESSION["valid_id"];
echo "<p>Username: " . $_SESSION["valid_user"];
echo "<p>Logged in: " . date("m/d/Y", $_SESSION["valid_time"]);

// Display logout link
echo "<p><a href=\"logout.php\">Click here to logout!</a></p>";
?>
Logout name this file "logout.php"
Code:
<?php
session_start();
session_unset();

session_destroy();
// Logged out, return home.
Header("Location: index.php");
?>
As for requesting passwords.

Yes. It can be done.
Ask the user to enter their email address.
select password from your_db where email_address = 'their_address'
Use the mail() function to send the password to the email address.

SQL:
SELECT
dbusers.password
FROM
dbusers
WHERE
(dbusers.email = 'pforgot')

'pforgot' is any name you've given to the form text input.
TechRunner is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
Old July 8th, 2009, 06:19 AM   #2
Reputable Member
 

Join Date: Apr 2009
Location: The Toon
Posts: 120
Thanks: 0
Thanked 0 Times in 0 Posts
Rep Altering Power: 0 davidj is on a distinguished road
Re: Create a Registration page and Login page

I would contact admin about these guides / tutorials and ask that they be placed in a better location else they will be lost forever
davidj is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
How to create a login page (beginner)?? sing2trees Databases 13 October 15th, 2009 05:49 AM
login page in asp.net htmlforums .NET 2 February 18th, 2009 05:57 AM
How to create a registration page with approval? sing2trees PHP 7 May 30th, 2008 07:38 PM
Login page HELP!!!!! biggy1985 PHP 1 April 25th, 2007 11:38 AM
Login / Registration help Noobie PHP 1 April 14th, 2006 11:01 AM


Search Engine Optimization by vBSEO 3.2.0 RC8