Jump to content

Cookie counter


Runilo

Recommended Posts

Hello.

On my website i have this sms application which is beeing heavily used, so therefore i would like for the number of sent messages for each user to be 5 per day, and no more.

When a user sends an sms, he is redirected to a page that confirms that the message has been sent. That page is called sms-ok.php. On this page i would like to have a cookie based counter. I have found this little script that does the counting:

<html>
<head>
<title>
PHP - Cookie Example 1
</title>
</head>
<body>
<h1>Cookie Example 1</h1>
<font size=+2 face = verdana>
<?
$visitcount = $HTTP_COOKIE_VARS["visits"];
if( $visitcount == "") $visitcount = 0;
else $visitcount++;
setcookie("visits",$visitcount);
print "This is visit number " . $visitcount;
?>
</font>
</body>
</html>


Now, does anyone know how i can set the expiration time of this cookie to 24 hours?

And how would i be able to display a message on another page, when the counter has reached 5?

Any help is much appresiated
Link to comment
Share on other sites

to set the expire time of the cookie to 24 hours just insert time()+86400 (number of seconds in 24 hours) as your expire time.

but if you want to limit the number of messages sent per day you shouldn't use cookies. all a user has to do is edit the cookie to show a different number (which is easy). use a database to keep track of messages sent per day.

but if you insist on doing it this way, to display a message when the limit is reached simply do this:

if($_COOKIE['visits'] >=5){

//put your error message here

}
Link to comment
Share on other sites

Thank you for the reply

Well, using cookies just seems as the easiest way to go about it.

I would like to ask a few more questions if that's alright.

If i do not set an expiration time on the cookie, how long will it last??

The thing you mentioned about the database seems like a lot of work. I hope i'm not being too demanding, but would you care do explain a little bit on how do accomplish this?
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.