w3resource

PHP script to check cookie existence and display a message

PHP Cookies and Sessions: Exercise-8 with Solution

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.

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.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://www.w3resource.com/php-exercises/cookies-sessions/php-cookies-sessions-exercise-8.php