Tag Archives: php

Add a share/like button to a PHP web page

There are a few things to consider when adding share and like buttons to a php web page.

Firstly, you must ensure that you have your Open Graph tags in place within the <head> section of the site.

<meta property="og:title" content="Page Title" />
<meta property="og:type" content="type (see facebook for types)" />
<meta property="og:image" content="url of image for page" />
<meta property="og:site_name" content="Overall site name" />

If you have developed your own CMS, you should dynamically fill the title tag with the same title as the page itself.

Once these are set up you then need to consider what type of button do you want?  Do you want a share button, which allows your guests to post the page to your timeline, to a friends timeline or to your pages or groups. Or do you simply want a like button?

Personally, even though facebook says the share button is deprecated I still use it.  it is used within facebook all the time so I can’t see why they would stop it being used and it is far more powerful in my opinion.

So the next step is to create the code for the buttons.  One thing you will need before proceeding is the url of the page.  If you need to generated this dynamically use the following code (or similar):

$pageurl = urlencode("http://" . $_SERVER["SERVER_NAME"] . $_SERVER["REQUEST_URI"]);

This will get you the url of the page in the following format: http://server-name/pagename and urlencode with encode the slashes and any other parameters for safe use in the button code.

To generated a share button we use the sharer.php url and appen our page url as follows

<a href="http://www.facebook.com/sharer/sharer.php?u=<?php echo $pageurl;?>">
<img border="0" src="images/facebook-share.jpg" />
</a>

In this instance you will notice that it is simply a standard image link, so you must create an image and store this somewhere on your sever.

This is the one I created if you need one that looks a bit like the ‘like’ button:  facebook-share

To create a like button use the following code:

<iframe src="http://www.facebook.com/plugins/like.php?href=
<?php echo $pageurl;?>&amp;layout=button_count&amp;show_faces=false&amp;
width=100&amp;action=like&amp;font&amp;colorscheme=light&amp;height=21" 
scrolling="no" frameborder="0" style="border:none; overflow:hidden; 
width:100px; height:21px;" allowTransparency="true"></iframe>

Notice the pageurl included and play around with the other parameters.  These are just defaults that I use.

 

 

ftp unzip

Here’s a little trick for web masters out there who upload and extract wordpress, prestashop, joomla or any other open source products with loads of fiddly little files that normally take an age to upload via ftp.

The usual case is you extract the zip locally and upload the few thousand files via FTP and even with a nice wide broadband pipe it can take time to upload each file individually. A better solution would be to upload the zip file via ftp and then log in to your server using ssh or whatever to extract the file, but sometimes it can be a pain on shared hosting to set up ssh and then go through setting up logins etc. or maybe you don’t have that option at all.

Here’s an alternative that might save a few minutes. Look, I’m lazy OK!

What I do is run a little script on the server that calls a method on the ZipArchive class.

The example below extracts the prestashop zip file to the same directory that the archive is in.

<?php
//new zip archive object
$zipArchive = new ZipArchive;

//file to unzip in same directory as this script
$fileToUnzip = "prestashop_1.5.3.1.zip";

//retrieve current path from the zip
$unzipPath = pathinfo(realpath($fileToUnzip), PATHINFO_DIRNAME);

//open and extract the zip file
if ($zipArchive->open($fileToUnzip) === TRUE) 
{
    echo "Unzipping $file to $unzipPath...<br/>";
    $zipArchive->extractTo($unzipPath);
    $zipArchive->close();
    echo "Hey presta!!!";
} 
else 
{
    echo "Cannot open $fileToUnzip... Doh!";
}
?>

If the zip file extracts to a subfolder it will only take seconds to copy the files back to the root directory, if that’s what is required.

Might be handy for someone.