PHP script to check cookie existence and display a message
8. Check for "visited" Cookie
Write a PHP script to check if a cookie named "visited" exists. If it does, display a welcome message; otherwise, display a default message.
Sample Solution:
PHP Code :
<?php
$cookieName = "visited";
if (isset($_COOKIE[$cookieName])) {
echo "Welcome back! You have visited before.";
} else {
echo "Welcome! This is your first visit.";
}
?>
Sample Output:
Welcome! This is your first visit.
Flowchart:

For more Practice: Solve these Related Problems:
- Write a PHP script to detect if a cookie named "visited" is set, and display a personalized welcome message if true.
- Write a PHP function that checks the existence of "visited" and sets it if not present, then outputs a different message based on its state.
- Write a PHP program to conditionally display a welcome back message if the "visited" cookie is found, otherwise greet a new visitor.
- Write a PHP script to check for the "visited" cookie, update its expiration, and display a status message accordingly.
PHP Code Editor:
Contribute your code and comments through Disqus.
Previous: Set a secure PHP cookie for an encrypted connection.
Next: PHP script to store user preferences in a session variable.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.