Search:
Main Menu
Login | RSS |

Quick PHP Code Tips and Examples

PHP Programming Tips, Tutorials and Source Code Examples for newbie

Naive Bayesian Tutorial in PHP

May 16th, 2007 by Jon Moffet

The practitioner of artificial intelligence and machine learning algorithm will recognize Naive Bayesian as one of the technique use to construct intelligent web application. Naive Bayesian is widely use as an intelligent classifier utilized to automatically classfies data based on statistical probability.

Among the immediate use of Naive Bayesian technique is the classification of (spam) emails, medical diagnosis dan data pattern identification.

Naive Bayesian is able to 'learn' from experience by training it with sample data set to categorized certain type data.

Excellent Naive Bayesian Tutorial
I found an excellent source of PHP Naive Bayesian classifier tutorial written specifically for those who has no background experience in the field of AI and Data Mining :

  1. Implement Bayesian inference using PHP, Part 1
  2. Implement Bayesian inference using PHP, Part 2
  3. Implement Bayesian inference using PHP, Part 3

Other PHP Naive Bayesian Library
Here is a few great naive bayesian implementation written in PHP

  1. PHP Naive Bayesian Filter
  2. b8 (formerly bayes-php)

Tags: , , , , , ,

Posted in Uncategorized | 1 Comment »

Detect ip location, operating system and browser using PHP Detector Library

May 15th, 2007 by Jon Moffet

I've decided to share my PHP Detector library to the public which I've coded somewhere around 2006. The library does wonders in detecting IP address geo-location, operating system and the type of browser used based on its user-agent information.

The ip address->geo-location information is provided for free from ShowIP fakap webservice.

The library is handy for for creating web application which serve content depending on users location and type of operating system/browser that he use or for creating web application that collect web surfers statistical data.

Here's how to use Detector library :

PHP:
  1. require('detector.php');
  2. $dip = &new Detector($_SERVER["REMOTE_ADDR"], $_SERVER["HTTP_USER_AGENT"]);
  3.  
  4. echo "$dip->town";
  5. echo "$dip->state, $dip->ccode,$dip->town, ($dip->ipaddress) ";
  6. echo "using : $dip->browser $dip->browser_version on $dip->os $dip->os_version";

The live demo of Detector library in action can be viewed here : Detector Library demo.

Requirements

  1. Requires PHP 4.4.0 or 5.0.x
  2. Requires php cURL extension installed, please refer here for workaround if your server doesn't have PHP cURL extension installed

License
Detector library is licensed under Creative Commons Attribution-ShareAlike 3.0, and can be modified and used in commercial application as long as you share the modified source code with the public.

Tags: , , , , , ,

Posted in Uncategorized | 10 Comments »

How to send e-mail with Attachment with PHP Scripts

May 15th, 2007 by Jon Moffet

Here's how to send an email with attachment using PHP scripts (via PHPMailer class).

CODE:
  1. require("phpmailer/class.phpmailer.php");
  2. //****
  3. //PLEASE CHANGE THE SETTINGS HERE !!!!
  4. //change settings here
  5. $your_email = "info@example.com";
  6. $your_smtp = "mail.example.com";
  7. $your_smtp_user = "info@example.com";
  8. $your_smtp_pass = "example_password";
  9. $your_website = "http://example.com";
  10. //****
  11. //get contact form details
  12. $name = $_POST['name'];
  13. $email = $_POST['email'];
  14. $url = $_POST['url'];
  15. $comments = $_POST['comments'];
  16. $response="Name: $name\nContents:\n$comments\n";
  17. $mail = new PHPmailer();
  18. $mail = $mail->SetLanguage("en", "phpmailer/language");
  19. $mail->From = $your_email;
  20. $mail->FromName = $your_website;
  21. $mail->Host = $your_smtp;
  22. $mail->Mailer   = "smtp";
  23. $mail->Password = $your_smtp_pass;
  24. $mail->Username = $your_smtp_user;
  25. $mail->Subject = "$your_website feedback";
  26. $mail->SMTPAuth  =  "true";
  27. $mail->Body = $response;
  28. $mail->AddAddress($your_email,"$your_website admin");
  29. $mail->AddReplyTo($email,$name);
  30.  
  31. $mail->AddAttachment("picture.png","picture");
  32.  
  33. echo "<p>Thanks for your feedback, <em>$name</em>! We will contact you soon!</p>";
  34. if (!$mail->Send()) {
  35. echo "<p>There was an error in sending mail, please try again at a later time</p>";
  36. }
  37. $mail->ClearAddresses();
  38. $mail->ClearAttachments();

the key function of this script is $mail->AddAttachment("picture.png","picture.png");, where you can add any file as email attachments from your script.

Hopefully this example will help you to create a useful and flexible mail sending php scripts

Tags: , , , , , , ,

Posted in Uncategorized | 4 Comments »

PHP: Currency Exchange using XML-RPC client

May 11th, 2007 by Jon Moffet

This is an example PHP code I wrote to demonstrate how to connect to Foxrate Currency Exchange server and retrieve the latest exchange rate.

The example uses The Incutio XML-RPC for PHP library. Please visit refer to Incutio XML-RPC Library Manual for further in-depth reference.

PHP:
  1. require('IXR.inc.php');
  2.  
  3. // connect to foxrate.org currency exchange server
  4. $client = new IXR_Client('http://foxrate.org/rpc/');
  5.  
  6. //make the currency exchange
  7. if (!$client->query('foxrate.currencyConvert', 'USD', 'GBP',1000)) {
  8.     die('Error detected - '.$client->getErrorCode().' : '.$client->getErrorMessage());
  9. }
  10.  
  11. $response = $client->getResponse();
  12. echo "USD1000  is currently equal to GBP " . $response['amount'];

The demo shows you how to get the current value of USD 1000 in British Pound (GBP). Hopefully this example will be useful for you to build a working currency exchage webapp using foxrate.org api

Download the sample code here : foxrate_rpc_1.zip

Tags: , , , , , ,

Posted in Uncategorized | 1 Comment »

OpenTracker - Lean and mean PHP Bittorrent Tracker

May 10th, 2007 by Jon Moffet

Opentracker is the most simple php bittorrent tracker that I came across. It only requires a single MySQL table to function and it is a fully compliant bittorrent tracker as outlined by Bram Cohen.

I've included Opentracker in this blog because of its simplicity that made it easy to be use as a reference implementation for your own home-brew tracker.

Download Opentracker

  1. opentracker.zip (local)
  2. opentracker.zip (mirror)

Important Notice Due remember that MySQL based bittorrent trackers are known to use many connections at the same time, and may cause your database server to choke in a shared hosting environment. You've been warned!

Tags: , , , , ,

Posted in Uncategorized | No Comments »

« Previous Entries Next Entries »