Cool Social Share Buttons with Share Count Script for PHP

The fastest share buttons with share count.

Last updated on
Subscribe to Internet Tips and Tools Feed

Social Network share buttons with share count can really slow down visitor's browsers that are using your website. Each button has to load the share count from the remote server of the social network. This sometimes causes pausing and freezing. Some of these buttons load asynchronously (async) with javascript and they may "claim" that they won't slow your website down. But this is not the case. Accessing the remote servers even asynchronously still causes the user to experience slowdowns.

With "Cool Social Share Buttons with Share Count Script for PHP" you can still display share buttons with share count for Google+, Facebook, Pinterest, Twitter and/or LinkedIn without slowing down your visitor's browsers because the share count is retrieved and cached on your server, so your visitor's browsers to not have to load any data from remote servers to display the share count.


Social Buttons:
Share Count Style:
Buttons Direction:

Copy the data below to your website:



Downloaded 0 times.
Please make a donation to reveal the download link.

Unzip the 3 files in and upload them to your website after you edit the following variables in coolsocial.php:

	/* Edit the variables below */

	$share_url = $_SERVER['HTTP_REFERER']; // The url to share will be detected by the webpage address
	//$share_url = ""; // Or uncomment this line to specify URL to like
	$share_text = ""; // Used on Twitter and Pinterest as description. Blank out to use current document.title
	$share_image = ""; // Used on Pinterest. Blank out to use the webpage's meta og: or itemprop tags
	$get_count_onclick = true; // Get count whenever a user clicks a social button
	$get_count_minutes = 720; // 0 = Don't get count every n minutes; (720 = Get count every 12 hours)
	$use_twitter_newsharecounts = false; // If true then register your website at

	$server="localhost"; // Usually keep as "localhost"
	$database="your_mysql_database"; includes these files:
coolsocial.phpThe PHP script for super fast share buttons with share count
coolsocial.cssThe css style sheets for the share buttons
db.phpRequired by coolsocial.php for mysql database functions


March 8, 2021 - Version 1.1c - Bug Fix - Put inet_pton() inside of escape_string because on the outside mysql sometimes has an error because inet_pton() sometimes puts an unescaped single quote in the string.

2/19/2018 - Version 1.1b - On February 7, 2018 LinkedIn announced that they are no longer keeping track of or displaying share counts for LinkedIn. So now the script will keep track of the LinkedIn shares itself in the MySQL database by keeping count of the clicks on the LinkedIn share button. Also, added PHP curl as an alternative to get share count from Facebook and Pinterest in case of file_get_contents() not working such as getting the error: PHP Warning: file_get_contents(): http:// wrapper is disabled in the server configuration by allow_url_fopen=0.

2/17/2018 - Version 1.1a - Fixed a number of bugs including adding $get_count_minutes to the global variable in check_network() function and not updating the mysql database if the last share count in the database is the same as the share count retrieved from the share network. Also if the count is updated or added to the database because it is the first time or because $get_count_minutes as elapsed then it won't use the ip address of the user visiting so that they can still click on the share button.

1/18/2018 - Version 1.1 - Google announced that Google+ will no longer keep track of display social share count. So now the script will keep track of the Google+ shares itself in the MySQL database. Also, previously a user click on the share button was not added to the database, only what was received from the share service itself was recorded in the database. This has been changed. Now user clicks on a share button are recorded in the database as long as they are from a different IP address then the last click on the share button.

12/22/2016 - Version 1.0 - Cool Social Share Buttons with Share Count Script for PHP Created

Last updated on March 8, 2021
Created on December 22, 2016

Back to
Subscribe to Internet Tips and Tools Feed        

User Comments

There are 12 comments.

Displaying first 50 comments.

1. Posted By: Clinton - - September 6, 2017, 1:31 am
No Database and where is this files @include_once('realsettings.php');?

2. Posted By: Jeff - - September 6, 2017, 10:27 am
Hi Clinton,

