Some time ago, I was doing a winning activity, and I used it to write data into the session after winning the prize, and then the user submitted the data for verification, the following is not a similar example. Some time ago, I was doing a winning activity, and I used it to write data into the session after winning the prize, and then the user submitted the data for verification, the following is not a similar example.
Script ec (2); script
The logon page is:
Login
Page for storing sessions:
Header ("Content-Type: text/html; charset = utf8 ");
If (! Isset ($ _ POST ["submit"]) {
Die ("error execution ");
} // Check for any submit operation
Require_once ('connect. php'); // link to the database
If (isset ($ _ POST ['name']) & isset ($ _ POST ['Password']) {// if neither user name nor password is blank
$ Name = $ _ POST ['name'];
$ Password = $ _ POST ['Password'];
$ SQL = "SELECT id, limits, message FROM user WHERE username = '$ name' AND password =' $ password' LIMIT 1 ";
$ Result = mysqli_query ($ con, $ SQL); // execute the SQL user name and password
$ Rows = mysqli_num_rows ($ result); // returns whether the user name and password exist.
If ($ rows! = 0 ){
Session_start ();
While ($ rows_other = mysqli_fetch_assoc ($ result )){
$ _ SESSION ['id'] = $ rows_other ['id'];
$ _ SESSION ['name'] = $ name;
$ _ SESSION ['limit'] = $ rows_other ['limit'];
$ _ SESSION ['message'] = $ rows_other ['message'];
}
Header ("refresh: 0; url = welcome. php"); // returns to the welcome.html page
Exit;
} Else {
Echo "incorrect user name or password ";
Echo "script"
Alert ('user name or password error ');
Settimeout(function({{windolocation.href}'login.html ';}, 1000 );
Script ";
}
} Else {
Echo "incomplete form filling ";
Echo "script"
Alert ('Incomplete forms ');
Settimeout(function({{windolocation.href}'login.html ';}, 1000 );
Script ";
}
?>
After logging on to the page, different permissions and user names are displayed based on different users:
Document
Session_start ();
If (isset ($ _ SESSION ['id']) {
Require_once ('connect. php ');
$ Id = $ _ SESSION ['id'];
$ Name = $ _ SESSION ['name'];
$ Limits = $ _ SESSION ['limit'];
$ Message = $ _ SESSION ['message'];
If ($ limits = 1 ){
Echo 'hello, Postmaster '.'
';
} Else {
Echo 'helo, normal user '.'
';
}
Echo 'Hello you name is: '. $ name;
} Else {
Echo 'not logged on! ';
Header ("refresh: 310000url1_login.html ");
}
?>
?>
Session considerations
1. When session is to be used on the current page, we add session_start () when there is no input at the beginning of the file ();
2. We can modify the time limit of the session as follows:
In fact, PHP5 Session also provides a function session_set_cookie_params (); To set the lifetime of PHP5 Session. This function must be called before the session_start () function is called:
// Save for one day
$ LifeTime = 24*3600;
Session_set_cookie_params ($ lifeTime );
Session_start ();
?>