Jump to content

Working with cookies


global-hans

Recommended Posts

Dear people,

I just started learning and playing with PHP

For my new website I want to show a message that says "welcome some text". The little script puts up a cookie and if people visit my site for there second time I want it to show something like "back again"

I use the following code to set the cookie;

[code]<?php
$Month = 2592000 + time();
//this adds 30 days to the current time
setcookie(firstvisit, date("F jS - g:i a"), $Month);
?>[/code]

And I use the following to check if there is a cookie and if yes to print the text I want:

[code]<?php
if(isset($_COOKIE['firstvisit']))
{
$last = $_COOKIE['firstvisit'];
echo "Welcome back! <br> You last visited on ". $last;
}
else
{
echo "Welcome to our site!";
}
?>[/code]

This works fine and it really works like I wanted it to work but that's still not enough for me.

At this moment when people refresh the page it shows the second message and that really is something I hate.

I have searched every website but can't find something that can help me out.

Is there a way to tell the script that if the cookie is made today it has to show the first "welcome" message.

Or maybe set a second cookie that checks if the cookie that's made is of today and prints the first message.

Is there anyone who can point me in the right direction?

Thank you all in forward :)

Greetings from Hans
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.