John Davidson

How to create a cookie to store the timestamp of when a page is first loaded with php

0 comments
Message:


I would really appreciate any help if possible!


This is the question:
Use a cookie (NOT PHP SESSION) that expires a year from now to do the following:
Record the timestamp of the FIRST load of this page (but NOT any subsequent load of this page).


The code:


<!DOCTYPE html>
<html>
<head>
<title>User Profile</title>
</head>
<body>
<h3><?=$message?></h3>

User Profile:
<br><br>
<form action="" method="POST" name="form1" onsubmit="return validate_form()">
<input type="hidden" name="task" value="process_profile">

<input type="checkbox" name="is_cool" value="yes">
Are you cool?
<br><br>
What Bands do you like?
<br>
<select name="like_bands[]" multiple> <small>* Required Field</small>
<option value="Sabbath">Black Sabbath</option>
<option value="Mastodon">Mastodon</option>
<option value="Metallica">Metallica</option>
<option value="Swift">Taylor Swift</option>
</select>
<br><br>
Favorite band not in the above list.
<br>
<input type="text" name="other_band" value="">
<br><br>
<button type="submit"> Continue/Confirm </button>
</form>

<script>
//////////////////////////////////////////////////////////////////////////////////////////////////////////
// Client-side form validation
// Don't change the names of stuff in the form, and you won't have to change anything below
// Used built-in JS instead of JQuery or other validation tool
//////////////////////////////////////////////////////////////////////////////////////////////////////////
function validate_form() {

var form_obj = document.form1;

var count_liked = 0;
for (var i=0 ; i < form_obj['like_bands[]'].options.length ; i++ ) {
if (form_obj['like_bands[]'].options[i].selected) {
count_liked++;
}
}

if (count_liked == 0) {
alert("You must choose a band from the menu.");
return false; // cancel form submission
}

return true; // trigger form submission
}
</script>

</body>
</html>


My Answer:
To create a cookie to store the timestamp of when a page is first loaded with PHP, you can use the following code:

php
// Check if the cookie is already set
if(!isset($_COOKIE['first_load_timestamp'])) {
// Set the cookie with the current timestamp
setcookie('first_load_timestamp', time(), time() + 31536000); // Expires in a year
}

// Retrieve the timestamp from the cookie
$first_load_timestamp = $_COOKIE['first_load_timestamp'];

// Display the timestamp
echo "First load timestamp: " . date('Y-m-d H:i:s', $first_load_timestamp);
?>


This code checks if the cookie 'first_load_timestamp' is already set. If it is not set, it sets the cookie with the current timestamp and expires in a year. Then, it retrieves the timestamp from the cookie and displays it on the page.

Please note that cookies are stored on the client-side, so the timestamp may not be completely accurate if the user clears their cookies or uses a different device.

Rate this post

4 of 5 based on 1264 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us