w3resource

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:

Flowchart: PHP script to check cookie existence and display a message.

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.

Go to:


PREV : Set Secure Cookie Over HTTPS.
NEXT : Store Array in Session Variable.

PHP Code Editor:



Contribute your code and comments through Disqus.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.