Hey, I wasn't exactly sure whether to post this question here or or in the coding section but being that I am very newb to PHP I decided this was likely the best place. I have managed thus far through a lot of reading and studying source code and have done OK so far.
The issue I am having is trying to find a better way to handle updating the database when clicking a link to an external site. Currently and the only way "I" have been able to make this happen is with a JavaScript (function load_site($site)) but this is obviously blocked by most pop-up blockers and right fully so, I don't like annoying pop-ups anymore than anyone else and why Im trying to find a better way to handle this.
This is the code as I currently have it:
/****
**** Initialising the variables we'll need.
****/
/* Name used for the DB that will handle the voting system. */
$dbname = 'votes';
/* Defined below are the voting sites, with a short name (used for the database) and the url */
$urls = array (
1 => "http://www.link_to_site_1.com",
2 => "http://www.link_to_site_2.com",
3 => "http://www.link_to_site_3.com",
4 => "http://www.link_to_site_4.com"
);
/* Another array, of the same length, for the scripts used to display images (the link MUST point to "vote.php?action=<array index>" for the votes to be counted */
$votingscripts = array (
1 => '
<a href="vote.php?action=1">
<img src="img/site_1_pic.jpg" border="0" alt="site_1">
</a>
',
2 => '
<a href="vote.php?action=1">
<img src="img/site_2_pic.jpg" border="0" alt="site_2">
</a>
',
3 => '
<a href="vote.php?action=1">
<img src="img/site_3_pic.jpg" border="0" alt="site_3">
</a>
',
4 => '
<a href="vote.php?action=4">
<img src="img/site_4_pic.jpg" border="0" alt="site_4">
</a>
'
);
/* Just checking that we're not missing any url: */
$numberofsites = sizeof($urls);
if (sizeof($votingscripts) != $numberofsites) {
die(error("Error in the voting script, make sure you have the same amount of urls and scripts."));
}
/****
**** General functions
***/
/* Simple function to open the url (uses JavaScript). */
function load_site($site) {
global $output;
$output .= "<script language=\"JavaScript\">\n
open('$site');\n
</script>";
}
/* Checks out all the needed info from the votes DB. */
function get_db_info() {
global $acct_db, $dbname, $user_id, $sites, $numberofsites;
$sql = new SQL;
$link = $sql->connect($acct_db['addr'], $acct_db['user'], $acct_db['pass'], $dbname);
$query="SELECT votecount";
for ($index=1; $index <= $numberofsites; $index++) {
$query .= ", last_$index";
}
$query .= " FROM account where id='$user_id'";
$result = $sql->query($query);
$row = mysql_fetch_row($result);
$sql->close();
return $row;
}
/* Called when updating the DB with new info (not on the first vote though). */
function update_db_info() {
global $acct_db, $dbname, $last_votes, $user_id, $votes, $numberofsites;
$sql = new SQL;
$link = $sql->connect($acct_db['addr'], $acct_db['user'], $acct_db['pass'], $dbname);
$query = "UPDATE account SET votecount='$votes'";
for ($index=1; $index <= $numberofsites; $index++) {
$query .= ", last_$index='$last_votes[$index]'";
}
$query .= " WHERE id='$user_id'";
$sql->query($query);
$sql->close();
}
/****
**** Voting-specific functions
****/
/* Simple function to create a new entry for an account on the first vote, and initialize it. */
function first_vote() {
global $acct_db, $dbname, $user_id, $numberofsites;
$sql = new SQL;
$link = $sql->connect($acct_db['addr'], $acct_db['user'], $acct_db['pass'], $dbname);
$query = "INSERT INTO account VALUES ('$user_id'";
for ($index=1; $index <= ($numberofsites + 1); $index++) {
$query .= ", '0'";
}
$query .= ")";
$sql->query($query);
$sql->close();
}
/* Because having it say "you have points" or "you have 1 points" just won't do! */
function votestring($votes)
{
if (!$votes) {
$votestring="no points...";
}elseif ($votes == 1){
$votestring="one point.";
} else {
$votestring="$votes points.";
}
return $votestring;
}
/* Displays the time remaining in a readable fashion. */
function time_left($integer) {
$seconds = ( $integer % 60 );
$integer = (int) ($integer / 60);
$minutes = ( $integer % 60 );
$hours = (int) ($integer / 60);
$return = "$hours hours, $minutes minutes, $seconds seconds.";
return $return;
}
/* A voting banner was clicked: Checks if it has been long enough, and updates votes and loads the page if it has. */
function send_vote($sitenumber) {
global $timetogo, $user_id, $votes, $last_votes, $urls;
if (!$votes)
{
first_vote();
}
$now = date(c);
$timetogo = (12 * 60 * 60) - (strtotime("$now") - strtotime("$last_votes[$sitenumber]"));
if ($timetogo < 0) {
$votes++;
$last_votes[$sitenumber] = $now;
update_db_info();
load_site($urls[$sitenumber]);
}
return;
}
/****
**** Execution starts here.
****/
/* Loading up the necessary info from the database. */
$db_info = get_db_info();
$votes = $db_info[0];
for ($index = 1; $index <= $numberofsites; $index++) {
$last_votes[$index] = $db_info[$index];
}
/* Handling the button pushing action. "Action" is when voting ... explains itself.
Pretty simple all in all, just calls the associated functions. */
$action = (isset($_GET['action'])) ? $_GET['action'] : NULL;
if ($action) {
send_vote($action);
}
/****
**** We start displaying here!
****/
/* First field, to vote (and earn vote points). */
$output .= " <center>
<fieldset style=\"width: 650px;\">\n
<center><h1><br>You currently have ";
$output .= votestring($votes);
$output .= "</h1><br>
<h3>Earn more by voting every day :)<br><br></h3>
<legend>Please take a moment to vote today!</legend>";
for ($index=1; $index <= $numberofsites; $index++) {
$output .= $votingscripts[$index];
}
$output .= "<br>";
/* Warning if it hasn't been long enough since the previous vote on that site. */
if ($timetogo > 0) {
$output .= "<center><img src='img/warn_red.gif'";
$output .= "<br><br><h4>It hasn't been long enough since your last vote on that site!<br><br>You can vote again in: </h4><h2><br>";
$output .= time_left($timetogo);
$output .= "</h2></center>";
}
$output .= "</fieldset><br><br>";
require_once("footer.php");
?>
As I'm sure you can see the formatting is pretty dismal and I suppose that is just confirmation of my newbness. (is that a word?) If anyone can get me pointed in the right direction I would be extremely grateful. I've been reading and researching different possibilities but I guess Im still too new to even know what to look for. For some reason I am thinkng this could be done in a ( <form action= ) but Im not really sure.
Another even bigger issue than the pop-up is the fact that this is easily abusable. If you notice there is a limitation (once every 12 hours) which can be hacked simply by adding any keystrokes to the end of the address (.../vote.php?action=1) like so (.../vote.php?action=1xxx) doing so will continually update the database regardless of the $timetogo.
Again, thanks for any direction you can provide.