Archive for the ‘PHP’ Category

How To Get Statistics For A Facebook “Like” Button And Shared URLs

Friday, May 28th, 2010

The last article showed how to add a Facbook “Like” button to your website and observing the click event on the button to track and/or do something else with JavaScript when a user clicks on the “Like” button.

Now you might want to get some information on the URL via some automated processes like cronjobs etc. You can simply use FQL - the Facebook Query Language - for that purpose. FQL is similar to SQL but doesn’t support all of the features.

Using FQL

Here is some FQL to get some statistics for a link:

SELECT  like_count, total_count, share_count, click_count from link_stat  where  url="http://www.saschakimmel.com/2010/05/how-to-capture-clicks-on-the-facebook-like-button/"

You can simply use a Facebook API client SDK or access the data directly via the URL even in your browser:

https://api.facebook.com/method/fql.query?query=select%20%20like_count,%20total_count,%20share_count,%20click_count%20from%20link_stat%20where%20url=%22http://www.saschakimmel.com/2010/05/how-to-capture-clicks-on-the-facebook-like-button/%22

This will return XML like this:

<?xml version="1.0" encoding="UTF-8"?>
<fql_query_response xmlns="http://api.facebook.com/1.0/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" list="true">
 <link_stat>
 <like_count>4</like_count>
 <total_count>5</total_count>
 <share_count>1</share_count>
 <click_count>0</click_count>
 </link_stat>
</fql_query_response>

This means you can also use something like cURL or even PHP’s file_get_contents() method to obtain this information and parse it with SimpleXML, DOM or even regular expressions.

Facebook doesn’t seem to define any specific API call limit but there seems to be a limit to the number of calls per day.

Pitfalls

The Facebook API doesn’t support “LIKE” queries in FQL so you cannot find out how many pages on your website were liked directly - only by querying like described above for every single URL on your website.

I hope this short article is useful for you. If you like it why not just click on the “Like” button below? :)

How To Locate Your Website Visitors Via IP

Tuesday, October 13th, 2009

More and more websites are using a geolocation service to show advertisements and offers near a visitor’s location. Using such a service on your own website is quite easy. This post only focuses on using the MaxMind GeoIP City database based on IP addresses and using it with PHP because I have already implemented this solution on one of my websites and wanted to share this tutorial because I think it may be helpful for my visitors.

This tutorial requires that you are able to compile PHP extensions on your system which means that you must have root access to your server and need to be able to access it from the Linux command line.

Although the price you have to pay for the MaxMind database may appear high at first glance you may not need the full database but may just purchase a database for a single country - just as I purchased the German database only. There is also a free GeoLite City database available from MaxMind which has a worldwide coverage and has an accuracy of 79% for US cities.

There are just 6 simple steps for you to perform - I will cover using the German database on an Ubuntu Linux system only but only the file name is different with versions for other countries so you need to adjust some values in the examples below.

  1. Purchase and download the database (choose the API version, not the CSV file)
  2. Unpack the file and move the contents to the appropriate directories:

    tar -xvzf GeoIP-132de_20090901.tar.gz
    mkdir /usr/local/share/GeoIP
    mv GeoIP-132de_20090901/GeoIPCityde.dat /usr/local/share/GeoIP/GeoIPCity.dat
  3. Install the header files for the library:
    apt-get install libgeoip-dev
  4. Install the geoip extension for PHP:
    pecl install geoip

    If you’re getting an error like this

    checking for LGPL compatible GeoIP libs... wrong version
    configure: error: You need version 1.4.0 or higher of the C API

    you can fix this by downloading the C files directly from MaxMind and compiling them manually:

    wget http://geolite.maxmind.com/download/geoip/api/c/GeoIP-1.4.6.tar.gz
    tar -xvzf GeoIP-1.4.6.tar.gz
    cd GeoIP-1.4.6
    ./configure
    make
    make check
    make install
    apt-get remove libgeoip-dev
    pecl install geoip

  5. Load the new PHP extension - add these lines to your php.ini:
    extension=geoip.so
    geoip.custom_directory=/usr/local/share/GeoIP
  6. Restart your (Apache) web server

