PHP 5.4 benchmarks are showing significant speed and memory improvements.
Category Archives: PHP
Postmark API Wrapper for CodeIgniter
I’ve been using Postmark and it’s API for quite a while. If you’re still sending out emails through PHP’s mail() function, you need to check out Postmark! With Postmark you can off-load those transactional emails and not have to worry about it yourself.
While there is a Postmark API Wrapper for CodeIgniter already, I felt it was unnecessary to create an entirely different Email class just to send off API requests to Postmark. Instead, this library extends the Core CI_Email class, retaining the small footprint philosophy of CodeIgniter.
Installation
- Download the library from Github.
- Copy
config/postmark.phpto yourapplication/config/folder - Copy
libraries/Postmark.phpto yourapplication/libraries/folder
Configuration
There is only one setting you need to update in the config file (application/config/postmark.php) and that is your Postmark API key. You can find your API key from the Server Details -> Credentials page in your Postmark Account.
$config['postmark_api_key'] = "YOUR_API_KEY_HERE";
Loading the Library
To use the library, you will need to load it along with the Core CI_Email library (because we extend it).
$this->load->library('email');
$this->load->library('postmark');
OR
$this->load->library(array('email', 'postmark'));
Just make sure to load the Core CI_Email (email) class first.
Sending an Email
The great thing about extending the Core CI_Email class is the ability to not have to change the way you use the class! The only difference is that you will be calling functions as $this->postmark->function_name() instead of $this->email->function_name().
$this->load->library('email');
$this->load->library('postmark');
$this->postmark->from('your_example.com', 'Your Name');
$this->postmark->to('someone@example.com');
$this->postmark->cc('another@another-example.com');
$this->postmark->bcc('them@their-example.com');
$this->postmark->subject('Email Test');
$this->postmark->message('Testing the email class.');
$this->postmark->send();
Other Information
When calling $this->postmark->from(), you will have to use your Postmark API Sender Signature located on the Signatures page in your Postmark Account.
Changelog
- Version 1.0
- April 22nd, 2011 – Initial Release
CodeIgniter Form with Text CAPTCHA
“A CAPTCHA (Completely Automated Public Turing Test To Tell Computers and Humans Apart) is a program that protects websites against bots by generating and grading tests that humans can pass but current computer programs cannot.” — captcha.net
The current CAPTCHA system I started using is Text CAPTCHA. The Text CAPTCHA web service generates text-based CAPTCHAs which is a single question that can be easily solved by humans but cannot be solved by a robot.
Since I added comments functionality to ThePollPlace a few weeks ago, I’ve been noticing lots of spam. Lots meaning, thousands of spam comments. So I implemented Text CAPTCHA because it was simple and easy to do. Now, I’ll show you how to implement it into a form using CodeIgniter. First I’ll show you the code, then I’ll explain it.
application/controllers/example.php
class Example extends Controller {
function Example()
{
parent::Controller();
}
function index()
{
// load libraries
$this->load->library(array('session', 'form_validation'));
// load helper
$this->load->helper('form');
// setup form validation
$this->form_validation->set_rules('name', 'name', 'required');
$this->form_validation->set_rules('email', 'email', 'valid_email');
$this->form_validation->set_rules('url', 'url', 'prep_url');
$this->form_validation->set_rules('captcha', 'captcha', 'required|callback_check_captcha');
$this->form_validation->set_rules('comment', 'comment', 'required');
if( $this->form_validation->run() )
{
// create comment
die('Validated!');
}
// setup textCAPTCHA
try {
$xml = @new SimpleXMLElement('http://textcaptcha.com/api/your_api_key', NULL, TRUE);
} catch ( Exception $e ) {
$fallback = '<captcha>';
$fallback .= '<question>Is ice hot or cold?</question>';
$fallback .= '<answer>'.md5('cold').'<answer>';
$fallback .= '</captcha>';
$xml = new SimpleXMLElement($fallback);
}
// store answers in session for use later
$answers = array();
foreach( $xml->answer as $hash )
{
$answers[] = (string)$hash;
}
$this->session->set_userdata('captcha_answers', $answers);
// load vars into view
$this->load->vars(array( 'captcha' => (string)$xml->question ));
// load the view
$this->load->view('example');
}
function check_captcha( $string )
{
$user_answer = md5(strtolower(trim($string)));
$answers = $this->session->userdata('captcha_answers');
if( in_array($user_answer, $answers) )
{
return TRUE;
}
else
{
$this->form_validation->set_message('check_captcha', 'Your answer was incorrect!');
return FALSE;
}
}
}
application/views/example.php
<h2>Leave a Comment</h2>
<?php echo form_open('/example/index/'); ?>
<div class="textfield">
<?php echo form_label('Name', 'name'); ?>
<?php echo form_error('name'); ?>
<?php echo form_input('name'); ?>
</div>
<div class="textfield">
<?php echo form_label('Email', 'email'); ?>
<?php echo form_error('email'); ?>
<?php echo form_input('email'); ?>
</div>
<div class="textfield">
<?php echo form_label('Url', 'url'); ?>
<?php echo form_error('url'); ?>
<?php echo form_input('url'); ?>
</div>
<div class="textfield">
<?php echo form_label($captcha, 'captcha'); ?>
<?php echo form_error('captcha'); ?>
<?php echo form_input('captcha'); ?>
</div>
<div class="textarea">
<?php echo form_label('Comment', 'comment'); ?>
<?php echo form_error('comment'); ?>
<?php echo form_textarea('comment'); ?>
</div>
<div class="buttons">
<button type="submit" name="submit" value="submit">Submit Comment</button>
</div>
<?php echo form_close(); ?>
In order for Text CAPTCHA to do its job, we need to add a new form field for the question to be answered. I pass the question to the view as $captcha.
<div class="textfield">
<?php echo form_label($captcha, 'captcha'); ?>
<?php echo form_error('captcha'); ?>
<?php echo form_input('captcha'); ?>
</div>
Using CodeIgniter’s Form Validation library, we need to add a new rule for the CAPTCHA. Notice we also have defined a call back: callback_check_captcha. We will use this callback to write a custom validation function to make sure our CAPTCHA was answered correctly
$this->form_validation->set_rules('captcha', 'captcha', 'required|callback_check_captcha');
Next we need to call the Text CAPTCHA service to get our random question. You will need to register to receive your api key. Be sure to replace your_api_key with the key you receive.
This try-catch statement will attempt to communicate with the Text CAPTCHA web service. If it fails, we fallback to a predefined question seamlessly. We then store all of the accepted answers into an array which is also stored in the session so we can use it for validation later.
// setup textCAPTCHA
try {
$xml = @new SimpleXMLElement('http://textcaptcha.com/api/your_api_key', NULL, TRUE);
} catch ( Exception $e ) {
$fallback = '<captcha>';
$fallback .= '<question>Is ice hot or cold?</question>';
$fallback .= '<answer>'.md5('cold').'<answer>';
$fallback .= '</captcha>';
$xml = new SimpleXMLElement($fallback);
}
// store answers in session for use later
$answers = array();
foreach( $xml->answer as $hash )
{
$answers[] = (string)$hash;
}
$this->session->set_userdata('captcha_answers', $answers);
After Text CAPTCHA is setup and initialized, we need to pass the question to our view for use in the form.
// load vars into view $this->load->vars(array( 'captcha' => (string)$xml->question ));
CodeIgniter’s Form Validation library makes custom validation functions easy. All we need to do is check if the answer submitted is in the list of acceptable answers. If not, we fail the test and return our error message.
function check_captcha( $string )
{
$user_answer = md5(strtolower(trim($string)));
$answers = $this->session->userdata('captcha_answers');
if( in_array($user_answer, $answers) )
{
return TRUE;
}
else
{
$this->form_validation->set_message('check_captcha', 'Your answer was incorrect!');
return FALSE;
}
}
That’s it! Simple enough, right? You can download the demo for the complete code.
Directory Listing from a Path
$path = dirname(__FILE__);
$listing = array_filter(scandir($path), function($var) {
// remove special directories '.' and '..' from listing
if( preg_match('/^[.]{1,2}$/', $var) ) { return FALSE; }
// remove files from listing
if( !is_dir($var) ) { return FALSE; }
return TRUE;
});
This is a little snippet of code I’ve been using a lot recently. This function will return all directories inside the path passed as $path. The functions utilizes the ability of Anonymous Functions, only available in PHP 5.3.0.
Turn on Error Reporting
It’s PHP 101, yet so many developers fail to remember it.
display_errors = On error_reporting = E_ALL | E_STRICT
Always enable the display of errors on your development server, disable them on the live server. Showing all errors will help you find those stubborn programming mistakes quickly. Set these options in your php.ini file.