Issue Description:
A user is experiencing issues with their PHP login script. The script aims to query a database and insert a token and user general authentication code into a separate table based on user credentials. However, the script currently does not check the tokens table. Additionally, the user wants to display the logged-in user's username on pages that require access restrictions.
Solution:
1. Database Cleanup:
The provided MySQL tables are not secure. The password column should be stored in a hashed format for added protection.
2. Centralizing Login Functionality:
Instead of repeating login code on multiple pages, create a separate init.php file to handle database connection and login initialization. This file can be included in every PHP page that requires login functionality.
3. Improved Login Script:
Rewrite the login script in ajax/login.php to sanitize user input, verify credentials against the database, and use PHP sessions to store the logged-in user's information.
4. Using Ajax for Login:
Implement Ajax to handle login requests asynchronously from the login form. This prevents page reloads and provides a more user-friendly experience.
5. Access Restriction:
On pages that require login, add a verification check at the beginning to redirect unauthenticated users to the login page.
6. Displaying Username:
Use PHP sessions to store the logged-in user's username. On pages where the username should be displayed, simply output the value using echo $_SESSION['username'];.
Additional Tips:
The above is the detailed content of How to Build a Secure and User-Friendly Member-Only Login System in PHP?. For more information, please follow other related articles on the PHP Chinese website!