Đang chuẩn bị liên kết để tải về tài liệu:
PHP and MySQL Web Development - P104

Đang chuẩn bị nút TẢI XUỐNG, xin hãy chờ

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 24.9 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 24.10. Listing 24.10 db_connect Function from db_fns.php 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 login.php see Figure 24.3 and submit it they will be taken to the script called member.php.This 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 application.This script is shown in Listing 24.11. Listing 24.11 member.php This Script is the Main Hub of the Application php include function files for this application .