PHP session application instance logon Verification

Source: Internet
Author: User
Tags php session

CopyCode The Code is as follows: <HTML>
<Head>
<Title> login </title>
<Meta http-equiv = "Content-Type" content = "text/html; charset = gb2312">
</Head>

<Body>
<Form name = "form1" method = "Post" Action = "login. php">
<Table width = "300" border = "0" align = "center" cellpadding = "2" cellspacing = "2">
<Tr>
<TD width = "150"> <Div align = "right"> User name: </div> </TD>
<TD width = "150"> <input type = "text" name = "username"> </TD>
</Tr>
<Tr>
<TD> <Div align = "right"> password: </div> </TD>
<TD> <input type = "password" name = "passcode"> </TD>
</Tr>
<Tr>
<TD> <Div align = "right"> cookie retention time: </div> </TD>
<TD> <select name = "cookie" id = "cookie">
<Option value = "0" selected> browser Process </option>
<Option value = "1"> Save for 1 day </option>
<Option value = "2"> Save for 30 days </option>
<Option value = "3"> 365 days </option>
</SELECT> </TD>
</Tr>
</Table>
<P align = "center">
<Input type = "Submit" name = "Submit" value = "Submit">
<Input type = "reset" name = "reset" value = "reset">
</P>
</Form>
</Body>
</Html>

Copy code The Code is as follows: <? PHP
@ Mysql_connect ("localhost", "root", "1981427") // You must connect to the database server before selecting a database.
Or die ("database server connection failed ");
@ Mysql_select_db ("test") // select the database mydb
Or die ("the database does not exist or is unavailable ");
// Obtain user input
$ Username = $ _ post ['username'];
$ Passcode = $ _ post ['passcode'];
// Execute an SQL statement to obtain the session Value
$ Query = @ mysql_query ("select username, userflag from users"
. "Where username = '$ username' and passcode =' $ passcode '")
Or die ("SQL statement execution failed ");
// Determine whether the user exists and the password is correct
If ($ ROW = mysql_fetch_array ($ query ))
{
Session_start (); // indicates the start of the session.
// Determine whether the user's permission information is valid. If the value is 1 or 0, the permission information is valid.
If ($ row ['userflag'] = 1 or $ row ['userflag'] = 0)
{
$ _ Session ['username'] = $ row ['username'];
$ _ Session ['userflag'] = $ row ['userflag'];
Echo "<a href =" Main. php "href =" Main. php "> welcome to log on. Click here to go to the welcome page </a> ";
}
Else // output error message if the permission information is invalid
{
Echo "incorrect user permission information ";
}
}
Else // if the user name and password are incorrect, an error is returned.
{
Echo "incorrect user name or password ";
}
?>

Copy codeThe Code is as follows: <? PHP
Unset ($ _ session ['username']);
Unset ($ _ session ['passcode']);
Unset ($ _ session ['userflag']);
Echo "logout successful ";
?>

Copy code The Code is as follows: <? PHP
Session_start ();
If (isset ($ _ session ['username'])
{
@ Mysql_connect ("localhost", "root", "1981427") // You must connect to the database server before selecting a database.
Or die ("database server connection failed ");
@ Mysql_select_db ("test") // select the database mydb
Or die ("the database does not exist or is unavailable ");
// Obtain the session
$ Username = $ _ session ['username'];
// Execute the SQL statement to obtain the userflag Value
$ Query = @ mysql_query ("select userflag from users"
. "Where username = '$ username '")
Or die ("SQL statement execution failed ");
$ ROW = mysql_fetch_array ($ query );
// Compare the permission information in the current database with that in the session. If the permission information is different, update the session information.
If ($ row ['userflag']! = $ _ Session ['userflag'])
{
$ _ Session ['userflag'] = $ row ['userflag'];
}
// Output different welcome information based on the session Value
If ($ _ session ['userflag'] = 1)
Echo "Welcome administrator". $ _ session ['username']. "log on to the system ";
If ($ _ session ['userflag'] = 0)
Echo "welcome". $ _ session ['username']. "log on to the system ";
Echo "<a href =" logout. php "href =" logout. php "> logout </a> ";
}
Else
{
Echo "You are not authorized to access this 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.