Error » Certification & Programming center Error !! » Programming tutorials » Ajax Registration Form With E-mail Confirmation And Login Form

Programming tutorials All Knowledge Info and links to posted here

Post New Thread Reply
  Ajax Registration Form With E-mail Confirmation And Login Form
LinkBack Thread Tools Display Modes
Old 23-Feb-2007, 06:37 AM   #1 (permalink)
Fixed Error!
 
kingaff's Avatar

Posts: 330
Join Date: Feb 2007
Rep Power: 2 kingaff is on a distinguished road

IM:
Default Ajax Registration Form With E-mail Confirmation And Login Form

config.php file, containing the SQL connection and the variables.

Code:
// Up to you to fill out the vars...

//=====================================================//
// Main Site Settings
//=====================================================//

$sitename = ''; //The name of youe website, self explanatory
$siteurl = ''; //The URL of your site. Must include http:// and trailing slash

//=====================================================//
// mySQL Stuff
//=====================================================//

$host = 'localhost'; //Your mySQL host. If unsure, leave it at localhost
$dbname = ''; //Your mySQL database name
$dbpass = ''; //Your mySQL database password
$dbuser = ''; //Your mySQL database user

//=====================================================//
// Global mySQL Connection
// DO NOT REMOVE!!
//=====================================================//

mysql_connect($host,$dbuser,$dbpass) or die(mysql_error());
mysql_select_db($dbname) or die(mysql_error());
register.php

Code:
<?
session_start();
require("config.php"); // Starts SQL connection and retriever config and vars.

if($_GET['do'] == "ver" && $_GET['email']) // This is the ver part.
{
$result = mysql_query('SELECT * FROM `users` WHERE `email` = "'.$_GET['email'].'"');
while ($row = mysql_fetch_assoc($result))
{
echo '<title>Registration Verification</title>';
echo 'Thanks for coming to the verification page, '.$row['username'].'';
echo '<br><br>Please enter your password below to continue.';
echo '<br>';
?>

<form action="register.php?do=ver&email=<?=$_GET['email']?>&pass=1" method="post">
Password: <input type="password" width="50" name="confpass" /><br />
<input type="Submit" value="Submit Confirmation!" />
</form>

<?
    if($_POST['confpass'] && $_GET['email'] && $_GET['do'] == "ver" && $_GET['pass'] == "1") { // The pass ver part

        $result = mysql_query('SELECT * FROM `users` WHERE `email` = "'.$_GET['email'].'"');
        while ($row = mysql_fetch_assoc($result))
        {
        $verpass = md5($_POST['confpass']);
        if($row['password'] == $verpass)
        {
            echo 'Confirmation Successful!';
                        mysql_query("UPDATE `users` SET `group` = 'SOME_VALIDATED_GROUP' WHERE `email` = ".$_GET['email']) or die(mysql_error());
        }
        else
        {
            echo 'Wrong Password!';
        }

        }
    }
}
die();
}

?>
<script type="text/javascript" language="Javascript">
<!-- AJAX FUNCTIONS - LEAVE THIS ALONE -->
function createXMLHttpRequest() {
var ua;
if(window.XMLHttpRequest) {
    try {
      ua = new XMLHttpRequest();
    } catch(e) {
      ua = false;
    }
  } else if(window.ActiveXObject) {
    try {
      ua = new ActiveXObject("Microsoft.XMLHTTP");
    } catch(e) {
      ua = false;
    }
  }
  return ua;
}
var req = createXMLHttpRequest();
function joinn(username, password, confpass, email) {
  document.getElementById('join').innerHTML = '<img src="./admin/images/loading.gif" alt="Loading" /><br><br>';
  req.open('get', 'regfunc.php?id=register&username=' + username + '&password=' + password + '&confpass=' + confpass + '&email=' + email);
  req.onreadystatechange = handleResponse;
  req.send(null);
}
function handleResponse() {
  if(req.readyState == 4){
    var response = req.responseText;
    var update = new Array();
    //document.getElementById('request').innerHTML = response; // DEBUG USE ONLY (PHP Debugging etc.)
    if(response.indexOf('||' != -1)) {
      update = response.split('||');
      document.getElementById(update[0]).innerHTML = update[1];
    }
  }
}
</script>
<?

if(isset($_SESSION['member'])) // If a user is already logged in...
{
echo '<title>Error</title>Sorry, you are already logged in. <a href=java script:history.back(-1)>Please go back</a>.';
}
else
{
?>

<title>Register</title>
<span id="join">
<form action="java script:joinn(document.req.username.value, document.req.password.value, document.req.confpass.value, document.req.email.value);" name="req">
Username: <input type="text" width="50" name="username" /><br />
Password: <input type="password" width="50" name="password" /><br />
Confirm Password: <input type="password" width="50" name="confpass" /><br />
Email: <input type="text" width="50" name="email" /><br />
<input type="Submit" value="Submit Registration!" /></form>
</span>

<?
}
?>
regfunc.php

