Full PHP Ajax user registration and User name Verification Example _php Tutorial

Source: Internet
Author: User
Tags mysql tutorial




<title>Insert title here</title>




checkusername.php file

$username = $_get[' username ');
MySQL tutorial _connect (' 127.0.0.1 ', ' root ', ') or die (' could not connect: '. Mysql_error ());
mysql_select_db (' Test ');

When stitching SQL statements, you must handle the values entered by the user, replace special characters, enclose them in quotation marks
$username = Htmlentities ($username, ent_quotes);
$sql = "SELECT count (*) from user where username = ' {$username} '";

$result = mysql_query ($sql);
if ($row = Mysql_fetch_array ($result, mysql_num)) {
$cnt = $row [0];
if ($cnt = = 0) {
Echo ' User name is not used ';
} else {
Echo ' username is already in use, please change username ';
}
}
?>

Verifycode.php Verification Code Program


Session_Start ();

$rnd = Array_merge (range (0,9), Range (' A ', ' Z '));
Shuffle ($RND);

$code = Implode (' ', $rnd);
$code = substr ($code, 0,4);

$_session[' verifycode '] = $code;

Header ("Content-type:image/png");
$im = @imagecreate (+) or Die ("Cannot initialize new GD image stream");
$background _color = imagecolorallocate ($im, 255, 255, 255);
$text _color = imagecolorallocate ($im, 233, 14, 91);
Imagestring ($im, 5, 5, 5, $code, $text _color);
Imagepng ($im);
Imagedestroy ($im);


?>
checkcode.php Check the user input verification code is consistent
Session_Start ();

$code = $_get[' code ');

echo strtoupper ($code) = = $_session[' Verifycode '? ' Verification code correct ': ' Captcha error ';
?>

Register.php Registration Handler

Session_Start ();

$filename = ";
if (isset ($_files[' photo ')) {
$uploaddir = DirName (__file__). Directory_separator. ' Upload ';
$originfilename = $_files[' photo ' [' name '];
$extname = Strtolower (substr ($originfilename, Strrpos ($originfilename, '. ') +1));

$filename = Time (). '.' . $extname;

$validext = array (' jpg ', ' jpeg ', ' gif ', ' PNG ');
if (!in_array ($extname, $validext)) {
ErrorMsg ("Wrong file type");
}

if ($_files[' photo ' [' size '] > 100*1024) {
ErrorMsg ("File is too large to exceed 100k");
}
$uploadfile = $uploaddir. Directory_separator. $filename;

if (!move_uploaded_file ($_files[' photo '] [' tmp_name '], $uploadfile)) {
ErrorMsg ("Photo upload failed");
}
}

$code = $_post[' Verifycode ');
if ($code! = $_session[' Verifycode ')} {
ErrorMsg ("Verification code input is incorrect");
}

mysql_connect (' 127.0.0.1 ', ' root ', ');
mysql_select_db (' Test ');
mysql_query (' Set names UTF8 ');

$username = addslashes ($_post[' username ');
$PWD = MD5 ($_post[' pwd ");
$name = addslashes ($_post[' name ');
$sex = addslashes ($_post[' sex ');
$age = intval ($_post[' age ');
$mobile = addslashes ($_post[' mobile ');
$address = Addslashes ($_post[' address ');
$email = addslashes ($_post[' email ');
$photo = $filename;
$birthday = addslashes ($_post[' birthday ');

$sql = "INSERT into user (Username,pwd,name,sex,age,mobile,address,email,photo,birthday)
VALUES (' $username ', ' $pwd ', ' $name ', ' $sex ', $age, ' $mobile ', ' $address ', ' $email ', ' $photo ', ' $birthday ');

if (!mysql_query ($sql)) {
ErrorMsg (' Database tutorial write not successful! ');
}

$sql = ' SELECT * from user ';
$res = mysql_query ($sql);
Echo '






























Echo '

'; while ($row = Mysql_fetch_assoc ($res)) {$photo = $row [' Photo ']? "" : ''; echo " "; }
User name Name Gender Age Cell phone Correspondence Address e-mail address Date of birth
{$row [' username ']} {$row [' name ']} {$row [' Sex ']} {$row [' age ']} {$row [' Mobile ']} {$row [' address ']} {$row [' email ']} {$row [' Birthday ']}
';


function ErrorMsg ($STR) {
Die (');
}


?>

The simplest database structure

Drop database if exists test;
Create DATABASE test character set UTF8 collate utf8_general_ci;

Use test;

CREATE TABLE User (
Username char (TEN) Primary key
, pwd char (+) NOT NULL
, name Char (TEN) NOT NULL
, sex char (1) NOT NULL
, Mobile char (11)
, age smallint
, address varchar (50)
, email varchar (30)
, photo varchar (20)
, Birthday date
);

Http://down.bKjia.c0m/down/code/php/qitayuanma/2010/1220/22331.html

http://www.bkjia.com/PHPjc/631710.html www.bkjia.com true http://www.bkjia.com/PHPjc/631710.html techarticle !doctype HTML public-//w3c//dtd HTML 4.01 transitional//en http://www.w3.org/tr/html4/loose.dtd HTML head meta http- Equiv=content-type content=text/html; charset=gb2312 titleinser ...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.