Archive for the ‘PHP’ Category



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
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.

Click to continue…

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.

Click to continue…

29
Jun

PHP Tutorial – Forking using wget or php cli in the background

I see a lot of people asking on how to use a fork to run a php script or web page in the background.

Here’s a quick script I created using linux’s wget to run another process in the background. You could use this to automatically upload something to another server, or just about anything that needs to happen after a user action.

Note: you must have exec enabled for this to work. This is a security risk, so make sure you know the potential damage that can be done by enabling exec, and by the script that you are running. Make absolutely sure to sanitize any input being transferred to the executed script.

In my case, I needed to initiate a file upload to a remote server after a certain action was taken. Because of the nature of the upload, it would have taken about 30 seconds before displaying any response to my user, which is an unacceptable delay.

First, make the script that needs to run in the background.

Click to continue…

29
Jun

**UPDATED** Adding Google Business search to your ecommerce website

**UPDATED**
This is an updated version of the script. It now resides on only a single page and should be less confusing to put into a website. Please feel free to use this script as a base and customize as you like.

Sign up for a business site search account here: http://www.google.com/sitesearch/index.html

The internet is abuzz with news of Google’s new business custom search engine. Google is allowing business sites to have their own custom search engine for $100 per year (<5,000 pages) or $500 per year (up to 50,000) pages. This includes some cool reporting and an XML API so that you can customize to your heart’s content.

Google Custom Search

After messing around with Google’s javascript code I decided to write my own little script to better integrate it into my website. The javascript code was giving me some major problems with the width of my pages, so I canned it and went to their XML version of the search function.

The following is a very simple php & html script that you can use for your own Google business search engine. It was made for a php5 server as it uses the built-in simpleXML class. Upload this to your website, enter your account id, and make a form to post to it. That’s it!

Click to continue…

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