PDA

View Full Version : Resolved Clicking on a button for limited times



egturnkey
07-26-2009, 12:03 AM
Hello dear friends ,

Consider we have a button where you should click

Is there anyone can help me and give the best idea how to make it limited

i means after 20 clicking on that button , it goes de-active / or / image / or / text for example says ( no more clicking )


Yes it could be very complex idea cause it may needs to recall a command at cronjob ( if i'm not wrong )



So the full story :-

i want a button that valid to be clicked 20 times per day , after the clicking 20 times , it gives msg says for example " come back 2morrow "


thanks in advance and i know it maybe hard and may takes time
but i really with that help , will open a new area into my mind

thetestingsite
07-26-2009, 01:32 AM
is it 20 times per person per day or just 20 times per day? Either way, you could either write to a file or db to keep track of how many times it has been clicked either by ip address or total clicks.

egturnkey
07-26-2009, 09:10 AM
is it 20 times per person per day or just 20 times per day? Either way, you could either write to a file or db to keep track of how many times it has been clicked either by ip address or total clicks.

It is 20 times per person per day

just like some downloading websites, when you downloads many files after for example 20 downloads it gives you msg " no more download for you today "

so it is yes as you've said 20 times per person per day


:( i've tried to find any script has such function so i can isolate the code but i didn't find any , so i hope if someone can help me about that , making it as simple as it could

thanks in advance

egturnkey
07-26-2009, 09:17 PM
but it didn't works cause i've still some error and really not able to understand where to put thay query



$time = time();
mysql_query("INSERT INTO `ip_table` (`ip`,`timestamp`) VALUES ('".mysql_real_escape_string($_SERVER['REMOTE_ADDR'])."',$time)");


so can you please check out the example and fix it

JShor
07-28-2009, 02:20 PM
In this case, your $time var is a timestamp [including mins, secs, etc]. Be less specific w/your time, so that it only includes the DATE and not anything else.

This ought to fix it for ya:


$time = date("m/d/y");

mysql_query("INSERT INTO `ip_table` (`ip`,`timestamp`) VALUES ('".mysql_real_escape_string($_SERVER['REMOTE_ADDR'])."',$time)");



Now, the next part would be to edit whatever is looking up the matching values from the db to associate w/a certain date:



$date = date("m/d/y");

$qq = mysql_query("SELECT * FROM ip_table WHERE ip='$_SERVER[REMOTE_ADDR]' AND date='$date'") or die(mysql_error());

$num = mysql_num_rows( $qq ) or die(mysql_error());

if($num > 20) {
echo "You can't click that. You've already clicked it 20 times.";
} else {
echo "You can click that.";
}


HTH :)

egturnkey
07-28-2009, 11:59 PM
Have fixed the example as follow :-

in the database your storing date as a varchar, change the type to be date. Mysql stores dates in the Y-m-d format so change $date = date ('Y-m-d'). You select query is also wrong to, it says date = when your insert (and db structure) is timestamp



$today_date = date("Y-m-d");
mysql_query("INSERT INTO `ip_table` (`ip`,`timestamp`) VALUES ('".$_SERVER['REMOTE_ADDR']."','".$today_date."')");

$qq = mysql_query("SELECT * FROM ip_table WHERE ip='".$_SERVER['REMOTE_ADDR']."' AND timestamp='".$today_date."'") or die(mysql_error());
$num = mysql_num_rows( $qq ) or die(mysql_error());

if($num > 20) {
echo "You can't click that. You've already clicked it 20 times.";
} else {
echo "You can click that.";
}




Thanks to JShor from dynamicdrive.com forum ( idea of the code )
Thanks to Shof515 from devshed.com forum ( fixed the code )


Now we still have simple littel problem , if someone can help me


it will store the IP/DATA when someone visit the page Not upon clicking on a button


- can you please show me simple example where clicking on the button, will submit to the to the insert query to stroe IP and Data

i mean , it count the click only on clicking a button not visiting the page .

thanks in advance

JShor
07-29-2009, 08:05 PM
OK, for this, the best solution would be to use ajax [or an iframe, if you're desperate].

First page, page.php, where the object to be click will be:


<html>
<head>
var http_request = false;
function makeRequest(url, parameters) {
http_request = false;
if (window.XMLHttpRequest) { // Mozilla, Safari,...
http_request = new XMLHttpRequest();
if (http_request.overrideMimeType) {
// set type accordingly to anticipated content type
//http_request.overrideMimeType('text/xml');
http_request.overrideMimeType('text/html');
}
} else if (window.ActiveXObject) { // IE
try {
http_request = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
http_request = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e) {}
}
}
if (!http_request) {
alert('Cannot create XMLHTTP instance');
return false;
}
http_request.onreadystatechange = alertContents;
http_request.open('POST', url + parameters, true);
http_request.send(null);
}
</script>
</head>
<body>
<?php $ip = $_SERVER[REMOTE_ADDR]; ?>
Button:<br />
<input type="button" value="Click here" onClick="makeRequest('get.php', '?IP=<?=$ip ?>');">
Link: <br />
<a href="javascript:makeRequest('get.php', '?IP=<?=$ip ?>');">Click here</a>


Next, use the page to process the req [req.php]::


<?php

$ip = $_GET['ip'];

$today_date = date("Y-m-d");

mysql_query("INSERT INTO `ip_table` (`ip`,`timestamp`) VALUES ('".$ip."','".$today_date."')");


?>


It's pretty simple and straight forward, make changes at will.

HTH :)

