PHP

PHP

Gzip output, minify CSS and JavaScript

Wednesday, February 13th, 2008 in PHP, Tools of the trade | 11 comments

Smaller pages mean smaller downloads and faster page loading. As a programmer, my primary goal is to develop code that parses fast and consumes the minimum amount of resources. Bandwidth and filesize were not a problem that I realized until lately. So, what can you do to reduce filesize and as a consequence to that to use less bandwidth? (more…)

Tags: , , , , ,

PHP read directory

Tuesday, October 30th, 2007 in PHP | 11 comments

The other day I found myself having a problem with not empty directories. I had to delete a directory that had another directory in it that contained a file. Giving the fact that I’m a lazy programmer, I had to find an easy enough solution to use then and every time I needed it without doing much work every time.

So I came up with a class that reads the whole directory, it stores in 2 different arrays all the files and subdirectories (the full path to them, starting with the given directory) and than it deletes them if one wants to do that. If you need such code use the one below. It worked fine for me so it should read the directories for you too. Here goes: (more…)

Tags: , , ,

PHP login

Friday, September 7th, 2007 in PHP, Security | 17 comments

After searching the web for a php login script (and you can find tons of it), I declared myself a little bit unsatisfied. I was looking for something simple yet powerful and I found all sorts of login scripts (a lot of them not safe at all). By the way, as a warning, BEWARE OF THE LOGIN SCRIPTS THAT HAVE SOMETHING LIKE: $_SESSION['has_access'] = true; INTO THEIR CODE followed by the explanation:”and you simply check the pages using if($_SESSION['has_access'])“.They are not safe at all, but that’s another discussion.

So, what was i looking for? Well, as I said before, the login script should be small, reusable, simple to understand and implement, it should check the user on every page (and I mean CHECK like … strip search). Since the www offer was not good enough, I started implementing my own code.

In order to make it reusable, I thought about creating an object to do this task for me. So, what this php login script does it’s actually quite simple: for every secured page it is installed on, it checks using the data stored in session if the user credentials are ok and if they are it returns his/her details from the database. (more…)

Tags: , ,

PHP header 500 – manage your mySQL errors

Tuesday, August 21st, 2007 in MySQL, PHP | 1 comment

From time to time problems appear. mySQL server crashes, some error in a sql query might think it’s time to show her ugly face and in top of all that, the all mighty Google comes to index your page. And when he does, he indexes a page that presents an error or a blank page. How can we stop him from indexing that page? With a header 500 error.

The header 500 error or “Internal Server Error” means that the server has a nasty indigestion and the search engine should come back later to see the page in all it’s splendor. And that’s exactly what it will do. So, how do we trigger that error?

Most scripts use the mysql_query(“some sql”)or die(mysql_error()) sintax. Well, that’s ok because in case of an error, the script stops and the error appears so the debug process can begin. The only problem is that if some error appears, it will be indexed by search engines. (more…)

Tags: , , ,

Watermark all images in a folder

Monday, August 20th, 2007 in PHP | 14 comments

I was checking out the searches from this blog and one in particular caught my eye. It was something like “watermark images from folder“. I remember that not a long time ago I had the same problem.

One of the clients I work for, in the development stage, asked me not to watermark the images of his products because… I really don’t remember the reason. What I do remember is that at some later point, he changed his mind (the website was online with a few hundred products added) and now he wanted to watermark images to prevent theft (some of his competition borrowed them).

So, I came up with 2 solutions. The first one was to watermark the images and save them on his server (a bulk watermark, while reading the whole directory) and another one was to watermark on demand. (more…)

Tags: , , ,

Watermark image

Monday, July 23rd, 2007 in PHP | 12 comments

watermark Do you need to watermark your images? If so, do it the smart way. Put your mark on them by resizing the watermark. If you upload images having different sizes, you’ll love this. Read on.

Over the time I had clients that wanted to put a watermark on their images for copyright purposes. But there was a little problem. The watermark was too small on bigger images and too big on smaller ones. What to do… what to do?! Resize the watermark of course! (more…)

Tags: , ,

Image titles generated with PHP and GD

Saturday, July 21st, 2007 in PHP | 1 comment

image titles generated with php Some time ago, a client came and asked specifically that the headers (h1 stuff) should use his particular font specified in his identity manual. At that time I did now knew about the flash possibility to overwrite text (search for sIFR.js and you’ll see what I mean), so I chose to dynamically generate these titles using and GD. The reason for this solution (instead of changing them using some image editing software) was that there was some dynamic data (a list of newsletters) that needed the same title styling. (more…)

Tags: ,

Simple PHP math captcha image

Friday, June 22nd, 2007 in PHP | 52 comments

math captcha image There are tons of captcha scripts on the web. The problem is that, from the enthusiasm of making them more flexible, a lot of code and pages are added and instead of a simple solution one may end up with several files necessary to display the captcha image. I’m not saying that they are useless, but for most of the time, such complexity is not quite welcomed. I strongly believe that less is more and that things should be kept simple. Having that in mind, I want to share a way to protect your forms using a math captcha image that requires a single file and a font file.

This is a very simple, easy to understand example that shows you how to display a math captcha image that requires the user to do a little thinking. Simple operation like subtraction or addition is displayed on the math captcha and the user needs to input the result. Math captcha result is stored in session and from the page doing the form validation you just need to compare the value from session with the value that the user inputed. (more…)

Tags: ,

Simple PHP-GD captcha image

Thursday, June 21st, 2007 in PHP | 13 comments

If you need to secure a form from being automatically submitted by robots of by someone who wants to damage your server and/or scripts, it’s best that you use a captcha image.
For this one you’ll need compiled with GD and the FreeType Support enabled (in order to use True Type fonts in your images). (more…)

a few things you must never do when quering database

Tuesday, June 19th, 2007 in PHP | no comments

I’m putting this in here because I’ve done it myself and I’ve seen it done by others.
NEVER EVER use mysql_query() in a loop (like while(), for(), foreach() … ). It will slow down your script and drive you nuts. It’s the wrong way of doing anything you have in mind. (more…)