View Full Version : simple cookies question
Hi!
i have this search script, i want to give users permission to do 5 searchs per day. after that they will get a message of: Only 5 searchs per day. Please come back tommorow
<?php
//i opened a connection to the DB
$link = mysql_connect('localhost', 'USER-NAME') or die ('Could not connect to MySQL');
mysql_select_db('MY-DB' , $link) or die('Could not open db');
//basic search
if ($_POST['SendButton'] == 'Search'){
if (ereg('[0-9]{1,20}', $_POST['zipcode'])){
$selection = mysql_query("
SELECT
signup.email,
signup.uname
FROM
signup
LEFT JOIN
authuser
ON
signup.uname = authuser.uname
WHERE
zipcode=" . addslashes(trim($_POST['zipcode'])) ."
AND
authuser.status = 'active'") or die ('Queryproblem');
if (mysql_num_rows($selection)>= 1){
echo '<ul>';
while ($row = mysql_fetch_assoc($selection) ) {
echo '<li>' . $row['email'] . '</li>';
}
echo '</ul>';
}else{
echo 'No emails for this zipcode.';
}
}else{
echo 'Invalid value. Zipcode needs to be between 1 and 20 digits.';
}
}else{
echo 'Invalid pagerequest.';
}
mysql_close();
?>
thanks
DA Master
08-25-2004, 08:15 AM
Please post your PHP code in PHP tags like this (easier to read)...
<?php
//i opened a connection to the DB
$link = mysql_connect('localhost', 'USER-NAME') or die ('Could not connect to MySQL');
mysql_select_db('MY-DB' , $link) or die('Could not open db');
//basic search
if ($_POST['SendButton'] == 'Search'){
if (ereg('[0-9]{1,20}', $_POST['zipcode'])){
$selection = mysql_query("
SELECT
signup.email,
signup.uname
FROM
signup
LEFT JOIN
authuser
ON
signup.uname = authuser.uname
WHERE
zipcode=" . addslashes(trim($_POST['zipcode'])) ."
AND
authuser.status = 'active'") or die ('Queryproblem');
if (mysql_num_rows($selection)>= 1){
echo '<ul>';
while ($row = mysql_fetch_assoc($selection) ) {
echo '<li>' . $row['email'] . '</li>';
}
echo '</ul>';
}else{
echo 'No emails for this zipcode.';
}
}else{
echo 'Invalid value. Zipcode needs to be between 1 and 20 digits.';
}
}else{
echo 'Invalid pagerequest.';
}
mysql_close();
?>
Back to your question. After they search have a variable e.g. $searches and use this...
//increment search
$searches++;
//check $searches value
if($searches > 5)
{
echo "Come back tommorow";
}
how to integrate it to the existing code? where should it go?
thanks
DA Master
08-25-2004, 08:27 AM
Actually, thats wrong, I would set a cookie like this...
<?php
//i opened a connection to the DB
$link = mysql_connect('localhost', 'USER-NAME') or die ('Could not connect to MySQL');
mysql_select_db('MY-DB' , $link) or die('Could not open db');
//set cookie
$time = 3600; //cookie time in seconds 3600 = 1hr
setcookie("searches", "0", time()+$time);
//basic search
if($_COOKIE['searches'] > 5)
{
echo "To many searches";
} else {
if ($_POST['SendButton'] == 'Search'){
if (ereg('[0-9]{1,20}', $_POST['zipcode'])){
$selection = mysql_query("
SELECT
signup.email,
signup.uname
FROM
signup
LEFT JOIN
authuser
ON
signup.uname = authuser.uname
WHERE
zipcode=" . addslashes(trim($_POST['zipcode'])) ."
AND
authuser.status = 'active'") or die ('Queryproblem');
if (mysql_num_rows($selection)>= 1){
echo '<ul>';
while ($row = mysql_fetch_assoc($selection) ) {
echo '<li>' . $row['email'] . '</li>';
}
echo '</ul>';
}else{
echo 'No emails for this zipcode.';
}
}else{
echo 'Invalid value. Zipcode needs to be between 1 and 20 digits.';
}
}else{
echo 'Invalid pagerequest.';
}
}
//increment search cookie
$_COOKIE['searches']++;
mysql_close();
?>
DA Master
08-25-2004, 08:33 AM
Nearly forgot, you will want to check if the cookie is still set, or it will overwrite it.
So replace...
$time = 3600; //cookie time in seconds 3600 = 1hr
setcookie("searches", "0", time()+$time);
With...
if(!isset($_COOKIE['searches']))
{
$time = 3600; //cookie time in seconds 3600 = 1hr
setcookie("searches", "0", time()+$time);
}
3600 = 1hour?
so they could do 5 searchs every hour?
DA Master
08-25-2004, 08:42 AM
Yeah, you set that to what you want.
scoutt
08-25-2004, 09:10 AM
cookies or sessions will not be any good. all you have to do is set them and then the user can close the browser and delete all cookies and do it again.
the only sure fire way is to have it set a row in mysql and then update that row for the day,count,IP, and whatever else. this way the user can't change anything.
DA Master
08-25-2004, 09:16 AM
True, I thought of that way, but he wanted a cookie solution.
well, of course a mysql solution is better...
the only question is - how complicated it is? :)
scoutt
08-25-2004, 10:47 AM
well pretty easy really. if htey aren't in there insert the row, then on any other search you update the count for tha tparticular row. so it is just a
1) check for user in db (probably use the users IP) - if found more than 3 times disallow the search.
2) if none found insert the user info with search column as 1
3) if found update search column to 2 or whatever.
simple small scritp actually.
yes,
but it's pretty easy to cheat it too...
people will register under a new username & do a search,
register under a new username & do a search,
register,register under a new username & do a search,
register, etc...
& besides...
i don't want that only registered users will have permission to search. so what then? record their ip's?
even that you can bypass... you can reconnect under a new ip. or just surf from another pc... ;)
DA Master
08-25-2004, 03:39 PM
Yeah but that takes time...
scoutt
08-25-2004, 07:12 PM
Originally posted by cooc
yes,
but it's pretty easy to cheat it too...
people will register under a new username & do a search,
register under a new username & do a search,
register,register under a new username & do a search,
register, etc...
& besides...
i don't want that only registered users will have permission to search. so what then? record their ip's?
even that you can bypass... you can reconnect under a new ip. or just surf from another pc... ;)
then why have the limit??? and the only way to do it with a new IP is if they have dialup. doing it the mysql way will be a lot better than cookies or sessions. and it is not as easy.
how important is it to have the limit anyway? what is the purpose?
purpose?
i don't have a lot of bandwidth :D
Originally posted by DA Master
Nearly forgot, you will want to check if the cookie is still set, or it will overwrite it.
So replace...
$time = 3600; //cookie time in seconds 3600 = 1hr
setcookie("searches", "0", time()+$time);
With...
if(!isset($_COOKIE['searches']))
{
$time = 3600; //cookie time in seconds 3600 = 1hr
setcookie("searches", "0", time()+$time);
}
i just got the time to check it...
the cookies doesn't work :(
i can search endlessly...
scoutt
08-27-2004, 10:34 AM
ummm you have to set the cookie to 1 at least
setcookie("searches", "1", time()+$time);
DA Master
08-27-2004, 10:35 AM
What? Did I put 0? I must have done, oh well, yeah that needs to be 1 sorry :D
hmmm, still doesn't work...
i have html page with a form, people enter a search string their, & the form is being sent to this php page... i tested it more than 20 times... the cookies doesn't work...
scoutt
08-27-2004, 11:22 AM
do this
setcookie("searches", "1", time()+$time, "/");
what does it do?
but... still, still doesn't work =:O
scoutt
08-27-2004, 11:33 AM
it sets the domain. what is your code?
<?php
//i opened a connection to the DB
$link = mysql_connect('localhost', 'MY-USER') or die ('Could not connect to MySQL');
mysql_select_db('MY-DB' , $link) or die('Could not open db');
//set cookie
if(!isset($_COOKIE['searches']))
{
$time = 3600; //cookie time in seconds 3600 = 1hr
setcookie("searches", "1", time()+$time, "/");
}
//basic search
if($_COOKIE['searches'] > 5)
{
echo "Sorry. only 5 searchs per 1 hour allowed. Try again later. thanks";
} else {
if ($_POST['SendButton'] == 'Search'){
if (ereg('[0-9]{1,20}', $_POST['zipcode'])){
$selection = mysql_query("
SELECT
signup.email,
signup.uname
FROM
signup
LEFT JOIN
authuser
ON
signup.uname = authuser.uname
WHERE
zipcode=" . addslashes(trim($_POST['zipcode'])) ."
AND
authuser.status = 'active'") or die ('Queryproblem');
if (mysql_num_rows($selection)>= 1){
echo '<ul>';
while ($row = mysql_fetch_assoc($selection) ) {
echo '<li>' . $row['email'] . '</li>';
}
echo '</ul>';
}else{
echo 'No emails for this zipcode.';
}
}else{
echo 'Invalid value. Zipcode needs to be between 1 and 20 digits.';
}
}else{
echo 'Invalid pagerequest.';
}
}
//increment search cookie
$_COOKIE['searches']++;
mysql_close();
?>
the domain?
i have it on a free host, does it matter?
scoutt
08-27-2004, 12:01 PM
because you never update the cookie. if you want the cookie to be incremented you have to update it.
if(!isset($_COOKIE['searches']))
{
$time = 3600; //cookie time in seconds 3600 = 1hr
setcookie("searches", "1", time()+$time, "/");
} else {
$inc = $_COOKIE['searches']++;
setcookie("searches", $inc, time()+$time, "/");
}
DA Master
08-27-2004, 01:32 PM
Oops my bad, that was my fault cooc, I missed it out for some reason.
Originally posted by scoutt
because you never update the cookie. if you want the cookie to be incremented you have to update it.
if(!isset($_COOKIE['searches']))
{
$time = 3600; //cookie time in seconds 3600 = 1hr
setcookie("searches", "1", time()+$time, "/");
} else {
$inc = $_COOKIE['searches']++;
setcookie("searches", $inc, time()+$time, "/");
}
still no go... :confused:
strange...
scoutt
08-27-2004, 03:06 PM
does it set a cookie? open the cookie up and see if it has a value?
do this too
if(!isset($_COOKIE['searches']))
{
$time = 3600; //cookie time in seconds 3600 = 1hr
setcookie("searches", "1", time()+$time, "/");
} else {
$inc = $_COOKIE['searches']++;
setcookie("searches", $inc, time()+$time, "/");
}
echo "cookie value: ".$_COOKIE['searches'];
what does it say? you may have to refresh the page as well.
Originally posted by scoutt
does it set a cookie? open the cookie up and see if it has a value?
do this too
if(!isset($_COOKIE['searches']))
{
$time = 3600; //cookie time in seconds 3600 = 1hr
setcookie("searches", "1", time()+$time, "/");
} else {
$inc = $_COOKIE['searches']++;
setcookie("searches", $inc, time()+$time, "/");
}
echo "cookie value: ".$_COOKIE['searches'];
what does it say? you may have to refresh the page as well.
well,
i replaced the code with this new code,
& the cookie value is always 1 :rolleyes:
scoutt
08-27-2004, 03:41 PM
always? even after refreshing, use f5-shift
Originally posted by scoutt
always? even after refreshing, use f5-shift
yup... even after refreshing...
scoutt
08-27-2004, 03:49 PM
then you have issues with your temporary internet files. clear your cache and try it agian. or the cookie is not getting updated.
i did more than that, i tested in a whole new browser. in netscape.
at first the cookie value is 1, after that 2 after that NOTHING is shown. after that 2, after that NOTHING... etc...
scoutt
08-27-2004, 04:54 PM
try this
$time = 3600; //cookie time in seconds 3600 = 1hr
if(!isset($_COOKIE['searches']))
{
setcookie("searches", "1", time()+$time, "/");
} else {
$inc = $_COOKIE['searches']++;
setcookie("searches", $inc, time()+$time, "/");
}
echo "cookie value: ".$_COOKIE['searches'];
I forgot about the time. the second time the cookie gets updated the time is gone so it expires fast.
now it's showing 1, then 2, & then it's showing 2 forever...
scoutt
08-27-2004, 05:44 PM
the fact that it gets incremented and then stops means it is not refreshing/reloading the page (using cache) or you have something wrong with your computer. the code is correct so I am at a loss as to why it stops.
do you have a link?
vBulletin® v3.6.7, Copyright ©2000-2010, Jelsoft Enterprises Ltd.