PHP

PHP

Install additional PEAR modules on Mediatemple dv 3.5

Friday, March 20th, 2009 in PHP | 4 comments

mediatemple dedicated virtual I have a MediaTemple dv 3.5 hosting account that I like a lot (MT support was great whenever I had an issue). Today I needed to install PEAR on one of my domains. To do that, I followed the tutorial from their KnowledgeBase ( please read it if you want to do the same thing; it’s available here ) and all went well for the domain I used ( I had to make a few tries to get it going – I’m a noob in this ).

My goal was to make this work on the main domain, although I needed this on a subdomain. Pear was installed successfully, but I needed Spreadsheets and OLE in order to make things work as planned. Unfortunately, they were not available.

To add them, I created in the httpdocs folder ( using a FTP client ) a folder called PEAR_modules and uploaded Spreadsheets and OLE source files. After that, I connected to my server ( SSH using putty ) and edited vhost.conf for that domain ( in the same way as the MediaTample tutorial explained ) by adding the include path to my PEAR_modules folder. (more…)

Tags: ,

JavaScript chained select with MooTools 1.2

Friday, January 23rd, 2009 in Mootools, PHP | 17 comments

MooTools chained select ChainedSelect is a MooTools 1.2 JavaScript plugin that enables the user to navigate through parent – children tree structure with indefinite number of nodes and make a selection when the desired option is encountered.

This approach offers a more powerful solution versus the classical drop down boxes chained together for a number of reasons: it takes significantly less space in the page; it can contain any number of nested levels, navigation would not be harmed; it can be styled according to the overall page design from CSS and most of all, it looks better.

The purpose of the plugin is to replace the classic chained dropdown lists that have 2 or more selects and get populated with options once you select an option from a previous one.

The whole idea behind this plugin evolves around the tree structure generated by a file and sent to JavaScript as a JSON string. Giving the fact that the whole structure of the tree is loaded, the request to the script is made only once and then the response is reused when navigating the nodes. Once a node is selected, the plugin allows the user to return on the path he used to get to the current node.

If an option is selected and the script that verifies the user’s entry returns an error (for example the e-mail address is invalid), the plugin offers the possibility to display the last selected option leaving it selected and accessible in POST / GET depending what method you used on the form. (more…)

Tags: , ,

Write more compact php code

Thursday, July 10th, 2008 in PHP | 4 comments

I’ve been programming using and MySQL for about 4 years by now. One thing that I’ve always hated was to write code. Don’t get me wrong, I love coding, what I hate is writing in 10 lines what I could do in 5. Besides the fact that is a terrible waste of time and effort, when I was put in the position of revising the code at a later time (to make changes or just for the sake of improving the code) I found myself in trouble trying to understand what the hell I was thinking back then. Another scenario is when someone else is taking what you started ( I know this because I had to take on someone else’s projects ).

Giving this, why not spend a little time thinking first and then start coding? Below are a few things I’ve found out with time. (more…)

Tags: , , ,

Output folder content in XML file

Thursday, July 3rd, 2008 in PHP | 39 comments

xml output Usually in a Flash gallery the images are shown using an XML file generated by the server (in our case using ). So, how do you create that XML file used to output the contents of your images folder?

Starting with 5, we have some new tools added that ease out work. One in particular is of interest and that’s SimpleXML. In plain english, it offers the possibility to convert XML to an object and that may be used later in the code with simple array iterators. More than that, it can also output XML valid code to generate XML files.

For this example, besides SimpleXML, we’ll need one more thing: a way to read the folder containing the images. And for that we have readdir. So the process will go like this: first we need to read the entire directory with readdir() and after that we need to generate the XML output using SimpleXML. Remember, SimpleXML is available in starting with version 5, so make sure you have it installed on your server.

To check your server version, simply create a file called phpinfo.php and inside it put only this: <?php phpinfo(); ?> – after that, upload the file on your web hosting service. Open your favourite web browser and point it at this file ( www.mywebaddress.com/phpinfo.php ). The first this you’ll see will be the server version ( something like Version 5.x.x ). Remember to delete the file after you’re done. (more…)

Tags: , , , , , ,

PHP highlight search keywords

Friday, April 11th, 2008 in PHP | 7 comments

php highlight keywords While performing a search, it’s ofter very useful to highlight in different colors, backgrounds or styles the search keywords in the results returned. This keeps focus on the keywords and helps scan the page faster to find the more relevant results.

But how exactly can that be done? As a basic idea, the following code ( a class ) practically takes as parameters the text that needs to be highlighted and the particular keywords that need to stand out. Using a regular expression, every keyword in the text is replaced with a span HTML element having different styles applied according to the keyword it matches.

Please remember that the code below is just an example, it can be and should be extended; I’m only presenting it for demonstration and “how to” purposes. (more…)

Tags: , ,

Gzip output, minify CSS and JavaScript

Wednesday, February 13th, 2008 in PHP, Tools of the trade | 10 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 | 10 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 | 11 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 | no comments

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 | 8 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: , , ,