JavaScript
Article

10 Code Snippets for PHP Developers

By Sam Deering

The list below is a compilation of list of some useful code snippets which might help you when writing your awesome PHP scripts.

Also: See more PHP Scripts


1. Email address check


The aim of this project is to create an email address validation class (or function) that checks for technical validity1 in email addresses, the idea being then that a site can send an email to that address to verify ownership.
Email-address-check.jpg
Source


2. Random password generator


Is a complete, working random password generation function for PHP. It allows the developer to customize the password: set its length and strength. Just include this function anywhere in your code and then use it.
Random-password-generator.jpg
Source


3. Get IP address


Returns the real IP address of a visitor, even when connecting via a proxy.
Get-IP-address.jpg
Source


4. XSL transformation


This tutorial is for developers who wish to know how to generate HTML documents using a combination of XML data and XSL stylesheets. PHP5 version.
XSL-transformation.jpg
Source


5. Force downloading of a file


Forces a user to download a file, for e.g you has an image but you want the user to download it instead of displaying it in his browser.
Force-downloading-of-a-file.jpg
Source


6. String encoding to prevent harmful code


Web applications face any number of threats; one of them is cross-site scripting and related injection attacks. The Reform library attempts to provide a solid set of functions for encoding output for the most common context targets in web applications (e.g. HTML, XML, JavaScript, etc)
String-encoding-to-prevent-harmful-code.jpg
Source


7. Sending mail


Using PHPMailer. PHPMailer a powerful email transport class with a big features and small footprint that is simple to use and integrate into your own software.
Sending-mail.jpg
Source


8. Uploading of files


Using class.upload.php from Colin Verot.
uploading-files.jpg
Source


9. List files in directory


List all files in a directory and return an array.
list-files-in-directory.jpg
Source


10. Querying RDBMS with MDB2 (for e.g MySQL)


PEAR MDB2 provides a common API for all supported RDBMS.
Querying-RDBMS-with-MDB2.jpg
Source

  • Pingback: 10 Code Snippets for PHP Developers » PHP Script Blog()

  • activeseven

    Nice collection with some useful tips. Particularly the IP address was off use to me. Thanks for that. Though I am uncertain why not just use filter_var( $email_address , FILTER_VALIDATE_EMAIL ); instead of your emailAddressValidator.

    Thanks for sharing :)

  • http://www.examplecode.org Jim

    The email validation code was pretty cool but can’t you do this quicker using simple regular expressions?

  • Pingback: Tweet-Parade (No.35 Aug-Sep 2011) | gonzoblog.nl()

Recommended
Sponsors
Because We Like You
Free Ebooks!

Grab SitePoint's top 10 web dev and design ebooks, completely free!

Get the latest in JavaScript, once a week, for free.