PHP Reference

PHP Zend Captcha image example

This is just a quick example of how you can deploy Zend Framework Captcha image component in a form.

Zend Framework use HTML in Zend Form Label

To use HTML in a Zend Framework Form standard label you will first have to set the escape option in the relevant Label decorator to false. Like so:

Zend Framework Use Zend mail with Zend view

Here´s an example combining Zend mail and Zend View for easy mail templating.

Zend Framework set selected Zend Form select

To set the selected value of a select element in a Zend Form object you need to use it's setValue method:

PHP Write post data to XML

Here's a short script that will receive data from a form and turn it into an xml file, translating the posted key value pairs to xml nodes with textnodes.

PHP Quick PHP write to file example

The following code tries to open a file , creates it if it doesn't exist, clears it if it does excist (!), then writes to it and closes it again.

PHP Simple RSS proxy script PHP

The first line of following php code produces an http header signifying that the content will be an xml file.

The second will simply retrieve the file or url and print out the contents.

PHP Text to image

This simple code example will open an image file and put the text specified in the GET parameter to via GD

Zend Framework using MySQL NOW() expression in Zend db insert statement

To insert the current date / time MySQL offers the convenient NOW() function that can be used in an insert query. To use this same function inside a Zend framework db adapter insert statement you can use the following syntax:

Zend Framework Change layout file from controller action

To choose another layout file inside your controller all you need to do is invoke the layout helper like so:

Zend Framework access view inside custom view helper

To access the view instance from your homemade view helper you just need to create a setView method and view property. This will be automatically called upon instantiation, setting the view variable to point at the view instance.

Example:

Zend Framework Simple Zend Router example

The default routing setup for Zend is ':module/:controller/:action/*'

Zend Framework Get baseurl inside a Zend Form descendant class

Often when using the Zend Form components it's convenient to Extend the Zend Form class. To set the action to of the form it will be necessary to access the base url helper method of the view object.

Because you don't have direct access to the view object from inside a Zend Form object, you have to do something like this:

PHP Execute mysql .sql dump files via php mysqli

The following script allows you to execute an sql dump file (for instance produced with mysqldump or phpmyadmin).

Zend Framework Get Zend Db Adapter instance

If you're using Zend Framework 1.8+, and created your project with the command line tool, then it's as simple as registering your database settings in your application.ini config file.

PHP Wordpress add body_class using filter and conditionals

The Wordpress body_class function prints out classes to your body tag for use in CSS and JavaScript.

You can filter it to add your own custom body classes based on conditionals.

If you want to access the post inside the function you have to declare can refer to a global inside you function.

This is an example that adds a 'withheader' className to the body of the twentyten template based on whether an image is specified.

Add it to your theme's functions.php

PHP Wordpress cheat sheet

Template tags related to setting / getting paths and directories:

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]