egturnkey
07-30-2009, 01:56 AM
OK, for this, the best solution would be to use ajax [or an iframe, if you're desperate].
:)

thanks so much , however there are an error in javascript i guess

here is the error :-

http://egcss.com/error.jpg


here is the full example :-

http://www.egcss.com/exp3.ra (http://www.egcss.com/exp3.rar)


it is as following

index.php ( with the js function makeRequest ) click on it will store IP and call

get.php (req "req.php" which store the IP + it has the real download link )

config.php --- dbconnection
dabase.sql --- db


so can you please check it out for that error

thanks so much , i feel we almost done it

your idea is very very great and simple , i did like it so much

JShor
07-30-2009, 07:14 PM
Alright, I made some changes to the script [and tested it on my server,works perfect]. The script will call ajax and record the number of times that the user clicked the object in the db, and after 20 clicks [or however many you set to] the object becomes disabled,a nd no longer clickable.

Here's teh pages:

index.php [script needs editing]


<?php

include('config.php'); // connect to db

$ip = $_SERVER[REMOTE_ADDR];
$date = date("Y-m-d"); // mysql db format as requested.

$qq = mysql_query("SELECT * FROM ip_table WHERE ip='$ip' AND timestamp='$date'") or die(mysql_error()); //select query

if(mysql_num_rows( $qq ) > 0) {
$int = mysql_num_rows( $qq ) or die(mysql_error()); // number of times clicked by user today
} else {
$int = 0;
}

?>
<html>
<head>
<script type="text/javascript">
var myLinkURL = "http://mysite.com/"; // change this to what you want the link to say
var timesToClick = 20; //times you wanna click the link before obj becomes disabled.
// NO NEED TO EDIT BELOW THIS //

var exp = parseInt(<?php echo $int; ?>); // number of times clicked [when page loaded] + 1more click..
var http_request = false;
function makeRequest(url, parameters) {
http_request = false;
if (window.XMLHttpRequest) { // Mozilla, Safari,...
http_request = new XMLHttpRequest();
if (http_request.overrideMimeType) {
// set type accordingly to anticipated content type
//http_request.overrideMimeType('text/xml');
http_request.overrideMimeType('text/html');
}
} else if (window.ActiveXObject) { // IE
try {
http_request = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
http_request = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e) {}
}
}
if (!http_request) {
alert('Cannot create XMLHTTP instance');
return false;
}
http_request.open('POST', url + parameters, true);
http_request.send(null);
determineClickedTimes(); //function to call clicked times constraint

}


function determineClickedTimes() {
exp = parseInt(exp + 1);
if(exp > parseInt(timesToClick - 1)) { //if obj is clicked {x} times
document.getElementById('clicker').disabled = "disabled"; //disable the object
} else {
window.open(myLinkURL);
}
}
</script>
</head>
<body>
<?php $ip = $_SERVER[REMOTE_ADDR]; ?>
<br /> <br /> Link:
<a href="javascript:makeRequest('req.php', '?IP=<?php echo $ip; ?>');" id="clicker">Click here</a>
</body>
</html>


