27
Jul
Comments Off on US States Snippet and SQL Dump

US States Snippet and SQL Dump

Here’s some US states snippets. Included are php arrays, and a MySQL states dump…

Continue reading…

25
Jul
Comments Off on PHP – Script benchmark / bottleneck debugging snippet

PHP – Script benchmark / bottleneck debugging snippet

Here’s a really simple function that I use for finding bottlenecks in php scripts. You can add any number of steps to the the script using the microtime() function, and this function shows the execution time of each step.

/**
 * Benchmark a php script
 *
 * @param array $time_sample
 * @return string HTML
 */
function quick_benchmark($time_sample = array())
{
	$steps = count($time_sample);
	$output = '';
 
	for($i=0;$i<$steps;$i++)
	{
		if($i<($steps-1))
		{
			$output .= '<p>Time '. ($i+1) .': '. number_format(($time_sample[$i+1] - $time_sample[$i]),6,'.','') .' seconds.</p>';
		}
	}
 
	$output .= '<p>Total time: '. number_format(($time_sample[$steps-1] - $time_sample[0]),6,'.','') .' seconds.</p>';
 
	return $output;
}

This is a simple example using sleep() to demonstrate the output.

$time_sample[] = microtime(true); //start
sleep(1);
$time_sample[] = microtime(true); //time 1
sleep(2);
$time_sample[] = microtime(true); //time 2
sleep(3);
$time_sample[] = microtime(true); //time 3
sleep(1);
$time_sample[] = microtime(true); //time 4
 
echo quick_benchmark($time_sample);

The script outputs:

Time 1: 1.001833 seconds.
Time 2: 2.001427 seconds.
Time 3: 3.001124 seconds.
Time 4: 1.001720 seconds.
Total time: 7.006104 seconds.

It’s a good idea to comment each time you record a microtime so that you know which section of script took that amount of time.

24
Jul

PHP – Random string generator snippet

This is a little function that I use all the time to generate random strings. There are 3 options for random strings with this: Alpha, Alpha-numeric, and Alpha-numeric with symbols. This is important because sometimes it’s a good idea not to allow special characters in a php string. However, the special characters are great if you need to create a key or initialization vector for 2 way encryption.

This can be used to generate random passwords or keys or just about anything else that needs a random string. You can also throw this directly into a class and use it as a static method.

 
/**
 * Generate a random string
 *
 * @param int $length
 * @param int $mode 1 = Alpha, 2 = Alpha-numeric, 3 = Alpha-numeric with symbols
 * @param boolian $char_set Set true for Upper and Lower case letters
 * @return string 
 */
function random_string($length=16,$mode=1,$char_set=false)
{
	$string = '';
	$possible = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
 
	if($char_set) {
 
		$possible .= strtolower($possible);
 
	}
 
	switch($mode) {
 
		case 3:
 
			$possible .= '`~!@#$%^&*()_-+=|}]{[":;<,>.?/';
 
		case 2:
 
			$possible .= '0123456789';
			break;
 
	}
 
	for($i=1;$i<$length;$i++) {
		$char = substr($possible, mt_rand(0, strlen($possible)-1), 1);
		$string .= $char;
	}
 
	return $string;
}

Examples:

echo random_string(32);
//WQTISVJVMWSEFXEIQISJPCBENFEHQAN
echo random_string(16,2,true);
//cZhVGHJb0PqJIk3
echo random_string(16,3);
//=,:UT__GN[ST>GH
21
Jul

25 Point Basic MySQL Setup/Optimization Checklist

Daily I run into new web programmers that are using PHP and MySQL to create their blogs and websites. I created this checklist as a guide for new and experienced to make sure they are covering the basics of a MySQL server setup.

This guide is by no means all inclusive, but should help to cover some of the major gaps in knowledge and commonly overlooked fundamentals that I run into on a daily basis.

The checklist is separated into 5 equal sections: Server Setup, Schema Design, Table Design, Index Optimization, Query Optimization, and a 6th Bonus Tips section.

Continue reading…

10
Jul

PHP AES Encryption

Sometime it’s needed to use 2 way encryption for storing data. I’ve been using an AES encryption class for a little over a year now, and it is an excellent way to use FIPS Compliant AES encryption in php. The script comes in a free version (ECB mode only) and a paid version for only $10.

This is a completely standalone class that does not require the mcrypt library, and has php4 and php5 support. Encryption is available in 128, 192, and 256 bit, depending on the cipher length.

Using it is easy as this:

include("AES.class.php");
 
$my_256_key = 'MpDsw*8cQM&fez*7eBoZB^W*kP652NoW';
$initialization_vector = 'WmR&z28zWn8r*9$R';
 
$aes = new AES($my_256_key, "CBC", $initialization_vector);
 
$string_to_encrypt = 'SOME STRING OF TEXT, OR EVEN AN ENTIRE FILE';
 
$encrypted_string = $aes->encrypt($string_to_encrypt);
 
$original_string = $aes->decrypt($encrypted_string);

The cipher modes that are supported are: Electronic Codebook (ECB), Cipher Block Chaining (CBC), Cipher Feedback (CFB), and Output Feedback (OFB). —Block Cipher Modes »

