PHP Facebook Share

Tired of "Waiting for connect.facebook.com..."?

You can use this little PHP workaround, it consists of "share.php", MySQL database table, a little function, and Share Button graphics.

1. "share.php"

<?php
/**
 * @author Webarto
 * @copyright 2010
 */

include("db.php"); //Include database connection

$uri = mysql_real_escape_string(htmlspecialchars($_SERVER["HTTP_REFERER"], ENT_QUOTES, "UTF-8")); //Sanitize URI input

mysql_query("INSERT INTO facebook(uri,ip,timestamp) VALUES(
'$uri',
'".$_SERVER["REMOTE_ADDR"]."',
'".time()."')"); //Insert necessary data

header("Location: http://www.facebook.com/sharer.php?u=".$_SERVER["HTTP_REFERER"]); //Redirect to Facebook Sharer
?>


2. MySQL database table

CREATE TABLE IF NOT EXISTS `facebook` (
  `id` int(11) NOT NULL auto_increment,
  `uri` varchar(255) NOT NULL,
  `ip` varchar(15) NOT NULL,
  `timestamp` int(11) NOT NULL default '0',
  PRIMARY KEY  (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=0;


3. PHP function to call the button
function share(){
    $uri = mysql_real_escape_string(htmlspecialchars("http://".$_SERVER["HTTP_HOST"].$_SERVER["REQUEST_URI"], ENT_QUOTES, "UTF-8"));
    $quantity = mysql_query("SELECT COUNT(0) FROM facebook WHERE uri = '$uri'");
    $quantity = mysql_fetch_array($quantity);
    $quantity = $quantity[0];
    return('<h4><a target="_blank" href="/share.php"><img alt="" src="/share.png"/> '.$quantity.'</a></h4>');
}


4. (edit to suit your language)
©2009-2011 Webarto • web design & development • Tuzla // Sarajevo // Beograd