PHP user login registration module implementation

Source: Internet
Author: User
Tags valid email address

PHP user login module implementation

 

Function scripts included in the project:

Login. php // log on

Reg. php // register a user

User_add.php // register the verification script

User_login_check.php // logon verification script

Image. php // Verification Code Image Generation script

 

Process:

Design Database:

Including uid, user name, password, nickname, gender, email address, registration time

The SQL statement is as follows:

Create Table users (UID bigint (20) not null auto_increment primary key unique key, username varchar (100) default null, userpassword varchar (100) default null, nickname varchar (100) default null, sex tinyint (1) default 0, email varchar (100) default null, regtime timestamp (14) default 0 );

 

Registration Module:

Reg. php fill in the registration information, and use the JS script to determine whether the entered information is legal

If it is valid, submit the form and register the user through user_add.php.

The user_add.php script provides the following functions:

Determine whether the input information is valid. This JS can be implemented, but to ensure browser compatibility, use PHP to implement the JS function again.

If it is legal and has not been registered before, you can register it. Otherwise, an error message is displayed.

 

Logon module:

Login. php fill in the login information, JS judge whether the input information is valid

If a valid form is submitted, log on to the system using user_login_check.php.

User_login_check.php provides the following functions:

Checks whether the input information is valid. It has the same function as Js.

If the username and password are correct, save the session variable and allow the user to log on.

 

Verification Code module:

Prevents malicious registration to generate verification codes. The 5-digit verification codes in PNG format are generated.

Session variables are required, and the server needs to install the GD library.

 

Note:

1. Javascript script

<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 CT!

{

If (Reg. username. value = "")

{

Alert ("Enter the user name ");

Return false;

}

Else if (! Isvalid ())

{

Alert ("the user name can only contain numbers and letters ");

Return false;

}

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

{

Alert ("enter the password ");

Return false;

}

Else if (Reg. userpassword. value! = Reg. cuserpassword. value)

{

Alert ("two different passwords ");

Return false;

}

Else if (Reg. Nickname. value = "")

{

Alert ("Enter your nickname ");

Return false;

}

Else if (Reg. Email. value = "")

{

Alert ("Enter your email address ");

Return false;

}

Else if (! Chkemail (Reg. Email. Value ))

{

Alert ("enter a valid email address! ");

Return false;

}

Else return true;

}

</SCRIPT>

Method to respond to JS scripts: <form name = "Reg" Action = "user_login_check.php" method = "Post" target = "_ Self" onsubmit = "Return docheck ()">

When the form is submitted (Some browsers still submit the form when docheck () returns an error, this is why we should continue to use PHP in the form processing script to re-verify the input information)

Only the user name and password can be verified during login.

 

2. Database Connection

$ Strsql = "";

$ Database_username = "root ";

$ Database_password = "ssklzs ";

$ Database_name = "userlog ";

$ S_username = $ _ post ["username"];

$ S_userpassword = $ _ post ["userpassword"];

$ Strsql = "select * from 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

// Connect to the database

$ Link = mysql_connect ("localhost", $ database_username, $ database_password) or die ("cocould 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 completed

If ($ ROW = mysql_fetch_object ($ result )){···}

Else {···}

 

3. session variable settings

Session is a channel for passing parameters between different PHP scripts.

There are roughly two session variables. One is used to save the verification code and the other is used to record the login user.

In general, session. auto_start = 0 in the PHP. ini file indicates that the session is not automatically enabled. Therefore, you need to set this value to 1 when using the session.

Note the initialization and cancellation of session Variables

 

4. Return button

Echo ("<input type = 'button 'name = 'btn 'value =' Return 'onclick = 'window. History. Go (-1) '/> ")

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.