Tuesday, 4 October 2016

Nairaland Registration System Using PHP and MYSQLI

Today, we are providing you an easy registration process using MYSQL connection with better password encryption, which has an advantage of working on different database systems. MYSQL is a PHP extension that allow us to connect to our database. This registration process is used in our Nairaland Forun System. Here is the demo of this cool and registration process. This will give you an inside of how the registration s
http://forum.thewallclone.com/comfirm_email.php
ystem works on nairaland

DB name is Nairaland


this is the table structure for users


--
-- Database: `nairaland`
--

-- --------------------------------------------------------

--
-- Table structure for table `users`
--

CREATE TABLE IF NOT EXISTS `users` (
`uid` int(11) NOT NULL AUTO_INCREMENT,
`username` varchar(20) NOT NULL,
`password` varchar(100) NOT NULL,
`email` varchar(30) NOT NULL,
`passreset` varchar(100) NOT NULL,
`status` int(11) NOT NULL,
`validecode` varchar(20) NOT NULL,
`created` varchar(20) NOT NULL,
PRIMARY KEY (`uid`),
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;



This tutorial contain 5 files
1) config file 2) index 3) do_confirm_email 4) register 5) success

Config.php
Database configuration

define('DB_SERVER', 'localhost');
define('DB_USERNAME', 'root');
define('DB_PASSWORD', '');
define('DB_DATABASE', 'nairaland');
$db = mysqli_connect(DB_SERVER,DB_USERNAME,DB_PASSWORD,DB_DATABASE);



index.php
contains html code for registration form


<div class ="panel-body">
<!-- Section Title -->
<div class="section-title">
<h1>Submit Your Email To Join </h1>
<div class="divider"></div>
</div>
</div>
</div>
<div class="row">
<div class ="panel panel-default">
<div class ="panel-body">
Before you can join, we need to verify your email address. Please enter it below:
</div>
<div class ="panel-heading">
<form class="form-inline" action="do_confirm_email.php?" method="get">
<!--<div class="form-group"> <label for="exampleInputName2">Name</label> <input type="text" class="form-control" id="exampleInputName2" placeholder="Jane Doe"> </div> -->

<div class="form-group"> <label for="exampleInputEmail2">Email</label>
<input type="email" name="email" required class="form-control" id="exampleInputEmail2" placeholder="jane.doe@example.com"> </div>
<button type="submit" name="invite" class="btn btn-primary">Send invitation</button>
<br/><small id="emailHelp" class="form-text text-muted">We'll never share your email with anyone else if you don't want to.</small>
</form>
</div>
</div>
</div> <!-- /row -->


do_confirm_email.php


