background preloader

15 very useful PHP code snippets for PHP developers

15 very useful PHP code snippets for PHP developers
Following are list of 15 most useful PHP code snippets that a PHP developer will need at any point in his career. Few of the snippets are shared from my projects and few are taken from useful php websites from internet. You may also want to comment on any of the code or also you can share your code snippet through comment section if you think it may be useful for others. 1. Send Mail using mail function in PHP 2. 3. The above code will not work in case your client is behind proxy server. 4. This function will return the duration of the given time period in days, hours, minutes and seconds. e.g. secsToStr(1234567) would return “14 days, 6 hours, 56 minutes, 7 seconds” 5. 6. Required Extension: SimpleXML 7. 8. Following is the PHP code to create the JSON data format of above example using array of PHP. Following code will parse the JSON data into PHP arrays. 9. 10. This basic snippet will create a random authentication code, or just a random string. 11. Validate a date in “YYYY-MM-DD” format.

10 code snippets for PHP developers « The html blog I’ve compiled a small list of some useful code snippets which might help you when writing your PHP scripts… Email address check Checks for a valid email address using the php-email-address-validation class. Source and docs: 01.include('EmailAddressValidator.php'); 03. 04.if ($validator->check_email_address('test@example.org')) { 07.else { Random password generator PHP password generator is a complete, working random password generation function for PHP. 01.function generatePassword($length=9, $strength=0) { 02. 03. 04. if ($strength & 1) { 05. 07. if ($strength & 2) { 08. 10. if ($strength & 4) { 11. 13. if ($strength & 8) { 14. 17. 18. 19. for ($i = 0; $i < $length; $i++) { 20. if ($alt == 1) { 21. 22. 23. } else { 24. 25. 28. return $password; Get IP address Returns the real IP address of a visitor, even when connecting via a proxy. 01.function getRealIpAddr(){ 02. if (! 04. 06. elseif (! 08. 10. else{ 11. 13. return $ip; XSL transformation 01. 04. 05. 08.

What's the Right Way to Prevent SQL Injection in PHP Scripts? - Brian Swan How to prevent SQL injection in PHP scripts is probably a topic that doesn’t need anything more written about it. It is pretty easy to find blog posts, documentation, videos, etc. that explain the importance of preventing SQL injection and suggestions for preventing it. In fact, I’ve already written a post on this topic as a guest writer on the SQL Server Driver for PHP team blog. However, it is important to have fresh information for new Web developers and I don’t necessarily agree with some of the most common suggestions for preventing SQL injection. (Besides, I hear that this is the Month of PHP Security.) So, this will be yet another post about preventing SQL injection, but I will offer my 2 cents about what I think is the right way to prevent it. SQL injection (or a SQL injection attack) occurs when a user provides SQL code as user input for a Web page, and the SQL code is then executed in the database. Now consider the form with the following inputs: Thanks. -Brian

7 Secure, Lightweight, and Easy to Use PHP Frameworks Choosing a good PHP frameworks can help you develop complex Rich Internet Applications quickly, with a best practices oriented approach, and saving a lot of time reusing code snippets that are already available. There are a lot of interesting PHP frameworks you can choose for your next web project. Which framework you choose is really a personal decision. There is no one “best” framework on the market at the moment. Different frameworks are better for different types of projects, and for different developer. Today we will focus on 7 secure, lightweight and easy to use PHP Frameworks. Why Use a PHP Framework? There are plenty of reasons to use a PHP Framework to build your websites. 1. Below we’ve covered some of the best frameworks available today. 1. CodeIgniter is a powerful, high-performance, open-source PHP framework that helps you author PHP applications rapidly. CodeIgniter has an exciting online manual, a couple of helpful video tutorials and an active user forum. 2. 3. 4. 5. 6. 7.

extremely useful PHP classes PHP PSD Reader A few weeks ago, I wrote an article about this PHP which allow you to display any Adobe PSD file on screen. Very usefull to create preview of PSDs designed for clients, for example.Download Browser detect One of the most common (and boring) problem for front-end developers is definitely cross-browser compatibility. This PHP class will detect almost all browsers and simplify your cross-browser work.Download Akismet Remember those days without spam? ADOdb The large majority of websites and web apps are using databases to store all kinds of data. HTML Purifier As it name tells, HTML Purifier is a PHP class created to help you writing a better code. Google charts API Charts are very useful and highly asked by clients, but they can be a lot of work. pChart pChart is another chart class, and it is as good as Google charts API. PHP Excel Excel documents are highly popular in the corporate world. Country from IP Cache Manager WPGet

PHP command line syntax checking Posted April 24th, 2010 in PHP The PHP CLI (command line interface) allows basic syntax checking of a PHP file. I'll show the basic usage of this in this post and then a couple of ways to check all files in a directory, and all files recursively down the directory tree as posted by a couple of people on Twitter. Syntax checking a single PHP file from the command line The syntax for checking is like this, where the -l flag is a lower case L: If the syntax in the file is correct you will see this: If there are errors then theses will be listed as in the following example output: Note that the syntax checking only checks for syntax errors - it does not report the usage of variables which have not yet been defined, functions or class which have not be declared etc. So something like this will pass: but these will not: Syntax check all PHP files in the current directory

Still life: Bent objects » Article » OWNI.eu, Digital Journalism UPDATE: The Return of Bent Objects Wires transform these objects from inanimate to hilarious works of art. Little polish girl McDonalds as Sculpture Materials Yeah, this is where those come from Dancing Queens English breakfast Sylvia Muffin put her head in the oven. The introvert Bananas in bed – let’s slip into bed together You Say Tomato, I Say Tomahto. Fruit with life experience Zombies are nuts about brains Modest pear Literary interpretations Paper training our little dog, Frank A little cat doodle Photo Credits: Terry Border at Bent Objects View more In Pictures sets on Owni.eu Simple Layouts with PHP This tutorial is for those starting out in PHP and want to learn a great way to create layouts. This will help reduce redundancy and keep you from writing too much code. Download the Source Anyone that has been developing HTML based sites for some time has undoubtedly ran in to the issue of keeping the design consistent across multiple pages. Technique #1 A lot of sites follow this kind of layout scheme The first technique is the entry level solution that works across nearly all situations. <! Take out everything up to the point and put that in a new file called head.php. <? Now just put whatever HTML/PHP content you want in between those two include statements and check it out in a browser. It may be easier to think of it as a puzzle, you are piecing together a complete HTML page with includes. Technique #2 Alright we have that under our belt let’s look at another solution. <! This will be your index.php, but the content will change based on the links the user goes to. Conclusion

Twig - The flexible, fast, and secure template language for PHP PDF Generation with Zend Framework | LiveDocx in PHP - Zend_Service_LiveDocx Generating print-ready well-formatted PDF documents with PHP is not an easy task. Traditionally, there are two main approaches to PDF generation with PHP. Given sufficient time and patience, both partially get the job done, but still leave a lot to be desired: HTML-to-PDF: This approach is widely used in mainstream applications. Programmatic: This approach offers total control of the resulting PDF. A completely new approach In this article, the author presents an entirely new, third approach. Before we delve into a technical discussion on the inner workings of this new approach, let us first take a look at a practical example. The code demonstrated in this article ships with Zend Framework 1.10 or newer. Introducing LiveDocx LiveDocx is a SOAP-based document generation service, based on the market-leading, word processing component TX Text Control .NET. Templates and documents Throughout this article, we refer to the terms templates and documents. Using LiveDocx and Learn more Footnotes

Object passing (with and without serialization) in php - This article will show you how to handle in a proper way a very common situation: a user logs in your site and you want to keep trace of his details while he browses around your site using just one Imagine you have done a site (an e-commerce one e.g.) and you have a nice login page. Your users will insert their username and password and if the data they insert is correct, they are redirected e.g. to the catalog page where they can buy your products. Many of us do this almost everyday buying stuff from many different sites. Ok, how would you handle variable passing? // we have surely remembered to put // session_start(); // at the beginning of our .php page // process user input here if (user has inserted correct vals) { // retrieve firstname, lastname, id, username from database and store them // retrieving code here $rs=mysql_fetch_array($qry); session_register('username'); session_register('firstname'); session_register('lastname'); session_register('id'); $username=$rs['username']; $firstname=$rs['firstname']; $lastname=$rs['lastname']; $id=$rs['id']; // now in every page where we put session_start() we're able to access this 4 vars } ? Nice huh? Very simple class. ?

Related: