Sunday Feb 5

Archive for the ‘PHP’ Category

Mar
10/11
Adding getIds() Method to Zend Framework Memcache Backend
Last Updated on Thursday, 10 March 2011 12:40
Written by Cody Snider
Thursday, March 10th, 2011

Zend Framework doesn’t allow the use of getIds() with the Memcache backend. This is likely due to performance issues, but the option should still be available. To enable this functionality, change the contents of library/Zend/Cache/Backend/Memcached.php in the getIds() function to the following:

$list = array();
$allSlabs = $this->_memcache->getExtendedStats('slabs');
$items = $this->_memcache->getExtendedStats('items');
foreach($allSlabs as $server => $slabs) {
	foreach($slabs AS $slabId => $slabMeta) {
		if(is_numeric($slabId)){
			$cdump = $this->_memcache->getExtendedStats('cachedump', (int) $slabId);
			foreach($cdump AS $server => $entries) {
				if($entries) {
					if (isset($this->_options['cache_id_prefix']) && $this->_options['cache_id_prefix'] !== '') {
						$prefix    = & $this->_options['cache_id_prefix'];
						$prefixLen = strlen($prefix);
						foreach($entries AS $eName => $eData) {
							if (strpos($eName, $prefix) === 0) {
								$list[] = $eName;
							}
						}
					} else {
						foreach($entries AS $eName => $eData) {
							$list[] = $eName;
						}
					}
				}
			}
		}
	}
}
ksort($list);
return $list;
Posted under PHP  |  Comments  No Comments
Aug
31/10
Hidden Images in Email to Confirm Receipt, Part 2.5
Last Updated on Tuesday, 31 August 2010 11:55
Written by Cody Snider
Tuesday, August 31st, 2010

In part 1 of this series, we discussed how to track email views using an image. In part 2, we setup the tables and functions necessary to record the emails. Before we start building the reporting and campaign tracking mechanisms for step 3, we will need to adjust the code so it will be more maintainable in the future. Based on the traffic and emails I’ve received regarding this script, we are going to develop this into an easy-to-use, quick-to-deploy solution that can adapt to a variety of existing content management systems and web frameworks but still work as a standalone system.

This is simply a cleanup step and the code below is the revised version of the functionality from step 2. Please update the constants in the Config class to use this script.

Requests to this new version also have an argument change and the URL should now look like this (where ID is the id for the user that will receive the email):

http://YOUR-SITE.com/signup_complete.php?userId=ID

<?php
 
// SCRIPT CONFIGURATION
class Config {
 
    const EMAIL_ADDRESS = 'you@site.com'; // YOUR EMAIL ADDRESS
 
    const SITE_DOMAIN = 'site.com'; // YOUR DOMAIN
    const IMG_PATH = '/images/email/'; // PATH TO PNG IMAGE AND TRACK.PHP
 
    const DB_HOST = 'localhost'; // YOUR DB HOST
    const DB_USERNAME = 'username'; // YOUR DB USERNAME
    const DB_PASSWORD = 'password'; // YOUR DB PASSWORD
    const DB_SCHEMA = 'database'; // YOUR DATABASE/SCHEMA NAME
 
}
 
// PRESISTENT DB CONNECT
mysql_pconnect(Config::DB_HOST, Config::DB_USERNAME, Config::DB_PASSWORD);
mysql_select_db(Config::DB_SCHEMA);
 
// USER MODEL
class User {
 
    public $id;
    public $emailAddress;
    public $status = 0; // WE'LL USE THIS AS AN ACTIVE/CAN-BE-EMAILED FLAG
 
    // STATIC METHODS
 
    public static function addUser($emailAddress) {
	$result = mysql_query('INSERT IGNORE INTO `user` SET `email_address` = "' . mysql_escape_string($emailAddress) . '";');
    }
 
    // PUBLIC METHODS
 
    public function __construct($userId){
	$this->id = $userId;
 
	if($this->fetchDetails()){
	    $this->status = 1;
	} else {
	    $this->status = 0;
	}
    }
 
    // PRIVATE METHODS
 
    private function fetchDetails(){
	$result = mysql_query('SELECT * FROM `user` WHERE `user_id` = ' . $this->id . ';');
	if(mysql_num_rows($result) == 1){
	    $row = mysql_fetch_array($result);
	    $this->emailAddress = $row['email_address'];
	    return true;
	} else {
	    return false;
	}
    }
}
 
// EMAIL MODEL
class Email {
 
    public static function send(User $user){
	if($user->status === 1){
	    $subject = 'Test email from ' . Config::SITE_DOMAIN;
	    $body = '<html><body><h1>Test Email</h1><img src="http://' . Config::SITE_DOMAIN . Config::IMG_PATH . 'image' . $user->id . '-' . rand(0, 9999) . '.png" /></body></html>';
	    $headers  = "From: " . Config::EMAIL_ADDRESS . "\r\n";
	    $headers .= "Content-type: text/html\r\n";
	    return mail($user->emailAddress, $subject, $body, $headers);
	} else {
	    return false;
	}
    }
 
}
 
$user = new User($_REQUEST['userId']);
Email::send($user);
 
?>
Posted under PHP  |  Comments  2 Comments