Php program code for simple user login

Source: Internet
Author: User
Tags php file rtrim

User login system (no database)

Key code:
LoginUI. php mainly includes:
---------------------------

The code is as follows: Copy code
<? Php
If (error! = Null ){
$ Error = $ _ GET ['error'];
Echo $ error;
Echo "login failed. Please check your username (yugaga) and password (123456 )";
}
?>

----------------------------
Some prompts are displayed after the logon fails.
In loginCheck. php
-----------------------------

The code is as follows: Copy code
<? Php
$ Username = $ _ POST ['username'];
$ Password = $ _ POST ['password'];
If ($ username = "yugaga" & $ password = "123456 & Prime ;){
$ Host = $ _ SERVER ['http _ host'];
$ Uri = rtrim (dirname ($ _ SERVER ['php _ SELF ']),'/');
$ Extra = 'Welcome. php? User = '. $ username;
Header ("Location: http: // $ host $ uri/$ extra");
} Else {
$ Host = $ _ SERVER ['http _ host'];
$ Uri = rtrim (dirname ($ _ SERVER ['php _ SELF ']),'/');
$ Extra = 'loginui. php? Error = failer ';
Header ("Location: http: // $ host $ uri/$ extra");
}
?>

------------------------------------
It mainly verifies the submitted information. Here, regular matching and global variables are used.
Main output information in welcome. php
------------------------------------

The code is as follows: Copy code
<? Php
$ Username = $ _ GET ['user'];
Echo "welcome". $ username. "login ";
?>

User logon system (with database)

We tried to connect to the database. Two files are added, one SqlHepler. php, used to obtain the database connection, a userService. php file, used to process user-related operations, such as user login verification. In this session, we just added a function checkUserLogin ($ username, $ password ), determine whether the user has successfully logged on based on the user name and password! ~

Html code

The code is as follows: Copy code

<! Doctype html public "-// W3C // dtd html 4.01 Transitional // EN" "http://www.w3.org/TR/html4/loose.dtd">
<Html>
<Head>
<Title> simple logon demonstration </title>
</Head>

<Body>
<Center>
<Form action = "loginCheck. php" method = "post">
User account: <input type = "text" name = "username" size = "20"/> <br/>
User secrets: <input type = "password" name = "password" size = "20"/> <br/>
<Input type = "submit" value = "submit"/>
</Form>
<? Php
$ Error = $ _ GET ['error'];
If ($ error = 'failer '){
Echo "login failed. Please check your username (yugaga) and password (123456 )";
 }
?>
</Center>

</Body>
</Html>


Main code:
-----------------------------------
<? Php
Include_once 'sqlhelper. Php ';
Function checkUserLogin ($ username, $ password ){
$ Res = mysql_query ("select * from users where username = '". $ username ."'");
If ($ listuser = mysql_fetch_array ($ res ))! = Null ){
If ($ listuser [2] ==$ password ){
Return true;
}
} Else {
Return false;
}
}
?>
-------------------------------------
SqlHelper code:
------------------------------------
<? Php
$ Server = "localhost ";
$ User = "root ";
$ Pass = "";
$ Con = mysql_connect ($ server, $ user, $ pass );
Mysql_select_db ("login", $ con );
?>
----------------
Code after successful login

<? Php
$ Username = $ _ GET ['user'];
Echo "welcome". $ username. "login ";
?>

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.