MySQL Forums
Forum List  »  PHP

Re: Clicking on a button for limited times
Posted by: Jay Alverson
Date: July 28, 2009 12:52PM

Sorry didn't have time to download yours and debug, but this works on my
PC and is a single page.

I placed the CREATE TABLE and DESC TABLE information in the PHP code so
it's all in one place...

There's no error checking on the INSERT query.

I used 5 clicks in 10 seconds, so you'll have to change it to make it
work for 24-hour period, which should be easy.


Save this script as Multi-Click.php

<?php

// ---------------------------------------------------------------------------------------------
/*
	When you test this, click the button at least five times in a row.
	
	When the H2 message displays, count to 10 then hit REFRESH on your browser...
	
	By Jay Alverson, 7-28-2009

*/
// ---------------------------------------------------------------------------------------------

	$click_max = 5;  // how many clicks per 10 seconds...
	
	$bc = $_REQUEST['buttonclick'];  // look for this being defined from the FORM submission...
	echo isset($bc) . "<BR>";

	$dt = date("Y:m:d H:i:s");	// format the current date/time for mysql...
	
	$this_IP = $_SERVER['REMOTE_ADDR' ];						// get the IP address of the client...
	echo "IP Address: " . $this_IP . " at " . $dt . "<br>";
	
	// ---------------------------------------------------------------------------------------------
	
		// here's what our table looks like in the mysql database...
	
	// mysql> use test;
	// Database changed
	// mysql> create table ip_table (
	    // -> ip varchar(20),
	    // -> clicktime timestamp);
	// Query OK, 0 rows affected (0.09 sec)

	// mysql> desc ip_table;
	// +-----------+-------------+------+-----+-------------------+-------+
	// | Field     | Type        | Null | Key | Default           | Extra |
	// +-----------+-------------+------+-----+-------------------+-------+
	// | ip        | varchar(20) | YES  |     | NULL              |       |
	// | clicktime | timestamp   | NO   |     | CURRENT_TIMESTAMP |       |
	// +-----------+-------------+------+-----+-------------------+-------+
	// 2 rows in set (0.02 sec)
	
	// ---------------------------------------------------------------------------------------------
	
	$mysqli = new mysqli('localhost', 'root', 'mysql', 'test');		// connect to mysql...


	if (mysqli_connect_error()) {
	    die('Connect Error (' . mysqli_connect_errno() . ') '
	            . mysqli_connect_error());
	}

	echo 'Success... ' . $mysqli->host_info . "<br>";		// show success on connect...
	
	//	 prepare a query to get a count of the number of entries that are less than 10 seconds...
	$sqlstr = "select count(*) as ipc from ip_table where ip = '$this_IP' and time_to_sec(timediff('$dt', clicktime)) < 10";
	echo $sqlstr . "<br>";		// show the query string for debugging...

	//  show how many are less than 10 seconds...
	if ($result = $mysqli->query($sqlstr)) {
    printf("Select returned %d rows.<br>", $result->num_rows);
		while ($row = $result->fetch_row()) {
			$current_count = $row[0];
			printf("Count is currently... %d<br>", $current_count);
		}
    /* free result set */
    $result->close();
	}
	
	//	show all the rows in the table and how many seconds from the clicktime they are
	//	only for debugging...
	//
	// ---------------------------------------------------------------------------------------------
	// $sqlstr = "select time_to_sec(timediff('$dt', clicktime)) from ip_table";
	// echo $sqlstr . "<br>";

	// if ($result = $mysqli->query($sqlstr)) {
    // printf("Select returned %d rows.<br>", $result->num_rows);
		// while ($row = $result->fetch_row()) {
			// $current_count = $row[0];
			// printf("This Difference is... %d<br>", $current_count);
		// }
    // /* free result set */
    // $result->close();
	// }	
	// ---------------------------------------------------------------------------------------------
	
	//	now compare the current count vs the click maximum (set arbitrarily to 5)
	if ($current_count < $click_max) {
		// 	on the first display of the page the $bc variable will be "" so you don't insert
		//	otherwise if it has a value, that means the user clicked the button, so we insert
		//	the ip and datetime string...
		if ($bc <> "") {
			$sqlstr = "insert into ip_table values ('$this_IP', '$dt')";
			$mysqli->query($sqlstr);
			//	show for debugging...
			echo "Button Pushed Insert ip/datetime into the table...<br>";
		}
		// since the current count < click maximum, show the user the button so they can click away...
		//	the form's action calls this very PHP script over and over...
		echo "<p><form action='Multi-Click.php' method='post'><input name='buttonclick' type='submit' value='Click Me' /></form></p>";
	}
	else {
	//	they've clicked too many times in the last 10 seconds, so show them this message.
	//	they can wait a few seconds and then hit refresh and it'll start all over...
		echo "<h2>Sorry you've clicked too many times in the last 10 seconds, please wait...</h2><br>";
	}
	
	//	close the connection to the database...
	$mysqli->close();

?>

>

Thanks, Jay



Edited 3 time(s). Last edit at 07/28/2009 03:45PM by Jay Alverson.

Options: ReplyQuote


Subject
Written By
Posted
Re: Clicking on a button for limited times
July 28, 2009 12:52PM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.