If you are needing to integrate real encryption into a script, I highly recommend this class. It’s strong enough for storing sensitive data like credit cards (key management is another topic), and has an extremely easy interface.

Keep in mind that encryption is only as secure as the key and the key management that is used. Unlike using hash functions (Md5, SHA1), encryption can be reversed, and will considerably slow down a php script, especially so for encrypting large amounts of data.

Database Storage:
I would recommend base 64 encoding an encrypted string and then storing in a text or blob type field. Output from AES encryption will likely be corrupted by a database’s character encoding if you do not.

2
Jul

PHP Multi-process 1.1.1 (Beta) Released

I just finished the second edition of the php multi-process script. Implementing it is significantly simpler now, and will allow mysql or sqlite implementation for the cache database.

The PHP Multi-process repository is now available at: http://github.com/jestep/PHP-Multi-process/tree/master.

Also, the usage guidelines can be found at: http://www.saynotoflash.com/scripts/php-multi-process/.

If you discover any problems, please log them here: http://github.com/jestep/PHP-Multi-process/issues

Still needing to implement:
Resource checking is a major component that needs to be addressed. If anyone has any suggestions or experience on how to check available resources, and would like to help out, please let me know.

1
Jul

PHP Multi-process dev page

I’ve created a final resting place for the PHP Multi-process script. This page will include all future updates as well as usage and other changes to the script.

Currently, I’m working on the Beta version of the script. From the feedback I’ve received, the script is mostly stable, and needs only minor changes considering its complexity.

The changes I’m currently anticipating for 1.1 beta are:

  • Alternate: Mysql or Sqlite Implementation
  • Server Resource Checking (RAM and CPU usage)
  • Separating the DB, Parent, and Child classes

I’ve also come up with method of creating new scripts from the parent to process, however changes like this would most likely appear in an alternate or fork of the current script.

Again, if you’ve used the PHP Multi-process script and have any changes that you’ve made that have helped or would like to help support this script please let me know.

In case you missed it, here’s the link to PHP Multi-process script: http://www.saynotoflash.com/scripts/php-multi-process/

30
Jun

Symfony and Modalbox

ModalBox is a cool inline-popup script that can be used to create interactive Web 2.0 dialogs. Modalbox uses the Prototype javascript framework, which is coincidentally the same that Symfony uses for its included Javascript and Ajax functions.

model-ex

Unfortunately Modalbox uses a more recent version of Prototype, and there is no track on when we can expect Symfony to upgrade it’s included version of Prototype. However, this doesn’t prevent us from using Modalbox in a Symfony development. We do have to trick symfony into using the correct Propel version for us. Once Symfony updates their Prototype version, using modalbox should become substantially easier.

To start out we need to download modalbox, and upload the newer prototype, scriptaculous, and modalbox.js scripts to our web/js directory.

Continue reading…

29
Jun

Querying with Propel Criteria using “NOT IN” (criteria::not_in)

It’s fairly common to use “NOT IN” database queries in web development.

Symfony when using Propel does not have native support for using NOT IN queries in reference to another tables. You can use NOT IN and an array, but cannot use NOT IN with reference to another table.

Here’s how to use NOT IN other_table with Propel and Symfony. Let’s pretend we need to run this query.

SELECT * FROM my_table_1 WHERE id NOT IN (
SELECT id FROM my_table_2);

Using Propel and Criteria, there is no native way of running this query. But, with the Criteria::CUSTOM modifier, we can force this query through propel.

It’s actually very easy.

 
$c = new Criteria;
 
/* Example query
$not_in_query = 'my_table_1.id NOT IN (
SELECT id
FROM my_table_2)';
*/
 
$not_in_query = '%s NOT IN (
SELECT %s
FROM %s)';
 
$not_in_query = sprintf(
$not_in_query,
MyTable1Peer::ID,
MyTable2Peer::ID,
MyTable2Peer::TABLE_NAME
);
 
$c->add(MyTable1Peer::ID, $not_in_query, Criteria::CUSTOM);
 
$result = MyTable1Peer::doSelect($c);

You can add whatever other Criteria to the query as well. So far this is the only reliable way to run these sort of queries that I’ve found.

29
Jun

Multi process / thread PHP execution

UPDATE– I’ve created the official PHP Multi-process script page. Further updates, the usage manual, and other information about PHP Multi-process will be found here.

I’ve run into the problem of needing to use multiple processes with a php script on multiple occasions. Most notably, I have some very complex data processing scripts that take several minutes to complete. If I can break these up into several parts and run them all at the same time, the execution time can be greatly reduced.

php-multi

There are several methods of running additional processes with php. pcntl_fork is a great way to do it, but it requires php to be running as a cgi module rather than through apache as most web hosts do.

This is a script of how to fork multiple processes on a server running PHP through apache. These process will all run concurrently, and the parent will wait to exit until all of the child processes have completed.

Continue reading…

Copyright © 2024 SayNoToFlash, Jamie Estep, All Rights Reserved · Theme design by Themes Boutique