Search:
Main Menu
Login | RSS |

Quick PHP Code Tips and Examples

PHP Programming Tips, Tutorials and Source Code Examples for newbie

Integrate Akismet anti-spam service in PHP Scripts

March 26th, 2007 by Jon Moffet

Everybody knows, spam is an annoying thing in the internet. Especially when you create a Contact Form or a Guestbook only to have it filled with junk messages.

Luckily the folks at Automattic have created Akismet as a measures to combat web spams, and best of all it can be integrated right inside your own customized PHP scripts.

First of all, you need to download Akismet PHP4 Class library (and it works with PHP5 too!)

Then get the Wordpress API key from Wordpress.com, simply sign up for an account there, and the API key can be obtained from "My Account" link at the top right when you log in.

Then include akismet.php class file in your php file that does form processing.

PHP:
  1. require('akismet.class.php');
  2.  
  3. $wpkey = '**YOUR WORDPRESS KEY***';
  4.  
  5. $comment = array(
  6.         'author' => $comment_author,
  7.         'email' => $comment_author_email,
  8.         'website' => $comment_author_website,
  9.         'body' => $comment,
  10.         'permalink' => $comment_guestbook_url,
  11.         'user_ip' => $comment_author_ip,
  12.         'user_agent' => $comment_author_user_agent,
  13.         );
  14.        
  15.     // instantiate an instance of the class
  16.     $akismet = new Akismet('http://fakapster.com/', $wpkey, $comment);
  17.    
  18.  
  19.     if($akismet->isSpam()){
  20.            // store the comment but mark it as spam (in case of a mis-diagnosis)
  21.                //display a warning
  22.  
  23.              die('your comment is marked as spam');
  24.  
  25.  
  26.       
  27.     } else {
  28.          //this comment is not a spam
  29.  
  30.          //insert comment into database code
  31.  
  32.         }

Your guestbook (or online form) should be fairly resistant to spam attacks if you implement anti-spam measures using this method.

Tags: , , , , , , , ,

Posted in Uncategorized | 7 Comments »

Online .htaccess creator tool

March 24th, 2007 by Jon Moffet

Those who run websites on Apache Web server will mostly familiar with the Apache .htaccess file which is used as configuration file on a particular directory. The use of .htaccess file is extremely popular with shared web hosting plans due to its flexibility.

.htaccess file can be use to

  1. Block access to website from certain ip addresses
  2. Create password protected directory
  3. Protects your file from hotlinking
  4. Block hitbots and spam bots
  5. Help create customized error page (404 ,401, 501,503 page)

However each of the task listed above requires different htaccess directive in order to make it work. Coupled with Regular Expression, .htaccess file might no be intuitive to average web masters.

Luckily there's http://www.htaccesstools.com/ which helps you generate the rules mention above. The website is relatively simple to use and easy to understand.

It even features .htaccess help forum for those who have questions about writing .htaccess files.

All in all, http://www.htaccesstools.com/ is a worth website bookmarking for, especially if you're running websites powered by Apache web server.

Tags: , , , ,

Posted in Uncategorized | No Comments »

Libchart : Howto Create Bar Chart and Pie Chart Easily with PHP

March 18th, 2007 by Jon Moffet

Libchart is a lightweight PHP class library which can be use to create charts. It support creating Horizontal Bar , Vertical Bar, Pie chart and Line graph which is useful for data representation on your PHP scripts.

Here's a sample of Libchart output :
bar graph
pie chart

Sample Code

PHP:
  1. include "libchart/libchart.php";
  2.  
  3.     header("Content-type: image/png");
  4.    
  5.     $chart = new VerticalChart(500,250);
  6.  
  7.     $chart->addPoint(new Point("Jan 2005", 273));
  8.     $chart->addPoint(new Point("Feb 2005", 321));
  9.     $chart->addPoint(new Point("March 2005", 442));
  10.     $chart->addPoint(new Point("April 2005", 711));
  11.  
  12.     $chart->setTitle("Monthly usage for www.example.com");
  13.     $chart->render();

You can download Libchart from its author's website.

Alternatively you can use JpGraph to produce chart with PHP. However Libchart is much simpler to use and it can be used freely in commercial applications without requiring you to pay extra licensing fees.

Tags: , , , ,

Posted in Uncategorized | No Comments »

Libcurlemu : a drop-in replacement for php curl library

March 16th, 2007 by Jon Moffet

libcurl or php curl extension is a flexible multiprotocol client library widely in use in various PHP scripts. It can be use to access FTP,HTTP,HTTPS,DICT,TELNET,SCP and LDAP protocol.

libcurl is popular among PHP programmers because it support various HTTP methods which can be use to create web robot, crawler and auto-submitters by emulating a web browser.

However for various reasons, some web hosting company choose not to install php-curl extension and this will affect your php scripts which uses curl_* functions.

Solution :
To solve that problem, you can download and use libcurlemu, a pure PHP implementation of libcurl. By using this library, your php scripts is guaranteed to run regardless if php curl extension is installed or otherwise.

Adapting your scripts to php curl is simple, you only need to include "libcurlemu.inc.php" in your scripts, and everything will run as if the server has php curl installed. There are no further modifications required to use libcurlemu.

Example :

PHP:
  1. // first, include libcurlemu.inc.php
  2. require_once('libcurlemu.inc.php');
  3.  
  4. //... the rest of your scripts is under here, unmodified
  5. $ch = curl_init();
  6.  
  7. curl_setopt($ch, CURLOPT_URL,"http://www.mysite.com/tester.phtml");
  8. curl_setopt($ch, CURLOPT_POST, 1);
  9. curl_setopt($ch, CURLOPT_POSTFIELDS,
  10.             "postvar1=value1&postvar2=value2);
  11. curl_exec ($ch);
  12. curl_close ($ch);

libcurlemu will attempt to use the native PHP Curl extension before it switches to its own library, this will ensure that your scripts will run portably without any further modification when you ported it to a server with PHP curl extensions installed.

Tags: , , , ,

Posted in Uncategorized | 2 Comments »

Convert PHP date() and time into MySQL Date Time Field

March 14th, 2007 by Jon Moffet

Here's a quick way to convert php date() into a format acceptable to be stored in MySQL database 'Date Time' field

PHP:
  1. $date = date("Y-m-d H:i:s",time());

If you want to store the time in MySQL 'Date' field then you can use this code

PHP:
  1. $date = date("Y-m-d",time());

you can later insert $date into MySQL "Date" or "Date Time" field to be manipulated by the database later.

Example of MySQL 'Date' value :

  • 2007-03-11 18:52:42
  • 2007-03-11

Tags: , ,

Posted in Uncategorized | No Comments »

« Previous Entries