Php user login

Source: Internet
Author: User
Today, I have nothing to do with it. I will post the php user logon code of my own blog system to share with you. let's take a look at the principle. The principle is very simple: the user enters the user name and password and then

I have nothing to do today. I will post the php user logon code of my own blog system to share with you. let's take a look at the principle. The principle is very simple, the user enters the user name and password and then uses the php file to determine whether the database exists. if the user is saved, the user name information is saved to the session, which is the simplest. it is also a common method. well, let's not talk much about it. let's look at the Code First:

  1.  
  2.  

Their names are myname and mypass, respectively. now we have a simple js client for better user experience:

  1.  
  2. Script

The function of this function is to determine whether the user has entered a valid user name and password. The most important thing is how to deal with php code.

  1. Session_start (); // you must declare this and give a small tip: no output can be made before the session. There may be problems in versions earlier than php.5.
  2.  
  3. $ Myname = get_value ('myname', post );
  4. $ Mypass = get_value ('mypass', post );
  5. If (! Preg_match ("/^ \ w + $/", $ myname) | strlen ($ myname) <3 | strlen ($ myname)> 15 ){
  6. Alert ('The user name entered is incorrect! The username must consist of English letters, numbers, and underlines. it must be 3-15 characters in length! ','');
  7. }
  8. If (! Preg_match ("/^ \ w + $/", $ mypass) | strlen ($ mypass) <6 | strlen ($ mypass)> 15 ){
  9. Alert ('Enter the user password! The password must consist of English letters, numbers, underscores (_) and a length of 6 to 15 characters! ','');
  10. }
  11. $ SQL = "select * from tbn where admin_name = '$ myname' and admin_pwd ='". md5 ($ mypass )."'";
  12. $ Result = mysql_query ($ SQL );
  13. If (mysql_num_rows ($ result )){
  14. $ My = mysql_fetch_array ($ result );
  15. $ _ SESSION ['uid'] = $ myname;
  16. // $ _ SESSION ['auth'] = return_auth ($ my ['group _ id']); // This is because the user group is used to obtain the permissions of the user group.
  17. Header ("location: main. php ");
  18. } Else {
  19. Alert ('prompt: the user name and password you entered are inconsistent! ','');
  20. }
  21. ?>

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.