Code:
<?
switch($_REQUEST['id']) {
    
case 'register':
    require("config.php"); // Once again start SQL
    session_start(); // Start Session
    $username = htmlentities($_GET['username']); // Get submitted name
    $password = htmlentities($_GET['password']); // Get submitted pass
    $conf = htmlentities($_GET['confpass']); // Get pass confirm
    $email = $_GET['email']; // Get email
    $ip = $_SERVER['REMOTE_ADDR']; // Get IP (security purposes)

// If any info is missing, give error!

    if (!$username) {
        echo "join||";
        echo 'You must enter a username, <a href=java script:window.location=window.location>refresh</a> and try again.';
        die();
    }
    if (!$password) {
        echo "join||";
        echo 'You must enter a password, <a href=java script:window.location=window.location>refresh</a> and try again.';
        die();
    }
    if (!$conf) {
        echo "join||";
        echo 'You must confirm your password, <a href=java script:window.location=window.location>refresh</a> and try again.';
        die();
    }
    if ($conf != $password) {
        echo "join||";
        echo 'Your passwords do not match, <a href=java script:window.location=window.location>refresh</a> and try again.';
        die();
    }
    if (!$email) {
        echo "join||";
        echo 'You must enter an email, <a href=java script:window.location=window.location>refresh</a> and try again.';
        die();
    }
    
    $insertpass = md5($password); // MD5 the submitted pass
    $query = 'INSERT INTO `users` (`username`,`password`,`email`,`ip`,`group`) VALUES ("'.$username.'","'.$insertpass.'","'.$email.'","'.$ip.'","SOME_GROUP_THATS_NOT_YET_VALIDATED")';  
    mysql_query($query) or die(mysql_error()); // Insert Query

    // Mail

            $to = "$email"; // Grab submitted email
            $subject = "Welcome to $sitename!"; // Email Subject
            $message = "Welcome to $sitename, $username! You must click the link below to complete your registration. Please do so and then you may log in at $siteurl. The link you must go to is: $siteurl/register.php?do=ver&email=$email"; // Email message
            $from = "admin@xportal.info"; // Email sender

            mail($to,$subject,$message,"From: $from<$from>
            X-Mailer: PHP/" . phpversion()); // Send Mail!

    echo "join||"; // Some AJAX thing
    echo "Thanks for registering $username! Your have been sent a confirmation E-Mail. Please click on the link to verify yourself. - <a href=java script:window.close()>Click here</a> to close this window.";
    break;

}

?>
login.php

Code:
<?php

if(isset($_SESSION['member']))
{
// Your code if a member is logged in
}
else // If not
{
?>

<script type="text/javascript" language="Javascript">
<!-- AJAX FUNCTIONS - DO NOT TOUCH UNLESS YOU KNOW WHAT YOU'RE DOING -->
function createXMLHttpRequest() {
var ua;
if(window.XMLHttpRequest) {
    try {
      ua = new XMLHttpRequest();
    } catch(e) {
      ua = false;
    }
  } else if(window.ActiveXObject) {
    try {
      ua = new ActiveXObject("Microsoft.XMLHTTP");
    } catch(e) {
      ua = false;
    }
  }
  return ua;
}
var req = createXMLHttpRequest();
function joinn(user, pass) {
  document.getElementById('join').innerHTML = '<img src="./admin/images/loading.gif" alt="Loading" /><br><br>';
  req.open('get', 'loginfunc.php?id=login&user=' + user + '&pass=' + pass);
  req.onreadystatechange = handleResponse;
  req.send(null);
}
function handleResponse() {
  if(req.readyState == 4){
    var response = req.responseText;
    var update = new Array();
    //document.getElementById('request').innerHTML = response; // DEBUG USE ONLY (PHP Debugging etc.)
    if(response.indexOf('||' != -1)) {
      update = response.split('||');
      document.getElementById(update[0]).innerHTML = update[1];
    }
  }
}
</script>

<span id="join">
<form action="java script:joinn(document.req.user.value, document.req.pass.value);" name="req">
Username: <input type="text" name="user" /><br>
Password: <input type="password" name="pass" /><br>
<input type="Submit" value="Submit" /></form>
</span>

<?
}
?>
loginfunc.php

Code:
<?
switch($_REQUEST['id']) {
    
case 'login':

    require("config.php"); // Refer to AJAX Reg Form

    $result = mysql_query("SELECT * FROM `users` WHERE `username` = '".$_GET['user']."'"); // Find the user that's trying to log in
    while ($row = mysql_fetch_assoc($result))
    {
    $name = $row['username']; // Set username
    $passw = $row['password']; // Set correct password
    }
    $user = htmlentities($_GET['user']); // Set submitted Username
    $pass = htmlentities($_GET['pass']); // Set submitted Password
// If anything missing... Error!
    if (!$user) {
        echo "join||";
        echo 'You must enter a username, <a href=java script:window.location=window.location><font color=red>refresh</font></a> and try again.<br><br>';
        die();
    }
    if (!$pass) {
        echo "join||";
        echo 'You must enter a password, <a href=java script:window.location=window.location><font color=red>refresh</font></a> and try again.<br><br>';
        die();
    }

    if($user == $name && md5($pass) == $passw) // Verify U-Name/Pass
    {
    session_start(); // Start session
    $_SESSION['member'] = $user; // Set session variable
    echo "join||";
    echo "Thanks for logging in $user! Please <a href=java script:window.location=window.location><font color=red>refresh</font></a> this page!<br><br>"; // Logged In!
    }
    else
    {
    echo "join||";
    echo 'Wrong username or pass! Please <a href=java script:window.location=window.location><font color=red>refresh</font></a> and try again.<br><br>'; // Or not...
    }
    break;

}
?>
kingaff is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit!
Reply With Quote
   


   
Post New Thread Reply


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

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
Forum Jump


All times are GMT -8. The time now is 12:22 PM.

Powered by vBulletin® Version 3.7.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0

DMCA Policy

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227