get.php, what ajax calls:


<?php

include('config.php');

$ip = $_GET['IP'];

$today_date = date("Y-m-d");

mysql_query("INSERT INTO `ip_table` (`ip`,`timestamp`) VALUES ('".$ip."','".$today_date."')") or die(mysql_error());

?>


HTH :)

egturnkey
07-30-2009, 10:39 PM
Alright, I made some changes to the script [and tested it on my server,works perfect]

thanks so much JShor
that idea is also much more better , i've test it and works perfect

However

:o it may works for statics links but not dynamics one , for example
at the following part



var myLinkURL = "http://mysite.com/"; // change the link


how can i intert a dynmic link written in php as following, depends in other defines



<a href='".FILE_DOWNLOAD."?productID=".$productrow['productID']."'>



that was the original
( if logged then download , if not then " Members Only " , if not both (lol) then the product not avl


i've replaced the 1st part ( if logged ) with the code that limited the number of downloads which you've given to me ( all thanks to you ) but as i've told you , the problem will be in the link itself



<?
if ($productrow['status']) {
if ($productrow['type'] == 0 && ($isMember || $isAdmin)) {


echo "<a href='#' onclick='append_cookie(\"cartcontent\", \"".$productrow['productID']."\", \"\")'>";
echo "<img src='$addtocart_button' border=0>";
echo "</a>";
echo "<a href='".FILE_DOWNLOAD."?productID=".$productrow['productID']."'>";
echo "<img src='$download_button' border=0>";
echo "</a>";
}

else if ($productrow['type'] == 0 && (!$isMember && !$isAdmin)) {
echo "<b>Member Download Only</b>";
}


} else { // product n/a for download or purchase
echo "<img src='$na_button' border=0>";
}
?>




i know i've taken a lot of your time ,
but it really helps me to release a simple php script and free to all , act as free/paid memberships for templates/scripts/downloadble downloads
like dream templates and templates boxed art ..ect ( lol ) but much more simple

nighthawks
07-31-2009, 10:30 AM
is it 20 times per person per day or just 20 times per day?

egturnkey
07-31-2009, 11:44 AM
is it 20 times per person per day or just 20 times per day?

all thanks to Jshor , yes it is working 20 click ( you can edit it to any number ) per IP per day

each IP must wait for next day in order to click more 20 clicks and so on

we just waitting for Jshor to give us a comment how to make the link dynamic as i've said, however i'm not sure how to run a php command inside a javascript .

JShor
07-31-2009, 02:24 PM
Alright, then, but I'm not clear on whatyou mean by "it doesn't work". Is it giving you a Jscript error, not opening the window, or the script fails altogether?

Depending on the problem will determine the solution for it.

If it's a Jscript error, magic quotes should fix it [that's waht I think is most likely].

egturnkey
07-31-2009, 02:46 PM
Alright, then, but I'm not clear on whatyou mean by "it doesn't work". Is it giving you a Jscript error, not opening the window, or the script fails altogether?
Depending on the problem will determine the solution for it.
If it's a Jscript error, magic quotes should fix it [that's waht I think is most likely].

It is working correct , but as you can see here
at the index.php



var myLinkURL = "http://mysite.com/"; // change the link


where should i put the link of the file which will be downloading upon clicking on the link which we limited its number of clicks per ip per day

as it clear , it is a javascript and hence i can only add a direct link
so what if i want to add a dynmic link ( php ) like the following



".FILE_DOWNLOAD."?productID=".$productrow['productID']."


which will call the file that will be downloaded


the was the original code
( if logged then download , if not then " Members Only " , if not both then the product not avl )


i've replaced the 1st part ( if logged ) with the code that limited the number of downloads which you've given to me ( all thanks to you ) but as i've told you , the problem will be in the link itself



<?
if ($productrow['status']) {
if ($productrow['type'] == 0 && ($isMember || $isAdmin)) {


echo "<a href='#' onclick='append_cookie(\"cartcontent\", \"".$productrow['productID']."\", \"\")'>";
echo "<img src='$addtocart_button' border=0>";
echo "</a>";
echo "<a href='".FILE_DOWNLOAD."?productID=".$productrow['productID']."'>";
echo "<img src='$download_button' border=0>";
echo "</a>";
}

else if ($productrow['type'] == 0 && (!$isMember && !$isAdmin)) {
echo "<b>Member Download Only</b>";
}


} else { // product n/a for download or purchase
echo "<img src='$na_button' border=0>";
}
?>



