Program 8A

Develop a PHP program (with HTML/CSS) to keep track of the number of visitors visiting the web page and to display this count of visitors, with relevant headings.

Instructions to Execute program

  1. Create a file name called track.php, copy the below code and paste it and save it.
  2. Copy track.php file and open XAAMP directory if installed else install it click here
  3. There you’ll find a folder named “htdocs”.
  4. Inside the “htdocs” folder, paste track.php file.
  5. After then open your XAAMP and start the Apache server.
  6. Open your favorite browser; we recommend using Google Chrome or Mozilla Firefox.
  7. Then, go to the URL “http://localhost/track.php“.

CODE

<?php
$counterFile = "counter.txt";

if (!file_exists($counterFile)) {
    file_put_contents($counterFile, "0");
}

$currentCount = file_get_contents($counterFile);
$newCount = $currentCount + 1;
file_put_contents($counterFile, $newCount);
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Visitor Counter</title>

    <style>
        body {
            font-family: Arial, sans-serif;
            text-align: center;
            margin: 0;
            padding: 0;
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
            background-color: #f4f4f9;
        }

        .container {
            background: #ffffff;
            padding: 20px;
            width: 60%;
            border-radius: 8px;
            box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
        }

        h1 {
            font-size: 2.5em;
            margin: 0;
        }

        p {
            font-size: 1.2em;
            color: #555;
        }
    </style>
</head>

<body>
    <div class="container">
        <h1>Welcome to Our Website!</h1>
        <p>You are visitor number:
            <strong><?php echo $newCount; ?></strong>
        </p>
    </div>
</body>
</html>


    

Output

Program8aOutput
Copied to Clipboard!