You can now use all of the PHP functions the geoip extension offers such as:

<?php var_dump(geoip_record_by_name($_SERVER['REMOTE_ADDR'])); ?>

In the next post I’ll cover using the GeoLocation object available in JavaScript in Firefox 3.5 and using it to get even more detailed locations.

How To Install Apache, MySQL and PHP (WAMP) On Windows Manually

Friday, May 29th, 2009

If you are using Windows as your operating system when developing your websites you should always test your websites on your local system before you upload the files to your live server.

Although there are several pre-packaged Apache-PHP-MySQL (WAMP) packages available for download when using these you are bound to their release cycles and the structure of the directories they are using.

This article describes how you can install Apache, PHP and MySQL manually on your local system with maximum flexibility because you can quickly update one program if the need may arise, e.g. if a new PHP version has been released.

This method also shows you where specific files are located which you may need to edit when changing settings so you can dive a bit deeper into the configuration files which may be quite helpful if an error occurs.

WAMP Setup

In this tutorial I’m assuming the following:

  • Apache, PHP and MySQL are installed to subdirectories of d:\webserver
  • The document root is d:\webroot
  • the first test domain is www.webserver.local
  • the document root for that VirtualHost is d:\webroot\www.webserver.local\htdocs which will contain all documents accessible via the browser

Installing The Apache Server

First of all you need to download the latest Windows distribution of the Apache HTTP server from the Apache download page. Pick the MSI installer including OpenSSL which is named something like „Win32 Binary including OpenSSL 0.x.x (MSI Installer)” and is listed under the section named something like “Apache HTTP Server 2.2.xx is the best available version”.

After launching the setup program pick the „Custom” setup and choose the directory d:\webserver\Apache as the installation path. Enter the information as shown in the screenshots below.

apache-install

apache-install-2

Installing PHP

Dowload the latest (stable) PHP 5 version from the PHP download page. Choose the „PHP 5.x installer”. After download and launching the file when prompted to select a directory choose d:\webserver\PHP

php-install

Webserver Setup - “Select the Web Server You Wish To Set Up” select “Apache 2.2.x”.

php-install-2

When prompted for the Apache configuration directory select d:\webserver\Apache\conf.

php-install-3

On “Choose Items To Install” select the extensions you’d like to install.

If you don’t know which might be useful you may use the following list to find some suggestions:

  • cURL (good for accessing HTTP servers)
  • EXIF (if you’re dealing with photos and wish to extract meta information)
  • GD2 (image creation and manipulation)
  • Multi-Byte String (mbstring, for i18n, e.g. conversion from ISO-8859-1 to UTF-8)
  • mcrypt (for encryption)
  • mysql, mysqli
  • other database extensions you might be using (postgreSQL etc.)
  • OpenSSL (for accessing https URLs)
  • PDO, PDO_Mysql, PDO_SQLITE
  • SOAP (if you’re planning to access SOAP resources or create you own SOAP server)
  • SQLite

Installing MySQL

To download the MySQL server visit this MySQL website and download the Windows MSI Installer package under „Community Edition”.

To ease administration and creation and editing of tables and databases I encourage you to download and install the MySQL GUI tools as well.

In the MySQL setup dialog pick the custom installation and select the directory d:\webserver\MySQL as the installation directory.

mysql-install-1

mysql-install-2

When configuring the MySQL server use the detailed configuration.

mysql-install-3

Use “Developer Machine” as your server type

mysql-install-4

Select “Multifunctional Database” in the database usage dialog:

mysql-install-5

Keep the default InnoDB tablespace settings and click Next:

mysql-install-6

Select “Decision Support” on the next dialog:

mysql-install-7

Keep the default values on the next screen:

mysql-install-8

When prompted to select a character set use the one that fits best, I’m using UTF-8 as the default charset.

mysql-install-9

On the next dialog you have to choose whether to install the server as a Windows service and if it shall be started automatically at boot time. This setting depends on how often you’re developing on your system. I’m keeping MySQL running in the background most of the time because it doesn’t eat that much memory.

