PHP Reference

PHP formatting retrieved mysql datetime

to format a mysql datetime to your needs use something like:

Zend Framework Access view values from partialLoop

Partials are handy because they allow you to dissect html code and template logic into smaller reusable bits. PartialLoops allow you to loop through objects and arrays in an efficient manner and in a seperate variable scope.

Sometimes it is neccessary to access variables and their values from the view object's scope that the partial was called from.

This can be done quite easily from the by calling the partialLoop() or partial() (or another view helper's) method from the partial template and then accessing the view object from that.

PHP Thumbnail class

This PHP class creates thumbnails of jpeg, jpg, png or gif files. It uses GD and PHP. The thumbnails are saved to a specified location on disk after creation.

If only width or height of the thumbnail are specified, the image is scaled maintaining correct proportions. If both width and height of the thumb are specified, proportions are disregarded.

Also the 'maxLength' argument can be used to just specify the maximum length of the longest side of the thumbnail so that either width or height are scaled to the specified pixel value based on which is longest. In this case proportions are kept.

PHP configuration Control session length

Use the following to set the necessary ini values that allow you to control time before php sessions are garbage collected

Zend Framework Zend db examine SQL query

You can use Zend_db_profiler to capture and examine the sql queries or statements that Zend db adapter forms and executes. example:

Zend Framework Retrieve request params

To get the request parameters in a Zend framework controller action you can use:

PHP Generate random password with php

This function will generate a random password string. With the length argument you can controll the number of characters returned.

PHP configuration Turn on error reporting - override ini settings

To dynamically turn on error reporting inside a php script, even if it is disabled in the php.ini file you can use the following functions.

Zend Framework Register plugins in ZF application.ini

To register frontController plugins in the ZF (1.8 and higher) application.ini file you can use the following syntax:

Zend Framework Set constants in ZF application ini file

To be able to set application wide constants inside the application ini file you can define a function as follows in the bootstrap class:

Zend Framework Change view script in ZF controller action

To change from the default view script (based on the action's name) and dynamically pick another view script to render you can use the following code:

Zend Framework Quick setup of navigation menu on every page

Just add the following to layout.phtml (or the file of your layout template is in)

Zend Framework Disable layout and viewRenderer in controller

To disable the layout and the viewrenderer on the fly inside a controller you can use the following lines :

Zend Framework Set response segment in controller actions

The default response segment your controllers action are outputting to is 'content'.

To change/set what response segment is written to inside your controller actions you can use the setResponseSegment method of the viewrenderer helper:

PHP Check email adress with regular expression

There' s a lot of regular expressions out there to match all possible email addresses out there. This is just an unrestrictive and extremely simple example of using PHP's preg_match function to check an email address. It will certainly resolve to true for any existing emailaddress. Perfectly sufficient for most general purposes

PHP Securing file uploads in php

PHP file upload scripts, if not properly secured pose a big threat to your webserver, as they potentially allow attackers to run any shell command on it. Although it's quite simple to add an image upload script to your site, inexperienced php scripters often tend not to take the security risks to seriously. For a nice explanation of potential risks of file upload scripts have a look at this document (pdf) on php file uploads from scanit.be.

Here's a few precautions that should be taken :

PHP configuration Quick manual install of PHP on windows IIS 6

  1. Download binaries and extract to C:\php (http://www.php.net/downloads.php)
  2. To make php4ts.dll / php5ts.dll available add your PHP directory, C:\php to the PATH. Read more about how to add your PHP directory to PATH in the corresponding FAQ entry (and then don't forget to restart the computer - logoff isn't enough).
  3. Rename php.ini-recommended to php.ini and copy to location where php can reach it - http://php.net/manual/en/configuration.file.php
  4. Edit php.ini :

    extension_dir = C:\php\ext

    and uncomment the extensions you need - for instance

    extension=php_gd2.dll

    session.save_path = "2;C:\tmp"


  5. The IIS user needs permission to read various files and directories, such as c:\php, php.ini, docroot, and the session tmp directory.
  6. Open IIS manager - rightclick & open the properties of your website. Under ISAPI Filters, add a new ISAPI filter. Use PHP as the filter name, and supply a path to the php4isapi.dll / php5isapi.dll.
  7. Under 'Home Directory' change the Execute Permissions to 'Scripts only'
  8. Click on the 'Configuration' button, and choose the Application Mappings tab. Click Add and set the Executable path to the appropriate ISAPI DLL. An example PHP 5 value is: C:\php\php5isapi.dll Supply .php as the extension. Leave 'Method exclusions' blank, and check the 'Script engine' checkbox. Now, click OK a few times.
  9. Stop IIS completely (NET STOP iisadmin)
  10. Start IIS again (NET START w3svc)
  11. With IIS 6 (2003 Server), open up the IIS Manager, go to Web Service Extensions, choose "Add a new Web service extension", enter in a name such as PHP, choose the Add button and for the value browse to either the ISAPI file (php4isapi.dll or

    PHP Alternative syntax php foreach

    Alternative syntax for foreach loops in PHP:

    PHP display errors (override ini settings if neccessary)

    The following to display errors regardless of the settings to do with this  in the php.ini file

    Smarty Smarty foreach loop example

    Smarty code:

    PHP 301 redirect

    The 301 redirect is the most efficient and search engine friendly method for webpage redirection. If you have to change file names or move pages around, it's the safest option.

    The correct way to do this redirect using php headers:

    PHP PHP class - ajax bridge

    This is a very simple bridge script that enables you to direct ajax calls directly at PHP classes.
    Do consider security when using this.

    PHP Simple PHP captcha script

    A very basic PHP / GD based captcha script for stopping those spammers from flooding your guestbooks, mailforms or any other online application susceptible to spam spawning web bots.spam

    You can also download the captcha script here

    Requires only PHP with GD library installation and a true type font file(.ttf) to create the image

    First you make the actual captcha image generating file, and save it as (for instance) 'captcha.php':

    To see an example of this script in a live site - have a look at this guestbook. Or just leave a comment below this article

[x]