Skip to content
Home » program 18

program 18

Write a program to create a login form. On submitting the form, the user
should navigate to profile page.

Here’s a simple PHP program to create a login form that redirects to a profile page upon successful login. This example demonstrates basic authentication by verifying a hardcoded username and password for demonstration purposes.

Step 1: Create the Login Form (login.php)

<!DOCTYPE html>

<html lang=”en”>

<head>

    <meta charset=”UTF-8″>

    <meta name=”viewport” content=”width=device-width, initial-scale=1.0″>

    <title>Login</title>

</head>

<body>

    <h2>Login Form</h2>

    <form action=”authenticate.php” method=”post”>

        <label for=”username”>Username:</label>

        <input type=”text” id=”username” name=”username” required><br><br>

        <label for=”password”>Password:</label>

        <input type=”password” id=”password” name=”password” required><br><br>

        <button type=”submit”>Login</button>

    </form>

</body>

</html>

This form sends the entered username and password to authenticate.php for processing.


Step 2: Authenticate the User (authenticate.php)

In authenticate.php, verify the user’s credentials. If the login is successful, start a session and redirect to the profile page; otherwise, show an error message.

<?php

session_start();

// Hardcoded credentials for demonstration

$validUsername = “user”;

$validPassword = “password”;

// Retrieve form data

$username = $_POST[‘username’];

$password = $_POST[‘password’];

// Check credentials

if ($username === $validUsername && $password === $validPassword) {

    // Successful login, set session variables

    $_SESSION[‘username’] = $username;

    // Redirect to profile page

    header(“Location: profile.php”);

    exit();

} else {

    // Invalid credentials

    echo “Invalid username or password. <a href=’login.php’>Try again</a>.”;

}

?>

Note: This example uses hardcoded credentials for simplicity. In a real application, retrieve and verify user credentials from a database.


Step 3: Create the Profile Page (profile.php)

In profile.php, check if the user is logged in by verifying the session variable. If not, redirect them back to the login page.

<?php

session_start();

// Check if the user is logged in

if (!isset($_SESSION[‘username’])) {

    // Redirect to login page if not logged in

    header(“Location: login.php”);

    exit();

}

?>

<!DOCTYPE html>

<html lang=”en”>

<head>

    <meta charset=”UTF-8″>

    <meta name=”viewport” content=”width=device-width, initial-scale=1.0″>

    <title>Profile Page</title>

</head>

<body>

    <h2>Welcome to Your Profile Page, <?php echo htmlspecialchars($_SESSION[‘username’]); ?>!</h2>

    <p>This is a protected page that only logged-in users can access.</p>

    <a href=”logout.php”>Logout</a>

</body>

</html>


Step 4: Logout Functionality (logout.php)

Add a logout.php page to destroy the session and log out the user.

<?php

session_start();

session_unset();

session_destroy();

// Redirect to login page after logout

header(“Location: login.php”);

exit();

?>


Summary of Files

  1. login.php: The login form.
  2. authenticate.php: Verifies credentials and starts a session.
  3. profile.php: Protected page, accessible only when logged in.
  4. logout.php: Logs out the user by destroying the session.