Example of session logon verification in php
-
-
- Login
-
-
-
-
II. logon detection page
- @ 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 "Welcome to log on. Click here to enter the welcome page ";
- }
- 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 ";
- }
- ?>
3. log out of the logon page
- Unset ($ _ SESSION ['username']);
- Unset ($ _ SESSION ['passcode']);
- Unset ($ _ SESSION ['userflag']);
- Echo "logout successful ";
- ?>
4. logon success prompt page
- 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 "logout ";
- }
- Else
- {
- Echo "you are not authorized to access this page ";
- }
- ?>
|