Filed Under (General) by Wenbert on 31-10-2007
Here is an interesting find. Someone blogged about a better programmer’s font. Like him, I have been using Courier New for years now. But this guy found a better alternative. It’s more compact and a lot more readable. It really does save a lot of desktop real estate.

Quoted from his post:
For years I’ve used Courier New or whatever font my IDE chose for me. On a whim, I decided to search for a better programmer’s font; one that’s not only is easier on the eyes, but is more compact and efficient. I think I’ve found it: ProggyFonts
The Proggy font collection is composed of several free monospaced fonts designed for programmers, and emphasizes what hackers really want: easy readability and concise presentation. It’s so much better than what I was using this morning that I can’t believe I lived with Courier for so long.
I know it’s probably a sign of deep rooted psychological instability to get this excited over a font, but I can’t help it! Yay!
UPDATE: I totally forgot to link to his post.
Filed Under (General) by Wenbert on 29-10-2007
Here is how to create a simple Captcha image with the Zend Framework.
I have created this controller named: ImagegeneratorController. The job of this controller is to display images - whether from a database blob or in this case, an image created on the fly.
-
< ?php
-
class ImagegeneratorController extends Zend_Controller_Action
-
{
-
public function init()
-
{
-
$this->_helper->viewRenderer->setNoRender();
-
}
-
-
public function generatecaptchaAction()
-
{
-
-
//Let’s generate a totally random string using md5
-
-
//We don’t need a 32 character long string so we trim it down to 5
-
$security_code = substr($md5_hash, 15, 5);
-
-
$_SESSION[‘captcha_code’] = $security_code;
-
-
//Set the image width and height
-
$width = 100;
-
$height = 20;
-
-
//Create the image resource
-
$image = ImageCreate($width, $height);
-
-
//We are making three colors, white, black and gray
-
$white = ImageColorAllocate($image, 255, 255, 255);
-
$black = ImageColorAllocate($image, 0, 0, 0);
-
$grey = ImageColorAllocate($image, 204, 204, 204);
-
-
//Make the background black
-
ImageFill($image, 0, 0, $black);
-
-
//Add randomly generated string in white to the image
-
ImageString($image, 3, 30, 3, $security_code, $white);
-
-
//Throw in some lines to make it a little bit harder for any bots to break
-
ImageRectangle($image,0,0,$width-1,$height-1,$grey);
-
imageline($image, 0, $height/2, $width, $height/2, $grey);
-
imageline($image, $width/2, 0, $width/2, $height, $grey);
-
-
//Tell the browser what kind of file is come in
-
header("Content-Type: image/jpeg");
-
-
//Output the newly created image in jpeg format
-
ImageJpeg($image);
-
}
-
}
Now, anywhere in my view file (.phtml), I can do this to display the image:
-
…
-
<img src="http://blog.ekini.net/wp-admin/%3C?php%20echo%20$this-%20/%3EbaseUrl.%27/imagegenerator/generatecaptcha%27%20?%3E" alt="" />
-
…
That’s about it. To check if there is are any errors, try to http://yoursite.com/imagegenerator/generatecaptcha - it should display the image. From there you can debug for errors if there are any.
*Update*
Apparently, I forgot to store $security_code variable into a session variable. This value will be used to verify if the user has entered the correct security code after submitting the form.
-
The test form:
-
< form action="/path/to/controller/action" method="post">
-
Captcha Code:
-
<img src="< ?=$this->baseUrl?>/imagegenerator/generatecaptcha" />
-
Enter Code Here:
-
<input name="entered_coded" type="text" />
-
<input value="Submit" type="button" />
-
</form>
Now, in the controller that will handle the form, you will have something like this:
-
…
-
public function handlecaptchaAction()
-
{
-
$entered_captcha = /*get from POST*/;
-
if ($entered_captcha != $_SESSION[‘captcha_code’]) {
-
-
}
-
//rest of the code here…
-
}
-
…
Resources: http://www.webcheatsheet.com/php/create_captcha_protection.php
Thanks to: DanielF and Booster in #zftalk
Post your comments / corrections below. Thanks!
Update:
Hi, just a suggestion, to update your code… For example when a user opens 2 posts with the same image, he won’t be able to post the first opened article (or what ever). I suggest naming the session variable ‘captcha_code’.post_id. And to pass the variable, use /imagegenerator/generatecaptcha/post_id/id.
BY: Tomas on 30 May 2008 at 4:38 pm
Filed Under (General) by Wenbert on 28-10-2007
Design trance has puts on its halloween costume. I love it. Fire and it’s black. \m/ (^_^) \m/.
Filed Under (General) by Wenbert on 28-10-2007
It is Saturday night and it is raining like it hasn’t rained in weeks. So I decided to spend the night to work on my pet project: MyLyricsFinder.com.
It uses the Zend Framework. The latest addition tonight is Zend_Cache. I added the Top 10 Songs and Artist list in the frontpage - so caching it would make sense. Without the cache, every user that will visit the site will have to query the database. Getting the top 10 list from 200K+ rows is pretty heavy on the resources. Not to mention the overhead it will cause on the user’s side. So I came up with the code below:
-
-
//this code snippet is part of my indexAction() in my IndexController
-
-
//for the cache
-
$frontendOptions = array();
-
$frontendOptions[‘lifetime’] = 3600;
-
$frontendOptions[‘automatic_serialization’] = true; $backendOptions = array();
-
$backendOptions[‘cache_dir’] = Zend_Registry::get(‘cache_dir’);
-
-
// getting a Zend_Cache_Core object
-
$cache = Zend_Cache::factory(‘Core’, ‘File’, $frontendOptions, $backendOptions);
-
-
// see if a cache already exists:
-
if(!$result_pop_songs = $cache->load(‘pop_songs’)) {
-
//get most popular songs
-
$dbAdapter = Zend_Registry::get(‘dbAdapter’);
-
$sql = ‘SELECT
-
s.song_title,
-
s.song_slug,
-
s.song_counter,
-
at.artist_slug
-
FROM songs_table s
-
LEFT JOIN album_table al ON s.album_id=al.album_id
-
LEFT JOIN artist_table at ON al.artist_id=at.artist_id
-
WHERE s.song_counter>0 ORDER BY s.song_counter DESC LIMIT 10′;
-
$this->view->pop_songs = $dbAdapter->fetchAll($sql);
-
$cache->save($this->view->pop_songs, ‘pop_songs’);
-
} else {
-
// cache hit! shout so that we know
-
//echo "This one is from cache!\n\n";
-
$this->view->pop_songs = $result_pop_songs;
-
-
}
In addition to the Top 10 List in the frontpage, I have also cached the list of Artist per Letter and the List of Albums per Artist. I have reduced the number of database requests significantly - although I don’t know how much - I can tell the difference in load time with and without the cache.
Code Walkthough
-
-
$frontendOptions = array();
-
$frontendOptions[‘lifetime’] = 3600;
-
$frontendOptions[‘automatic_serialization’] = true;
-
$backendOptions = array();
-
$backendOptions[‘cache_dir’] = Zend_Registry::get(‘cache_dir’);
-
$cache = Zend_Cache::factory(‘Core’, ‘File’, $frontendOptions, $backendOptions);
This part of the code sets up the Zend_Cache. Most likely, yours will be similar to this. Except for the line with the get(’cache_dir’). I have set up Zend_Registry::get(’cache_dir’) in my Bootstrap file to “../tmp/”
-
-
if(!$result_pop_songs = $cache->load(‘pop_songs’)) {
The line above checks if a cache for ‘pop_songs’ already exists. If not, then it will query the database with the code below:
-
-
$dbAdapter = Zend_Registry::get(‘dbAdapter’);
-
$sql = ‘SELECT
-
s.song_title,
-
s.song_slug,
-
s.song_counter,
-
at.artist_slug
-
FROM songs_table s
-
LEFT JOIN album_table al ON s.album_id=al.album_id
-
LEFT JOIN artist_table at ON al.artist_id=at.artist_id
-
WHERE s.song_counter>0 ORDER BY s.song_counter DESC LIMIT 10′;
-
-
$this->view->pop_songs = $dbAdapter->fetchAll($sql);
-
-
$cache->save($this->view->pop_songs, ‘pop_songs’);
The last line will save the query into the cache. So the next time a user will come, the ‘pop_songs’ cache will already exist in the cache and the code below is executed:
-
-
$this->view->pop_songs = $result_pop_songs;
Filed Under (General) by Wenbert on 26-10-2007
When I logged in today, I found this:

Pretty cool. I can now send up to 20MB! Anyways, here are the details:
What is the Power User program?
As one of the few in our Power User program, you can take advantage of some new, complimentary features that we’ve added to your Mail account. It’s our way of saying thanks for being such a loyal user.
What new features do Power Users receive?
* 20MB message capacity. Share more photos and send larger emails with this big boost to your email size limit.
* 24/7 live support online. Get faster answers with real-time support from our customer care team.
* Priority email support. Shoot us an email, and your premium status will move you up in the queue.
How do users qualify for the program?
Power Users are the heaviest and most frequent users of Yahoo! Mail and were selected by Yahoo! to participate in this pilot program. These users spend an incredible amount of time using Yahoo! Mail, so the Power User program is our way of thanking them.
How do I know if I’m a Power User?
Power Users who have opted in to receive communications from Yahoo! will receive an email welcoming them to the program.
Can Power User status expire?
The Power User program is a pilot program and access to these features is available for a limited period of time. Yahoo! may discontinue the program or one’s status as a Power User at any time and without notice. Also, Power User status will automatically end when an account expires or is terminated.
Does the Power User program replace Yahoo! Mail Plus?
No, the Power User program lacks many of the advantages of Yahoo! Mail Plus, such as the absence of graphical ads, POP access, SpamGuard Plus, AddressGuard, stationery, phone support, and no account expiration.
I have been using the same email address since I can remember - way back in the 2MB then 4MB mailbox limits. Hehe.
Filed Under (General) by Wenbert on 22-10-2007
Here is how to get the difference between two dates in PHP.
-
-
…
-
public static function toTimestamp ($date){
-
$arrayDate=self::toArray($date);
-
return mktime($arrayDate[‘hour’], $arrayDate[‘minute’], $arrayDate[’second’], $arrayDate[‘month’], $arrayDate[‘day’], $arrayDate[‘year’]);
-
}
-
-
public static function dateDiff ($date1, $date2, $unit= ‘HOUR’){
-
$date1=self::toTimestamp($date1);
-
$date2=self::toTimestamp($date2);
-
-
$secs=$date1-$date2;
-
-
-
case ‘WEEK’:return $secs/60/60/24/7; break;
-
case ‘DAY’:return $secs/60/60/24; break;
-
case ‘HOUR’:return $secs/60/60; break;
-
case ‘MINUTE’:return $secs/60; break;
-
case ‘SECOND’:return $secs; break;
-
default:return $secs/60/60; break;
-
}
-
}
-
…
-
Usage:
-
-
DateFormat::dateDiff($current_tr->requisition_dateTo,$current_tr->requisition_dateFrom,"DAY"));
-
The static function example above is from an MVC Framework. But you can edit it the sample code to fit your needs.
This tutorial is documented here.
Filed Under (General) by Wenbert on 21-10-2007
Here is an excerpt from a book - Beginning Ajax with PHP: From Novice to Professional.
While the concept of Ajax contains a handy set of functionality for creating actions on the fly, if you are not making use of its ability to connect to the server, you are really just using basic JavaScript. Not that there is anything truly wrong with that, but the real power lies in joining the client-side functionality of JavaScript with the server-side processing of the PHP language using the concept of Ajax.
Throughout this chapter, I will run through some examples of how PHP and Ajax can be used together to design some basic tools that are quite new to Internet applications but have been accessible to desktop applications for ages. The ability to make a call to the server without a page refresh is one that is quite powerful, if harnessed correctly. With the help of the powerful PHP server-side language, you can create some handy little applica-tions that can be easily integrated into any web project.
Why PHP and Ajax?
So, out of all of the available server-side processing languages (ASP, ASP.NET, ColdFusion, etc.), why have I chosen to devote this book to the PHP language, as any of them can function decently with Ajax technologies? Well, the truth is that while any of the afore-mentioned languages will perform admirably with Ajax, PHP has similarities with the JavaScript language used to control Ajax—in functionality, code layout, and ideology.
PHP has been and will likely continue to be a very open form of technology. While code written in PHP is always hidden from the web user, there is a massive community of developers who prefer to share and share alike when it comes to their code. You need only scour the web to find an abundance of examples, ranging from the most basic to the most in-depth. When comparing PHP’s online community against a coding language such as ASP.NET, it is not difficult to see the differences.
JavaScript has always been an open sort of technology, largely due to the fact that it does not remain hidden. Because it is a client-side technology, it is always possible to view the code that has been written in JavaScript. Perhaps due to the way JavaScript is handled in this manner, JavaScript has always had a very open community as well. By combining the communities of JavaScript and PHP, you can likely always find the exam-ples you want simply by querying the community.
To summarize why PHP and Ajax work so well together, it comes down to mere func-tionality. PHP is a very robust, object-oriented language. JavaScript is a rather robust language in itself; it is sculptured after the object-oriented model as well. Therefore, when you combine two languages, aged to maturity, you come away with the best of both worlds, and you are truly ready to begin to merge them for fantastic results.
Client-Driven Communication, Server-Side Processing
As I have explained in previous chapters, there are two sides to a web page’s proverbial coin. There is the client-side communication aspect—that is, the functionality happen-ing right then and there on the client’s browser; and the server-side processing—the more intricate levels of scripting, which include database interaction, complex formulas, conditional statements, and much, much more.
For the entirety of this book, you will be making use of the JavaScript language to handle the client-side interaction and merging it seamlessly with the PHP processing lan-guage for all your server-side manipulation. By combining the two, the sky is truly the limit. Anything that can be imagined can come to fruition if enough creativity and hard work is put into it.
Basic Examples
In order to get geared up for some of the more intricate and involved examples, I will begin by showing some basic examples of common web mini-applications that work well with the Ajax ideology. These are examples you are likely to see already in place in a variety of web applications, and they are a very good basis for showing what can be accomplished using the Ajax functionality.
Beyond the fact that these applications have become exceedingly popular, this chap-ter will attempt to guide you as to what makes these pieces of functionality so well-suited to the Ajax concept. Not every application of Ajax is necessarily a good idea, so it is important to note why these examples work well with the Ajax concept, and how they make the user’s web-browsing experience better. What would the same application look like if the page had to refresh? Would the same functionality have even been possible without Ajax, and how much work does it save us (if any)?
Filed Under (General) by Wenbert on 21-10-2007
I have been using Subversion for almost a year now but I still only know the basics. Add-update-commit, that’s pretty much about it. But lately, I encountered something while coding pet project called MyLyricsFinder.com - I got conflicts. I had no idea how to resolved this so I fired up my iGoogle and then found this subversion crash course.
If you’ve never used a version control system that makes use of conflict markers, the best way to understand them is through an example. Suppose you have a file in your working copy, hello.c, that looks like this:
#include
int
main (int argc,char *argv [])
{
printf (”hello world \n”);
return 0;
}
Then say you change the hello world string to Hello World, and before checking in your changes you update your working copy and find that someone else has already changed that line of the file. The copy of hello.c in your working copy will end up looking something like this:
#include
int
main (int argc, char *argv [])
{
< <<<<<<.mine
printf (”Hello World \n”);
=======
printf (”hello world!\n”);
>>>>>>>.r5
return 0;
}
The < <<<<<<, =======, and >>>>>>> lines are used to indicate which of your changes conflicted. In this case, it means that your version of the section of hello.c that you changed looks like printf (”Hello World \n”);, but in a newer version of the file that has already been checked into the repository, that line was changed to printf (”hello world!\n”);.
Of course, all of this only works if the file in question is in a format that Subversion understands well enough that it can merge the changes automatically. At the moment, that means the file must be textual in nature. Changes to binary files such as image files, sound files, Word documents, and so forth can’t be merged automatically. Any conflicts with such files will have to be handled manually by the user. To assist in that merging, Subversion provides you with copies of the original version of the file you checked out, your modified version, and the new version from the repository, so you can compare them using some other tool.
Click here for the subversion crash course.
Filed Under (General) by Wenbert on 20-10-2007
I got this from linuxscrew.com. Just click the thumbnail below.
Filed Under (General) by Wenbert on 18-10-2007
Here are eight examples of practical PHP regular expressions and techniques that I’ve used over the past few years using Perl Compatible Regular Expressions. This guide goes over the eight different validation techniques and describes briefly how they work. Usernames, telephone numbers, email addresses, and more.
Username Validation - Something often overlooked, but simple to write with regex would be username validation. For example, we may want our usernames to be between 4 and 28 characters in length, alpha-numeric, and allow underscores.
-
-
$string = "userNaME4234432_";
-
-
echo "example 1 successful.";
-
}
Email Addresses - Another practical example would be an email address. This is fairly straightforward to do. There are three basic portions of an email address, the username, the @ symbol, and the domain name. The following example will check that the email address is in the valid form. We’ll assume a more complicated form of email address, to make sure that it works well with even longer email addresses.
-
-
$string = "first.last@domain.co.uk";
-
if (preg_match( ‘/^[^0-9][a-zA-Z0-9_]+([.][a-zA-Z0-9_]+)*[@][a-zA-Z0-9_]+([.][a-zA-Z0-9_]+)*[.][a-zA-Z]{2,4}$/’,
-
$string)) {
-
echo "example 3 successful.";
-
}
IP Addresses – Without pinging or making sure it’s actually real, we can make sure that it’s in the right form. We’ll be expecting a normally formed IP address, such as 255.255.255.0.
-
-
$string = "255.255.255.0";
-
if (preg_match(‘^(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]\d|\d)(?:[.](?:25[0-5]|2[0-4]\d|1\d\d|[1-9]\d|\d)){3}$’,
-
$string)) {
-
echo "example 5 successful.";
-
}
Get the full article here.
|
|