The database is automatically created on lines 188-197 of coolsocial.php. Try it! It works fine. realsettings.php is not included. That file is just in case you want to put your settings in a different file. The @ in front of include() will make sure that PHP does not display an error if it does not find the file. If you put your settings in coolsocial.php then you can comment out the line.


3. Posted By: Clinton - - September 8, 2017, 4:37 pm
thank you very much

4. Posted By: Arshi - - January 17, 2018, 8:33 am

I have implemented your share to social media code and in first look fine, but when i click on any button, at once its showing increased value above social media button, but when i check database, there is no increment or when i refresh button page no increment, i don't understand why so? anything wrong ?

Please help me.

5. Posted By: Jeff - - January 17, 2018, 11:32 am
Hi Arshi,

The script puts a fake increased value just for the user that clicked. So they can see their click. But the click only increases in the database if the user follows through with sharing the page. Then the count is updated according to
$get_count_minutes. So it will be a while before you see the numbers so that your server is not slowed down. That is how it works for Facebook, LinkedIn and Pinterest. Since Twitter does not keep track of share counts those are automatically entered into the database. Also, recently Google+ has decided not to keep track of share count. So I will have to change the code to automatically add that click to the database. I will create a new version soon.


6. Posted By: Arshi - - January 17, 2018, 3:04 pm
Thank you so-much for response, basically I am developing member site, and member need to login and then click on social media icons to share different content on social media and content have their content_id and members have member_id both also want to save in database, so later i can see which member how many times and which content shared on which social media, I tried many way, but I think due to ajax i m not able to figure out, I m not good in ajax, I can not send/store member_id, content_id with all other details which already in your DB, May you guide just tell me where and what should i wrote to save/store these 2 fields also in database?, I was thinking session will do the trick, but nothing happening! May you please guide/help me?


7. Posted By: Jeff - - January 17, 2018, 6:15 pm
Hi Arshi,

I can try to help you. Session variables should work. Are you storing member_id in a session variable already? To use session variables, at the top of the script where you are storing the member_id in a session variable you will need to put @session_start(); for the first line of the script to start session handling. Then in coolsocial.php you will also want to add @session_start(); as the top line. You should create a new field for the social_count database. Something like: member_id BIGINT UNSIGNED

I have just uploaded a new version of coolsocial.php. So you will want to download that. It now keeps track of Google+ shares because Google+ no longer does. It also will keep track of a user click on each share button as long as it is from a different ip address.


8. Posted By: Jeff - - January 17, 2018, 6:17 pm
In the latest version of coolsocial.php lines 117-124 is where the database is updated. So before that on line 116 you will want to add:

$member_id=db_escape_string($db_link, $_SESSION['member_id']);

Then to add the sanitized variable to the database. Change line 117 to look like this:

$query="UPDATE social_count SET count='$count', ip='$ip', date_updated=NOW(), member_id='$member_id'

And change line 122 and 123 to look like this:

$query="INSERT INTO social_count (url, network, count, ip, date_updated, member_id)
VALUES ('$share_url', '$network', '$count', '$ip', NOW(), '$member_id')";


9. Posted By: Arshi - - January 27, 2018, 6:33 pm

I have modified code as u said for adding member id, Now problem is, when member click on Facebook share, twitter or any icon, its automatically update database without clicking on any social media post button/link, i want database update only when content shared on social media successfully, currently click on any icon its update even u don't click on any social media "POST" link/button.

Any solution for it? I will be Thankful to you!


10. Posted By: Sidharth - - May 10, 2019, 1:16 am
On sharing it to fb
It is showing 'feature isn't available right now, an error occur while processing this request.

It's also showing error on posting it to LinkedIn

11. Posted By: Jeff - - May 10, 2019, 8:51 am
Hi Sidharth,

In googling the error the results say that the error occurs when the link you try to share is not live. See this post:


12. Posted By: Sidharth - - May 11, 2019, 8:46 am
Very very thanks for replying ,
Sorry for bothering you,
Fault is mine , I was doing it wrong
Your code was just amazing