Giải pháp thiết kế web động với PHP - p 28

PAGES THAT REMEMBER: SIMPLE LOGIN AND MULTIPAGE FORMS This turns on output buffering and prevents output being sent to the browser until the end of the script, or until you specifically flush the output with ob_end_flush(). 2. Flush the output immediately after invalidating the session cookie like this: // invalidate the session cookie if (isset($_COOKIE[session_name()])) { setcookie(session_name(), '', time()-86400, '/'); } ob_end_flush(); 3. Save , and test the sequence again. This time, there should be no warning. More importantly, the session cookie is no longer valid. Using file-based authentication As you have just seen, the combination of session variables and conditional statements. | PAGES THAT REMEMBER SIMPLE LOGIN AND MULTIPAGE FORMS php session_start ob_start This turns on output buffering and prevents output being sent to the browser until the end of the script or until you specifically flush the output with ob_end_flush . 2. Flush the output immediately after invalidating the session cookie like this invalidate the session cookie if isset _COOKIE session_name setcookie session_name time -86400 ob_end_flush 3. Save and test the sequence again. This time there should be no warning. More importantly the session cookie is no longer valid. Using file-based authentication As you have just seen the combination of session variables and conditional statements lets you present completely different pages to a visitor depending on whether a session variable has been set. All you need to do is add a password checking system and you have a basic user authentication system. In PHP Solution 7-2 I showed you how to use the file function to read each line of a text file into an array. You can now adapt that script to create a simple login system using sessions. Each person s username and password is separated by a comma and recorded on a new line of a text file like this david codeslave chris bigboss I ll use the same text file as before which is in the private folder that was set up in Chapter 7. Refer to Chapter 7 if you haven t set up a folder for PHP to read and write files. PHP Solution 9-3 Building the login page This PHP solution shows how to submit a username and password through the post method and check the submitted values against those stored in an external text file. It uses the file function to inspect the external file one line at a time. If a match is found the script sets a session variable and then redirects the user to another page. 1. Create a file called in the sessions folder and insert a form with a text input field each for username and password plus a submit button named login like this .

Không thể tạo bản xem trước, hãy bấm tải xuống
TỪ KHÓA LIÊN QUAN
TÀI LIỆU MỚI ĐĂNG
77    134    2    19-05-2024
Đã phát hiện trình chặn quảng cáo AdBlock
Trang web này phụ thuộc vào doanh thu từ số lần hiển thị quảng cáo để tồn tại. Vui lòng tắt trình chặn quảng cáo của bạn hoặc tạm dừng tính năng chặn quảng cáo cho trang web này.