PHP User Login and Registration page

Source: Internet
Author: User
Tags valid email address

PHP User Login Module implementation

The project contains a feature script:

login.php//Login

reg.php//Registered Users

user_add.php//Registration Check Script

user_login_check.php//Login Check Script

image.php//Verification Code Picture generation script

Process:

Design database:

Includes user uid, username, password, nickname, gender, email, registration time

The SQL statements are as follows

CREATE TABLE users (UID bigint) NOT NULL Auto_increment PRIMARY key unique key,username varchar (+) default NULL, user Password varchar (+) default NULL, nickname varchar (+) default null, sex tinyint (1) Default 0,email varchar (+) Defaul T null, regtime timestamp (+) default 0);

Registration module:

reg.php fill in the registration information, through the JS script to determine whether the information entered is legitimate

If it is legal, then submit the form and register the user via user_add.php

The user_add.php script implements the following functions:

To determine whether the input information is legitimate, this JS can be implemented, but in order to browser compatibility here with PHP to realize the function of JS again

If it is legal and has not been registered before, then you can register, otherwise prompt error message

Login module:

login.php Fill in the login information, JS to determine whether the input information is legitimate

If the form is legally submitted, log in Via user_login_check.php

The user_login_check.php implementation functions are as follows:

Determine whether the input information is legal, and the same as JS function

Save the session variable if it is valid and the user name password is true, allowing the user to log in

Verification code module:

Prevents malicious registration from generating a verification code, resulting in a 5-bit digital verification code in PNG format

Need to use the session variable, the server needs to install the GD library

Specific precautions:

1. JavaScript scripts

<script language= "JavaScript" >

function IsDigit (Ccheck)

{

Return ((' 0 ' <=ccheck) && (ccheck<= ' 9 '));

}

function Isalpha (Ccheck)

{

Return ((' a ' <=ccheck) && (ccheck<= ' z ')) | | (' A ' <=ccheck) && (ccheck<= ' Z '));

}

function IsValid ()

{

var strusername=reg. Username.value;

for (nindex=0;nindex<strusername.length;nindex++)

{

Ccheck=strusername.charat (NIndex);

if (! ( IsDigit (Ccheck) | | Isalpha (Ccheck)))

{

return false;

}

}

return true;

}

function Chkemail (str)

{

Return Str.search (/[\w\-]{1,}@[\w\-]{1,}\.[ \w\-]{1,}/) ==0?true:false;

}

function Docheck ()///////////////this Place has a very important problem. MF can ' t do as Wo expect!

{

if (Reg. username.value== "")

{

Alert ("Please fill in User name");

return false;

}

else if (! IsValid ())

{

Alert ("User name can only use numbers and letters");

return false;

}

else if (Reg. userpassword.value== "")

{

Alert ("Please fill in the password");

return false;

}

else if (Reg. Userpassword.value!=reg. Cuserpassword.value)

{

Alert ("Two times password input is different");

return false;

}

else if (Reg. nickname.value== "")

{

Alert ("Please fill in a nickname");

return false;

}

else if (Reg. email.value== "")

{

Alert ("Please fill in your email address");

return false;

}

else if (!chkemail (Reg. Email.value))

{

Alert ("Please fill in a valid email address!") ");

return false;

}

else return true;

}

</script>

How to respond to JS script: <form name= "Reg" action= "user_login_check.php" method= "post" target= "_self" onsubmit= "return Docheck ()" >

Respond when submitting a form (some browsers still continue to submit the form when Docheck () returns an error, which is why it is still possible to continue using PHP in the form processing script to verify the input information again)

Login can only verify the user name and password

2. Database connection

$STRSQL = "";

$database _username= "root";

$database _password= "Ssklzs";

$database _name= "Userlog";

$s _username=$_post["UserName"];

$s _userpassword=$_post["userpassword"];

$STRSQL = "SELECT * from the users where username like '". $s _username. "' and UserPassword like '". $s _userpassword. "'";

$STRSQL = "INSERT into users (username,userpassword,nickname,sex,email,regtime) VALUES ('". $s _username. "', '". $s _ UserPassword. "', '". $s _nickname. "', '". $s _sex. "', '". $s _email. "', Now ())";//now () Current system time

Connecting to a database

$link =mysql_connect ("localhost", $database _username, $database _password) or Die ("Could not connect MySQL:". Mysql_ Error ());

mysql_select_db ($database _name, $link) or Die ("Can-not-use Userlog:". Mysql_error ());

$result =mysql_query ($STRSQL);

SQL execution Complete

if ($row =mysql_fetch_object ($result)) {...}

else{}

3.Session variable setting

Session is a channel for transferring parameters between different PHP scripts

Session variables need roughly two, one to save the verification code, a user to log the login

In general, the php.ini file Session.auto_start = 0 indicates that the session does not open automatically, so you need to set this value to 1 when using the session

Note Initialization and logoff of Session variables

4. Back button

Echo ("<input type= ' button ' name= ' btn ' value= ' return ' onclick= ' Window.history.go ( -1) '/>")

PHP User Login and Registration page

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.