Friday, June 30, 2006

Global Include Trick

As the major part in my PHP Application Architecture, this trick allows you to include a global file at the top of your scripts. It has the benefit of not keeping the file location or name inside of each individual PHP script.

I was able to accomplish this by using Apache's SETENV directive. It registers a variable visible to all scripting languages through either an entry in either an .htaccess or httpd.conf file. Then in each of my PHP scripts I include that variable's contents.

A very handy trick, if you ask me.
Source:


/*
* This goes into the Apache configuration file (or .htaccess)
*
* SetEnv GLOBAL /mypage/local/application.php
*
* Then put the below PHP into each of your scripts... enjoy!
*
*/

require(getenv("GLOBAL"));

?>

Monday, June 12, 2006

Creating a CAPTCHA with PHP

You may be thinking just exactly what is a captcha? Well you are likely to have already seen them across the web. They are those little images with a code on the front that you type into a box in order to submit something. This kind of system helps to prevent automatic submitting of an operation by some kind of program or robot. In this tutorial I will show you how to make a CAPTCHA just like the one below. Its not the most advanced captcha available because it uses a simple system font and nothing more.




The lines that you see above are to make any robots job of trying to work out that code a little harder. The dots in the background also help with this. Follow the link to learn more about how to create CAPTCHA.

Saturday, June 10, 2006

Graphs generation using php

Click on the image for more info

File Download Security

File Download Security
Want to prevent people from linking to your downloads? This script will force a page to be loaded before the download starts. HTML header statements are used to trigger the download of the file. PHP is used to push the file to the browser.
Principles
HTML headers must be sent before any output is sent to the browser. PHP uses the header function to pass raw HTML headers.

$dir="/path/to/file/";
if (isset($_REQUEST["file"])) {
$file=$dir.$_REQUEST["file"];
header("Content-type: application/force-download");
header("Content-Transfer-Encoding: Binary");
header("Content-length: ".filesize($file));
header("Content-disposition: attachment; filename=\"".basename($file)."\"");
readfile("$file");
} else {
echo "No file selected";
}
?>

We started with setting the directory where the files to be downloaded are located in $dir. Be sure not to use \ in $dir. Then we checked to make sure a filename was specified in the request. If a file was specified then we set $file to the path to the file and the filename. Now that the prep work is done its time to send the file to the browser.
The first header statement tells the browser to expect a download. The next two header statements tell the browser the format of the data and the size of the file respectively. The last header statement tells the browser the name of the file. Finally the readfile statement sends the file to the browser.

PHP User Group for Bangalore


We had our very first Bangalore PHP meetup last Friday. We had a bunch of PHP people join up at our company terrace, to get the momentum going on meeting up PHP professionals around Bangalore. One of the key points at the discussion was to start a PHP user group in Bangalore, since there wasn’t one existing already (yep, the IT city doesn’t yet have a PHP user group). As a first step, we’ve setup a mailing list over at Yahoo groups to enable communication between the users. If you’re interested in joining the group, you can subscribe bang-phpug-subscribe@yahoogroups.com. This is still a new group so we’re still getting the momentum going with discussions and memberships, so do contribute to the discussions over at the group.



Check the following meetup.com page regarding the meeting:

http://php.meetup.com/329/