Laboratory Experiment - 8a
- Get link
- X
- Other Apps
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.
2. How does the program store the visitor count?
- Answer:
The visitor count is stored in a file named counter.txt. The program reads the count from this file whenever the page is loaded, increments it by one, and writes the updated count back to the file. This ensures the visitor count is saved persistently.
3. Why do you use a file to store the visitor count?
- Answer:
Using a file like counter.txt provides a simple and lightweight method for storing data persistently without requiring a database. It is suitable for small-scale applications and makes the program easy to deploy on basic web servers.
4. How do you ensure the file exists before using it?
- Answer:
The program uses the file_exists() function to check if the counter.txt file exists. If the file does not exist, it creates the file using the file_put_contents() function and initializes the visitor count to 0.
5. Can this program track unique visitors?
- Answer:
No, this program counts total page visits, not unique visitors. Every page reload or revisit by the same user increments the counter. To track unique visitors, the program would need to use cookies, sessions, or IP tracking.
6. What are the limitations of using a text file for storage?
- Answer:
- A text file is not suitable for handling a large number of concurrent users as it can lead to file locking issues.
- There is no built-in mechanism for handling data corruption.
- It lacks advanced querying capabilities, which makes it difficult to perform analytics.
- It can be slower compared to databases for larger-scale applications.
7. How can this program be made more scalable?
- Answer:
To make the program scalable: - Replace the text file with a database such as MySQL or SQLite to handle larger data sets and concurrent users efficiently.
- Use cookies or sessions to track unique visitors.
- Implement caching mechanisms to reduce file read/write operations on every page load.
8. How do you dynamically display the visitor count?
- Answer:
The program uses PHP’s echo statement to embed the updated count into the HTML code. This ensures the visitor count displayed on the page is always current, based on the latest value in the counter.txt file.
9. What PHP functions are used in the program?
- Answer:
The program uses the following PHP functions: - file_exists(): Checks if the counter.txt file exists.
- file_get_contents(): Reads the current visitor count from the file.
- file_put_contents(): Writes the updated count back to the file.
- date(): Dynamically displays the current year in the footer.
10. How does the CSS improve the program?
- Answer:
CSS enhances the visual appeal and usability of the program by: - Styling the background, text, and layout to make the page look professional.
- Highlighting the visitor count with distinct colors and fonts.
- Ensuring the page is mobile-friendly using responsive design principles like padding and text alignment.
- Get link
- X
- Other Apps
Comments
Post a Comment