09 Check Username Availability On Signup

Started by Mindless, July 24, 2012, 10:50:09 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Mindless

#1
Found in various example scripts over the net.

Save and upload namecheck.php
<?php
if (empty($_GET['wantusername'])) {
die(
'Silly Rabbit - Twix are for kids - You cant post nothing please enter a username !');
}
sleep(1);
require_once(
'include/bittorrent.php');
dbconn();

$HTMLOUT ='';

$lang array_mergeload_language('global'), load_language('takesignup') );

function 
validusername($username)
{
global 
$lang;
if (
$username == "")
return 
false;
$namelength strlen($username);
if( (
$namelength 3) OR ($namelength 32) )
{
$HTMLOUT ='';
$HTMLOUT .= "<font color='#cc0000'>{$lang['takesignup_username_length']}</font>";
echo 
$HTMLOUT;
exit();
}
// The following characters are allowed in user names
$allowedchars $lang['takesignup_allowed_chars'];
for (
$i 0$i $namelength; ++$i)
{
if (
strpos($allowedchars$username[$i]) === false)
return 
false;
}
return 
true;
}

if (!
validusername($_GET["wantusername"])){
$HTMLOUT .= "<font color='#cc0000'>{$lang['takesignup_allowed_chars']}</font>"
echo 
$HTMLOUT;
exit();
}

if (
strlen($_GET["wantusername"]) > 12){
$HTMLOUT .= "<font color='#cc0000'>{$lang['takesignup_username_length']}</font>";
echo 
$HTMLOUT;
exit();
}

$checkname sqlesc($_GET["wantusername"]);
$sql "SELECT username FROM users WHERE username = $checkname";
$result sql_query($sql);
$numbers mysql_num_rows($result); 

if(
$numbers 0
{
while( 
$namecheck mysql_fetch_assoc($result) ) { 
$HTMLOUT .= "<font color='#cc0000'><font size='2'><b><img src='{$TBDEV['pic_base_url']}cross.png' alt='Cross' title='Username  Not Available' align='absmiddle' />Sorry... Username - ".htmlspecialchars($namecheck["username"])." is already in use.</font>"

}
else 
{
$HTMLOUT .= "<font color='#33cc33'><font size='2'><b><img src='{$TBDEV['pic_base_url']}tick.png' alt='Tick' title='Username Available' align='absmiddle' /> Username Available</font>";
}

echo 
$HTMLOUT;
exit();
?>



Save and upload check.js to scripts folder :
function checkit() {
wantusername = document.getElementById('wantusername').value;
var url = '../namecheck.php?wantusername=' + escape(wantusername);
try {
request = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
request = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e2) {
request = false;
}
}

if (!request && typeof XMLHttpRequest != 'undefined') {
request = new XMLHttpRequest();
}
request.open("GET", url, true);
global_content = wantusername;
request.onreadystatechange = check;
request.send(null);
}

function check() {
if (request.readyState == 4) {
if (request.status == 200) {
var response = request.responseText;
document.getElementById("namecheck").innerHTML = response;
if(response.substring(0,20) == "<font color='#cc0000'>")
document.reform.submitt.disabled = true;
else if(response.substring(0,20) == "<font color='#33cc33'>")
document.reform.submitt.disabled = false;
}
}
}




@File signup.php find :
<script type='text/javascript' src='captcha/captcha.js'></script>


Under it add :
<script type='text/javascript' src='scripts/check.js'></script>



Find :
<tr><td align='right' class='heading'>{$lang['signup_uname']}</td><td align='left'><input type='text' size='40' name='wantusername' /></td></tr>


Change to :
<tr><td align='right' class='heading'>{$lang['signup_uname']}</td><td align='left'><input type='text' size='40' name='wantusername' id='wantusername' onblur='checkit();' /><div id='namecheck'></div></td></tr>