To create an HTML page that accepts an input code, stores it in cookies (which are persistent) and ensures that users cannot change it afterward, you can use JavaScript along with HTML. Here's a simple implementation:
HTML Code
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Code Inputter</title>
<script>
// Function to set a cookie
function setCookie(name, value, days) {
const d = new Date();
d.setTime(d.getTime() + (days * 24 * 60 * 60 * 1000));
const expires = "expires=" + d.toUTCString();
document.cookie = name + "=" + value + ";" + expires + ";path=/";
}
// Function to get a cookie
function getCookie(name) {
const cookieArr = document.cookie.split(";");
for (let i = 0; i < cookieArr.length; i++) {
const cookiePair = cookieArr[i].split("=");
if (name === cookiePair[0].trim()) {
return decodeURIComponent(cookiePair[1]);
}
}
return null;
}
// Function to check for existing code and handle the input
function checkCode() {
const storedCode = getCookie("userCode");
if (storedCode) {
document.getElementById("output").innerText = "Your code is: " + storedCode;
document.getElementById("inputArea").style.display = "none"; // Hide input if code exists
} else {
document.getElementById("inputArea").style.display = "block"; // Show input if no code exists
}
}
// Function to handle form submission
function handleSubmit(event) {
event.preventDefault();
const inputCode = document.getElementById("codeInput").value;
// Store the code in a cookie and set its expiration to 365 days
setCookie("userCode", inputCode, 365);
// Show the stored code
checkCode();
}
// Call checkCode when the window loads
window.onload = checkCode;
</script>
</head>
<body>
<h1>Code Inputter</h1>
<div id="inputArea">
<form onsubmit="handleSubmit(event)">
<label for="codeInput">Enter your code:</label>
<input type="text" id="codeInput" required>
<button type="submit">Submit</button>
</form>
</div>
<div id="output"></div>
</body>
</html>
How It Works
-
Cookies: The JavaScript functions setCookie and getCookie handle setting and retrieving cookies, respectively.
-
Checking for Existing Code: The checkCode function checks if a user code is already stored. If it is, it displays the code and hides the input area.
-
Form Submission: When the user submits the form, the handleSubmit function prevents the default form submission behavior, retrieves the input value, and stores it in a cookie.
-
Persistence: The cookies are set to expire in 365 days, implicating that a user cannot change it once set.
-
UI: The input field and submit button are displayed only if there is no code set in the cookies.
Usage
Copy the above HTML code and save it to an .html file. Open the file in a web browser, and you can test the behavior as described. Once you enter a code and submit it, the input area will disappear and display the code. You won't be able to change it without clearing your cookies or using another browser.