mysql-install-10

The next dialog prompts you to select a root password. Keep this in mind.

mysql-install-11

After the installation has finished the MySQL server is already running and you can now begin to configure PHP and Apache.

Configuring Your System

For every VirtualHost (i.e. website/domain) you need to add a corresponding entry to your HOSTS file so that your browser knows that the given domain will be handled by the webserver running on your local system. Edit the file c:\windows\system32\drivers\etc\hosts with a text editor.

Windows Vista Information

On Windows Vista you have to launch Notepad as an administrator to see and edit the file.

Add a new line to your hosts file which has the following content:

127.0.0.1 www.webserver.local

Now requests to www.webserver.local from your browser will be sent to the webserver running on your local system.

Configuring The Apache Webserver

There are actually two files to edit. Please note that you always need to use forward slashes or a double backslash in path names.

General Settings

Open the file d:\webserver\Apache\conf\httpd.conf and search for the line reading

DocumentRoot "D:/webserver/Apache/htdocs"

The default document root is D:/webserver/Apache/htdocs. We now need to change this to point to the d:\webroot directory as defined above. So change this line now to

DocumentRoot "d:/webroot"

You also need to modify the line reading

<Directory "D:/webserver/Apache/htdocs">

to

<Directory "D:/webroot">

If you wish to use .htaccess files you also have to change the line reading

AllowOverride None

to

AllowOverride All

To include the VirtualHosts file which will contain all mappings of hostname to document root directory (and more) you have to change the line

#Include conf/extra/httpd-vhosts.conf

to

Include conf/extra/httpd-vhosts.conf

To enable automatic execution of an index.php file when the user accesses a directory (which you should always do) you need to add index.php to the DirectoryIndex directive just as in this example - simply add „index.php” to the end of the line:

<IfModule dir_module>
DirectoryIndex index.html index.php
</IfModule>

Creating The VirtualHosts Configuration

Open the file d:\webserver\Apache\conf\extra\httpd-vhosts.conf. Now it’s time to create a VirtualHost for our domain www.webserver.local.

Just add the following block to the end of the file.

<VirtualHost *:80>
ServerAdmin webmaster@example.com
DocumentRoot "D:/webroot/www.webserver.local/htdocs"
ServerName www.webserver.local
ErrorLog "logs/webserver.local-error.log"
CustomLog "logs/webserver.local-access.log" common
</VirtualHost>

This will tell Apache…

  • that the server administrator can be reached via webmaster@example.com (no need to enter your real email address, because on your own development system you’re the only one that would see your real email address so it doesn’t make sense
  • that the documents for www.webserver.local can be found in the directory d:/webroot/www.webserver.local
  • to log errors to the file d:/webserver/Apache/logs/webserver.local-error.log
  • to log every access to d:/webserver/Apache/logs/webserver.local-access.log

Creating The Documents

Create the directory D:/webroot/www.webserver.local/htdocs recursively and create the file D:/webroot/www.webserver.local/htdocs/index.php with the following contents:

<?php phpinfo(); ?>

Configuring PHP

The default PHP installation would not show you any errors in the browser and instead just return a 500 server error which would materialize as an empty white page.

Although this is the recommended setting for productions servers where you do not normally want to reveal any specific information like paths when an error occurs this makes life quite hard on your local development server. To enable the display of error messages in the browser open your php.ini file which is located at d:\webserver\php\php.ini and change the line

display_errors = Off

to

display_errors = On

To enable sending of emails via the mail() function you need to define a specific SMTP server name and port. You would normally enter your providers’s mail server name here. Please keep in mind that some providers use a so-called „POP-Before-SMTP” so that sending mails with PHP may fail if you have not downloaded new messages via POP3 from the server before.

Testing The Installation

Now you are ready to test your new web server. First of all you need to restart the Apache server via the included taskbar tool or via the Windows services panel.

After that open up your browser and enter http://www.webserver.local/

This should show the phpinfo() page.

And now: have fun developing!