Use session verification in php-PHP source code

Source: Internet
Author: User
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 ();
?>

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.