Prevent form submissions with javascript disabled

One of the first steps in validating form data is with JavaScript. And with JavaScript disabled you are open to possible invalid input data. Of course everyone further validates the form data on the server, but that entails a round trip if errors are encountered. In many situations it is just better to prevent any form submissions from the browser itself. The following few tips will enable you to quickly disable form submission if JavaScript is disabled in the browser.
(more…)

Read More

Tail functionality in PHP

Frequently one needs to get the last few lines of some log files, whether php error logs or Apache logs. Most of these file sizes run into megabytes, which makes it difficult and time consuming to remotely open them using ftp. If you have shell access then you can easily use the *nix ‘tail’ command to return the last ‘n’ lines; or one can use the following if shell access is not enabled on your hosting.

function Tail($filename) {
  return system('tail -n 20 '.$filename);
}

(more…)

Read More

Searching server files using GREP

During debugging the majority of my time is spent in searching for specific text. Searching on the local machine is a breeze via a IDE, but if I’ve an application hosted on a remote server then it can get difficult and slow, unless of course you have access to SSH when you can use good-old Grep. But many times the server does not have SSH access at which time I have to rely on using Linux commands though PHP.

So for example the following will allow me to recursively search all the files in a directory for the string ” mail”. I just upload the file to the server directory from where I need to make the search. Here I’m using the PHP backtick operator that allows me to escape to the shell, execute the specified command, and return the result in the variable $output.
(more…)

Read More

Fixing aspect-ratio of images in PHP

Frequently when you are displaying images on a page using a fixed width and height , the images come out stretched or squeezed. This is because the aspect ratio of the images have not been maintained. In such a case you can use the following script to correctly display the images according to the aspect-ratio of the image without actually resizing the image in a editor. This can be handy when you need to quickly correct image distortions. Here you need to keep either the width or the height fixed, so the other dimension can be calculated. Below the width is kept fixed at 200px, and the height is varied depending on the aspect ratio of the image, thus displaying the image without any distortion.

<?php
 
$imageFile = 'path/to/image';
 
/* Set the width fixed at 200px; */
$width = 200;
 
/* Get the image info */
$info = getimagesize($imageFile);
 
/* Calculate aspect ratio by dividing height by width */
$aspectRatio = $info[1] / $info[0];
 
/* Keep the width fix at 100px, 
   but change the height according to the aspect ratio */
$newHeight = (int)($aspectRatio * $width) . "px";
 
/* Use the 'newHeight' in the CSS height property below. */
$width .= "px";
 
echo "<img style=\"width: $width; height: $newHeight;\" 
       src=\"$imageFile\" />";

Read More