Sunday, May 13, 2012

10 ESSENTIAL PHP CODE SNIPPETS YOU MIGHT BE LOOKING FOR


PHP is a scripting language that is perfect for developing dynamic web pages and applications. Being one of the most popular scripting languages on the internet today, makes it pretty easy to get started with. If you are looking for a technique that you forgot about, or a function you didn’t know someone else has created before; then this article might be something useful for you and will save you some time for sure. Today we will highlight 10 Essential PHP functions with step by step tutorialsto get you some of the latest techniques used these days.

1. USING PHP TO BACKUP MYSQL DATABASES

If you website stores its data in a MySQL database, you will most definitely want to backup that information so that it can be restored in case of any disaster (we all have been there). There are different ways to backup your MySQL Database, but today we will only focus on how to use PHP to backup your MySQL database.

DIFFERENT WAYS TO BACKUP YOUR MYSQL DATABASE:

  • 1. Execute a database backup query from PHP file.
  • 2. Run mysqldump using system() function.
  • 3. Use phpMyAdmin to do the backup.

1.1 EXECUTE A DATABASE BACKUP QUERY FROM PHP FILE

Below is an example of using SELECT INTO OUTFILE query for creating table backup :
<?php
include 'config.php';
include 'opendb.php';

$tableName  = 'mypet';
$backupFile = 'backup/mypet.sql';
$query      = "SELECT * INTO OUTFILE '$backupFile' FROM $tableName";
$result = mysql_query($query);

include 'closedb.php';
?>
To restore the backup you just need to run LOAD DATA INFILE query like this :
<?php
include 'config.php';
include 'opendb.php';

$tableName  = 'mypet';
$backupFile = 'mypet.sql';
$query      = "LOAD DATA INFILE 'backupFile' INTO TABLE $tableName";
$result = mysql_query($query);

include 'closedb.php';
?>

1.2 BACKUP YOUR DATABASE INTO AN XML FILE USING PHP

If you want to have XML backup because it’s easy to read, here’s a PHP snippet that outputs your database as XML.

FURTHER RESOURCES

2. USER MEMBERSHIP AREA WITH PHP

No matter where you go on the internet, there’s a staple that you find almost everywhere – user registration. Whether you need your users to register for security or just for an added feature, there is no reason not to do it with this simple tutorial. In this tutorial we will learn the basics of user management, ending up with a simple Member Area that you can implement on your own website.
User Membership Area With PHP

3. ASYNCHRONOUS COMMENTS WITH PHP, JQUERY, AND JSON

In the public forum that is the blogosphere, the ability to capture and display visitor comments on your blogs can give you instant feedback and opinions from the people that matter most – those that read your blog. In this article, we’re going to look at how we create a simple but effective means of capturing and displaying visitor comments using a blend of jQuery, PHP and JSON.
Asynchronous Comments with PHP, jQuery, and JSON

4. USING PHP WITH YOUR SOCIAL MEDIA

There are different PHP snippets out there you can use in order to retrieve your bookmarked urls on different social media websites, you can retrieve your tweets and display them in your website or even post your tweets from your website. Below are couple of PHP techniques you can use.

4.1 UPDATE YOUR TWITTER STATUS USING PHP

We have seen many web application that offers you a way to update your twitter status through their apps. You can add this functionality to your website using the simple script in this post. Don’t forget to add your username, password and message.
Update your Twitter status using PHP

4.2 GET YOUR RECENT DELICIOUS BOOKMARKS USING PHP

This tutorial uses PHP5 to download and cache your recent bookmarks in RSS format from the Delicious API, then displays them in a HTML unordered list. This tutorial uses SimpleXML.
Source

4.3 BUILD A PHP TWITTER WIDGET

This tutorial will teach us how to use cURL to get your Twitter status and cache it into a file on your server. This file will then be read with JavaScript and displayed on the web-page.

5. MAKING FORMS WORK WITH PHP

