How can PHP developers prevent direct access to specific pages after successful login, to avoid unauthorized access through search engines or direct URLs?
To prevent direct access to specific pages after successful login, PHP developers can implement a session-based authentication system. By setting a session variable upon successful login and checking for its presence on restricted pages, developers can ensure that only authenticated users can access those pages. This prevents unauthorized access through search engines or direct URLs.
session_start();
// Check if the user is logged in
if(!isset($_SESSION['user_id'])) {
header("Location: login.php");
exit();
}
// Restricted page content here
Related Questions
- What are some potential pitfalls when dynamically generating SQL queries based on user input in PHP?
- Are there any security concerns when using placeholders and variable replacement in PHP strings?
- How can the HTML5 progress element be utilized to display file upload progress without relying heavily on PHP?