/* --------------------------------------------------------------------------------------- */
if(isset($_GET['invite'])){
$email = $_GET['email']; //Storing email in $username variable.
$code=md5(uniqid(true)); // random alphernumeric character store in $code variable

/* checking if user already exist */
$match = $db->query("SELECT * FROM users WHERE email ='$email' AND status='1' AND username!='0' ");
@$mysql_num_rows = mysqli_num_rows($match);

/* checking if requesed to join */
$requested = $db->query("SELECT * FROM users WHERE email ='$email' AND status='0' AND username='' ");
@$num_rows = mysqli_num_rows($requested);
$data=mysqli_fetch_array($requested);

/* --------------------------------------------------------------------------------------- */

if($mysql_num_rows)// if already requested, send new link!'
{
echo ' <div class="section-title">
<h1>Already a member!</h1>
<div class="divider"></div>
</div>';

} else
/* --------------------------------------------------------------------------------------- */
// if not registered run the below script,
if (!$num_rows)
{

//echo "$email $code";

$db->query("INSERT INTO users (email, validcode) VALUES ('$email', '$code')"); // insert into our table users with unique random code


/* Send a registration link to entered email address */
$to = $email;
$subject = "Confirm registration";
$header = "Nairaland Demo";
$body = "Thank you for registering with us. You are now part of a great community
For active your account, visit the link below to complete your registration:
<a href='http://www.thewallclone.com/nl/register?email=$email&validcode=$code'>Activate</a>
(You must complete your registration within a few days)";

$sent=mail($to,$subject,$body,$header);

/* if email is sent display success message */
if ($sent) {

echo ' <div class="section-title">
<h1>Please Check Your E-Mail!</h1>
<p>We have just sent you an e-mail.<p>Please <b>check your inbox</b>
to find the e-mail and <b>complete your registration</b>.
<p>Please <b>check your spam folder</b> if you cannot find it in your inbox. <p>Welcome to Nairaland!<p>
<div class="divider"></div>
</div>';
} /* end if statement for sent */

}
/* --------------------------UPDATE REGISTRATION LINK--------------------------------------------- */
if ($num_rows) {
$dbCode=$data['validcode'];
$dbEmail=$data['email'];
echo "$email $dbCode";
/* Send a registration link to entered email address */
$to = $email;
$subject = "Confirm registration";
$header = "Nairaland Demo";
$body = "Thank you for registering with us. You are now part of a great community
For active your account, visit the link below to complete your registration:
<a href='http://www.thewallclone.com/nl/register?email=$dbEmail&validcode=$dbCode'>Activate</a>
(You must complete your registration within a few days)";

$sent=mail($to,$subject,$body,$header);

/* if email is sent display success message */
if ($sent) {

echo '<div class="section-title">
<h1>Link resent!</h1>

</div>';
} /* end if statement for sent */
/* --------------------------------------------------------------------------------------- */
}

} /* end if get statement */


register.php


if(isset($_GET['validcode']) && ($_GET['email']) )
{

$code = $_GET['validcode'];
$email = $_GET['email'];

echo "$email";

$match = $db->query("SELECT * FROM users WHERE email ='$email' AND validcode='$code' AND status='0' ");
$mysql_num_rows = mysqli_num_rows($match);


if($mysql_num_rows)
{



echo '<div class = "panel panel-default">
<div class = "panel-body">
Complete Registration
</div>
<div class = "panel-heading">
<form class="form-inline" action="success.php" method="post">
<div class="form-group"> <label for="exampleInputName2">Name</label>
<input type="text" name="username" class="form-control" id="exampleInputName2" placeholder="Jane Doe"> </div>
<input type="hidden" name="email" value="'.$email.'">

<div class="form-group"> <label for="exampleInputEmail2">Password</label>
<input type="password" name="password" required class="form-control" id="exampleInputEmail2" placeholder=""> </div>
<button type="submit" name="join" class="btn btn-primary">Join</button>
</form>
</div>
</div>';
}

else
{
echo '<div class="section-title">
<h1>An error occured!</h1>
<div class="divider"></div>
<a href="index.php">Resend Link</a>
</div>';
}
}

Success.php


if (isset($_POST['join'])) {
# code...
$username = $_POST['username'];
$password = md5($_POST['password']);
$email = ($_POST['email']);

$match = $db->query("SELECT * FROM users WHERE username='$username'");
$exist = mysqli_num_rows($match);

if ($exist) {

echo '<div class="section-title">
<h1 style="color:red;">Username Not Available!</h1>
<div class="divider"></div>
</div>';
}

if (!$exist) {

$db->query(" UPDATE users SET username='$username', password='$password', status='1', validcode='0'
WHERE email='$email'");

$to = $email;
$subject = "Thank you for registering";
$header = "You are Welcome to Codexpress Labs Forum!";
$body = "Greetings, $username:
Thank you for registering with us. You are now part of a great community
thewallclone.com/login.php";

$sent=mail($to,$subject,$body,$header);


if ($sent) {
echo '<div class="section-title">
<h1>Registrtion Confirmed!</h1>
<div class="divider"></div>
</div>';
}
}
}



2 comments:

  1. Check out nairaland forum system script version 1.0 with php, mysqli, javascript www.thewallclone.con/theidea

    ReplyDelete

Hit me with a comment!