Laboratory Experiment - 8a
a. 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.
<?php
// File to store the visitor count
$counterFile = "counter.txt";
// Check if the file exists
if (!file_exists($counterFile)) {
// Create the file and set the initial count to 0
file_put_contents($counterFile, "0");
}
// Read the current count
$counter = (int)file_get_contents($counterFile);
// Increment the visitor count
$counter++;
// Update the file with the new count
file_put_contents($counterFile, $counter);
?>
<!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;
background-color: #f4f4f9;
color: #333;
text-align: center;
padding: 50px;
}
h1 {
font-size: 2.5em;
color: #5a5af0;
}
.counter {
margin-top: 20px;
font-size: 1.8em;
color: #ff4500;
}
footer {
margin-top: 50px;
font-size: 0.9em;
color: #888;
}
</style>
</head>
<body>
<h1>Welcome to My Web Page!</h1>
<p>You are visitor number:</p>
<div class="counter"><?php echo $counter; ?></div>
<footer>© <?php echo date("Y"); ?> My Website</footer>
</body>
</html>
Output:Explanation:
1. File to Store Visitor Count
- Purpose: This variable defines the name of the file (counter.txt) where the visitor count is stored.
- Why It's Needed: The file is used for persistent storage so that the visitor count remains even after the web server restarts.
2. Checking and Creating the Counter File
- Purpose: This checks if the file counter.txt exists.
- If the file does not exist, it creates the file and initializes the count with 0.
- Why It's Needed: Ensures the program can work even if the file is missing. The file_put_contents function writes the string "0" into the file to initialize the counter.
3. Reading the Current Count
- Purpose: The file_get_contents function reads the contents of counter.txt.
- Typecasting: The (int) ensures that the retrieved value is treated as an integer, even if stored as a string.
- Why It's Needed: This gives the program the current visitor count to update.
4. Incrementing the Counter
- Purpose: The ++ operator increases the value of $counter by 1 to represent a new visitor.
- Why It's Needed: Tracks each new visit or page load.
5. Writing the Updated Count Back to the File
- Purpose: This writes the updated count back into the counter.txt file.
- Why It's Needed: Updates the persistent storage so that the visitor count is saved for future visits.
- Purpose: Displays the visitor counter dynamically using PHP embedded in HTML.
HTML Breakdown:
- <h1>: Displays a welcome heading.
- <p>: Briefly informs the user about the visitor count.
- <div class="counter">: Contains the actual visitor number, displayed using PHP's echo $counter.
CSS Breakdown:
- Styling Elements:
- body: Sets the font, background color, text alignment, and padding for a clean layout.
- .counter: Customizes the appearance of the counter number (e.g., color and size).
- footer: Adds a footer with the current year dynamically generated using date("Y").
1. What is the purpose of this program?
- Answer:
The program keeps track of the number of visitors to a web page and displays the total count to users. It uses a text file for persistent storage, so the count remains consistent even if the server restarts or the page is reloaded. It provides a basic implementation of a counter to measure website visits.
Comments
Post a Comment