so how can we change the fixed link in javascript code




var myLinkURL = "http://mysite.com/"; // change the link


to that one in blue color

thanks

egturnkey
07-31-2009, 03:05 PM
thanks so much,

it has been solved and i've edit the following



var myLinkURL = "http://mysite.com/"; // change the link


to the following



var myLinkURL = "<?php echo "".FILE_DOWNLOAD."?productID=".$productrow['productID'].""; ?>"; // change the link



Now it works perfect


thanks so much jshor for your help, it is done :)

JShor
07-31-2009, 03:46 PM
Alright, I gotcha. The window.open function can be disabled if the if..else statement determines that the user doesn't have access to the link. This code should do the trick:

[Revised] index.php:


<?php

include('config.php'); // connect to db

$ip = $_SERVER[REMOTE_ADDR];
$date = date("Y-m-d"); // mysql db format as requested.

$qq = mysql_query("SELECT * FROM ip_table WHERE ip='$ip' AND timestamp='$date'") or die(mysql_error()); //select query

if(mysql_num_rows( $qq ) > 0) {
$int = mysql_num_rows( $qq ) or die(mysql_error()); // number of times clicked by user today
} else {
$int = 0;
}

if ($productrow['status']) {
if ($productrow['type'] == 0 && ($isMember || $isAdmin)) {

$dlLink = FILE_DOWNLOAD."?productID=".$productrow['productID'];

}
}

?>
<html>
<head>
<script type="text/javascript">

var myLinkURL = "<?php echo $dlLink; ?>";
var timesToClick = 20; //times you wanna click the link before obj becomes disabled.

// NO NEED TO EDIT BELOW THIS //

var exp = parseInt(<?php echo $int; ?>); // number of times clicked [when page loaded] + 1more click..
var http_request = false;
function makeRequest(url, parameters) {
http_request = false;
if (window.XMLHttpRequest) { // Mozilla, Safari,...
http_request = new XMLHttpRequest();
if (http_request.overrideMimeType) {
// set type accordingly to anticipated content type
//http_request.overrideMimeType('text/xml');
http_request.overrideMimeType('text/html');
}
} else if (window.ActiveXObject) { // IE
try {
http_request = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
http_request = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e) {}
}
}
if (!http_request) {
alert('Cannot create XMLHTTP instance');
return false;
}
http_request.open('POST', url + parameters, true);
http_request.send(null);
determineClickedTimes(); //function to call clicked times constraint

}


function determineClickedTimes() {
exp = parseInt(exp + 1);
if(exp > parseInt(timesToClick - 1)) { //if obj is clicked {x} times
document.getElementById('aClick').disabled = "disabled"; //disable the link
document.getElementById('imgClick').disabled = "disabled"; //disable the image
} else {

if(myLinkURL == '') {
location.href = myLinkURL;
} else {
return false;
}
}
}
</script>
</head>
<body>
<?php $ip = $_SERVER[REMOTE_ADDR]; ?>
<br /> <br /> Link:
<a href="javascript:makeRequest('req.php', '?IP=<?php echo $ip; ?>');" id="clicker">Click here</a>

<?
if ($productrow['status']) {
if ($productrow['type'] == 0 && ($isMember || $isAdmin)) {


echo "<a href='#' onclick='append_cookie(\"cartcontent\", \"".$productrow['productID']."\", \"\")'>";
echo "<img src='$addtocart_button' border=0>";
echo "</a>";
echo "<a href=\"javascript:makeRequest('req.php', '?IP=<?php echo $ip; ?>');\" id=\"aClick\">";
echo "<img src='$download_button' border=0 id=imgClick>";
echo "</a>";
}

else if ($productrow['type'] == 0 && (!$isMember && !$isAdmin)) {
echo "<b>Member Download Only</b>";
}


} else { // product n/a for download or purchase
echo "<img src='$na_button' border=0>";
}

?>
</body>
</html>


HTH :)