5.1 CREATING A REGISTER & LOGIN SYSTEM WITH PHP, MYSQL & JQUERY

Learn how to create a simple login / registration system in this step by step tutorial. You will have the ability to easily create a member-only area on your site and provide an easy registration process. It is going to be PHP driven and store all the registrations into a MySQL database.
The script is using sliding jQuery panel, developed by Web-kreation.
Creating a Register & Login System With PHP, MySQL & jQuery
Source

5.2 PHP ONLINE QUOTE FORM

A nice and clean quote form your clients can use to request a free quote for their web projects. The code is using date picker from ElectricPrism which uses Mootools.
PHP Online Quote Form

5.3 AJAX CONTACT FORM WITH CAPTCHA, REALTIME VALIDATOR AND PHP BACKEND

An advanced script with many features including CAPTCHA and Real-time Validation. Unlike many AJAX Captchas out there that rely on JavaScript, this one uses PHP to securely generate and register the required code in the background. Moreover, you can specify whether to show letter, numbers or both on the verification image.
AJAX Contact Form with CAPTCHA, Realtime Validator and PHP Backend
Source

6. SQL INJECTION WITH PHP

Most web applications interact with a database, and the data stored therein frequently originates from remote sources. Thus, when creating an SQL statement, you often use input in its construction. A typical SQL injection attack exploits this scenario by attempting to send fragments of valid SQL queries as unexpected values of GET and POST data. This is why an SQL injection vulnerability is often the fault of poor filtering and escaping, and this fact cannot be stressed enough.
This article explains SQL injection by looking at a few example attacks and then introducing some simple and effective safeguards. By applying best practices, you can practically eliminate SQL injection from your list of security concerns.
<?php
$sql = "SELECT card_num, card_name, card_expiry
FROM credit_cards
WHERE username = '{$_GET['username']}'";
 ?>

7. BUILDING CMS USING PHP

7.1 BUILDING SIMPLE CMS USING PHP

Jason Lengstorf shows how to build a very simply object-oriented Content Management System with PHP. While not a ready-to-use solution, due to a few security holes, it nonetheless does a good job at teaching a real-world use for PHP. You will find part2 of this tutorial very useful as it cover different security holes the first part didn’t cover. Further reading on security risks and safe PHP code can be found here.

7.2 BUILDING CONTENT EDITING SYSTEM USING PHP & JQUERY

This tutorial will show you how to use jQuery and PHP to build a content editing system that will allow you or your client to easily edit .html pages visually. The tutorial will use PHP’s file_get_contents() to load the selected HTML file into a text area.
Source

8. DYNAMICALLY CREATE THUMBNAILS USING PHP

Jeffrey Way shows us how to how to upload files and then have PHP dynamically create a thumbnail. This technique is useful specially When you deal with large images, it’s often necessary to create smaller “thumbnail” versions. Whether you’re building an ecommerce site, or just a simple gallery, these techniques will absolutely prove to be useful.

9. BANNER ROTATOR WITH PHP, JQUERY & MYSQL

In this tutorial we are going to learn how to make a simple PHP, jQuery & MySQL banner rotator, with which you can create and randomly display banners on your site. Each of the banners features a neat jQuery animation effect, which you can customize to fit your own needs.

10. YOUTUBE VIDEO MANAGER WITH PHP, MYSQL & JQUERY

Youtube is the most popular platform when it comes to upload your videos to the web. It’s therefore likely that some day, as a web developer, you’ll be asked by one of your clients to integrate some YouTube videos on a web page or a social web app.
We’ll learn in this post how with a PHP class, a little MYSQL table made of only 3 fields, and a bit of jQuery code, you can build a very nice Video Manager.
This manager will allow yourself or your client to manage videos easily, without any technical knowledge. We’ll see how you can add a video simply by pasting its YouTube url, how to edit them, and how to delete them thanks to a nice interface.

No comments:

Post a Comment

LinkWithin

Related Posts Plugin for WordPress, Blogger...

ShareThis