Do you like my tutorials?

Then consider supporting me on Ko-fi

Talking about Php, Tutorials and WordPress.

In part 1 we saw how to configure a cross-domain policy file, calling the javascript and send the results to a webpage.

In part 2 we saw how to prevent cheating.

In this 3rd part we’ll make something useful with it. I am going to add to my Triqui MochiAds Arcade theme for WordPress a WordPress widget showing the latest scores submitted from my portal, triqui.com, but once you understand how to do it, you can easily change the script to make it fit your needs.

The first thing to do is creating a new table in your WordPress database. At this time, I only want to save player name, score and obviously the game unique id.

So my MySQL query is:

CREATE TABLE IF NOT EXISTS `wp_mochi_scores` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `when` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `gameid` text COLLATE utf8_unicode_ci NOT NULL,
  `name` text COLLATE utf8_unicode_ci NOT NULL,
  `score` text COLLATE utf8_unicode_ci NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

as you can see, I added a primary auto increment key and a timestamp field, to determine when the score has been saved.

Then I need to change postscores.php this way:

 $varvalue){
     // if the key is not "signature" then append key and url encoded values to the string
     if($varname!="signature"){
          $string.=$varname."=".rawurlencode($varvalue)."&";
     }
}

// removing the last character (a "&"")
$string = substr($string,0,strlen($string)-1);

// appeding the secret key to the string
$string.= $key;

// comparing the md5 encryption of the string with the "signature" variable
if(md5($string)==$_POST[signature]){
     // requiring the relative path to your wp-config.php file
     // according to your wp installation, this path may change
     // but in most cases it will work this way
     require("../../../wp-config.php");
     // connecting to wp database
     $link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
     // selecting the proper database
     $db = mysql_select_db($link,DB_NAME);
     // inserting the game tag, the score and the player name
     $result = mysql_query("insert into ".$table_prefix."mochi_scores (gameid,name,score) values('$_POST[gameID]','$_POST[name]','$_POST[score]')");
}

?>

Now I have a script that silently populates the mochi_scores table with the latest scores.

It’s time to show them in a widget.

If you don’t know what is a widget or how to create and activate it, follow How to create a WordPress Widget.

I created a file called mochi_lastscores.php with this content:

LATEST SCORES

"; // selecting the latest 10 scores $query = "select * from ".$wpdb->prefix."mochi_scores order by id desc limit 0,10"; // saving scores inside an array called "scores" $scores = $wpdb->get_results($query,ARRAY_A); if($scores){ echo "
    "; // scanning all scores foreach($scores as $score){ // looking for the entry in my mochi game table with the same game tag as the one I saved $query = "select * from ".$wpdb->prefix."mochi where game_tag = \"$score[gameid]\""; // saving the entry in an array called "game" $game = $wpdb->get_results($query,ARRAY_A); // if the entry has a blog_id... if($game[0][blog_id]){ // selecting the post in the wp post table with the same id as the game found in the mochi games table $query = "select * from ".$wpdb->prefix."posts where ID = ".$game[0][blog_id]; // saving the entry in a variable called "post" $post = $wpdb->get_results($query,ARRAY_A); // printing the entry in the theme echo "
  • $score[name] just scored $score[score] on ".$game[0][name]."
  • "; } } echo "
"; } echo ""; } function init_mochi_lastscores(){ register_sidebar_widget("Mochi Last Scores", "mochi_lastscores_widget"); } add_action("plugins_loaded", "init_mochi_lastscores"); ?>

And you can find the result in the left sidebar in my triqui.com portal.

This feature will be added in my next Triqui MochiAds Arcade theme for WordPress update, but meanwhile you can easily set it up on your own.

Never miss an update! Subscribe, and I will bother you by email only when a new game or full source code comes out.