tailieunhanh - PHP and MySQL Web Development - P104

PHP and MySQL Web Development - P104: PHP and MySQL Web Development teaches the reader to develop dynamic, secure, commercial Web sites. Using the same accessible, popular teaching style of the first edition, this best-selling book has been updated to reflect the rapidly changing landscape of MySQL and PHP. | Implementing User Authentication 487 Listing Continued check if username is unique result mysql_query select from user where username username if result return Could not execute query if mysql_num_rows result 0 return That username is taken - go back and choose another one. if ok put in db result mysql_query insert into user values username password password email if result return Could not register you in database - please try again later. return true There is nothing particularly new in this function it connects to the database we set up earlier. If the username selected is taken or the database cannot be updated it will return false. Otherwise it will update the database and return true. One thing to note is that we are performing the actual database connection with a function we have written called db_connect . This function simply provides a single location that contains the username and password to connect to the database. That way if we change the database password we only need to change one file in our application. The function is shown in Listing . Listing db_connect Function from This Function Connects to the MySQL Database function db_connect result mysql_pconnect localhost bm_user password if result return false if mysql_select_db bookmarks return false return result When users are registered they can log in and out using the regular login and logout pages. We ll build these next. Logging In If users type their details into the form at see Figure and submit it they will be taken to the script called script will log them in if they have 488 Chapter 24 Building User Authentication and Personalization come from this form. It will also display any relevant bookmarks to users who are logged in. It is the center of the rest of the script is shown in Listing . Listing This Script is the Main Hub of the Application php include function files for this application .

TỪ KHÓA LIÊN QUAN