Latest Post
Tampilkan postingan dengan label functions. Tampilkan semua postingan
Tampilkan postingan dengan label functions. Tampilkan semua postingan

Useful PHP Functions and Codes for PHP Developers

Written By Unknown on Minggu, 15 Desember 2013 | 07.40

Hi guys, Today I am going to share some important PHP functions/Codes which I had been used in my web projects. This will really useful & save your coding time also. You can integrate anytime in your web projects / applications. All the below codes are more secure & worked well in most server platforms.

To read more about this article Click Here

For Demo Click Here

PHP’s Built-In MYSQL Functions

Written By Unknown on Selasa, 21 Agustus 2012 | 09.03

The MySQL database server is one of the most popular open source databases in the world. Most of php application developed using mysql database. PHP has many built-in MySQL functions which works with MySQL to execute queries. these functions help you to manage your database and speed-up development.
 
Below is the list of most used PHP/MYSQL built in functions in php.

To read more about this article Click Here


 

7 Useful functions to tighten the security in PHP

Written By Unknown on Rabu, 06 Oktober 2010 | 23.14


Security is a very important aspect of programming. In PHP, there are few useful functions which is very handy for preventing your website from various attacks like SQL Injection Attack , XSS attack etc.Let’s check few useful functions available in PHP to tighten the security in your project. But note that this is not a complete list, it just list of functions which I found useful for using in your project.

1) mysql_real_escape_string() - This function is very useful for preventing from SQL Injection Attack in PHP . This function adds backslashes to the special characters like quote , double quote , backslashes to make sure that the user supplied input are sanitized before using it to query. But, make sure that you are connected to the database to use this function.
2) addslashes() – This function works similar as mysql_real_escape_string(). But make sure that you don’t use this function when “magic_quotes_gpc” is “on” in php.ini. When “magic_quotes_gpc” is on in php.ini then single quote(‘) and double quotes (“) are escaped with trailing backslashes in GET, POST and COOKIE variables. You can check it using the function “get_magic_quotes_gpc()” function available in PHP.


3) htmlentities() – This function is very useful for to sanitize the user inputted data. This function converts the special characters to their html entities. Such as, when the user enters the characters like “<” then it will be converted into it’s HTML entities < so that preventing from XSS and SQL injection attack.

4) strip_tags() – This function removes all the HTML, JavaScript and php tag from the string. But you can also allow particular tags to be entered by user using the second parameter of this function. 

For example,
echo strip_tags(“<script>alert(‘test’);</script>”);
will output
alert(‘test’);

5) md5() – Some developers store plain password in the database which is not good for security point of view. This function generates md5 hash of 32 characters of the supplied string. The hash generated from md5() is not reversible i.e can’t be converted to the original string.

6) sha1() – This function is similar to md5 but it uses different algorithm and generates 40 characters hash  of a string compared to 32 characters by md5().


7) intval() – Please don’t laugh. I know this is not a security function, it is function which gets the integer value from the variable. But you can use this function to secure your php coding. Well, most the values supplied in GET method in URL are the id from the database and if you’re sure that the supplied value must be integer then you can use this function to secure your code.

$sql=”SELECT * FROM product WHERE id=”.intval($_GET['id']);

As, you can see above, if you’re sure that the input value is integer you can use intval() as a secrity function as well.

Function to rotate image in PHP

Written By Unknown on Selasa, 31 Agustus 2010 | 02.10


GD library in PHP is very useful for image processing and you can do a lot image manipulation from it. In this post, I’ll show you a simple Image manipulation (image rotation)using the function provided below in PHP. You’ll see how easy it is to rotate an image using PHP.

Function to rotate image using GD library of PHP

function rotateImage($sourceFile,$destImageName,$degreeOfRotation)
{
  //function to rotate an image in PHP
  //developed by Roshan Bhattara (http://roshanbh.com.np)
  //get the detail of the image
  $imageinfo=getimagesize($sourceFile);
  switch($imageinfo['mime'])
  {
   //create the image according to the content type
   case "image/jpg":
   case "image/jpeg":
   case "image/pjpeg": //for IE
        $src_img=imagecreatefromjpeg("$sourceFile");
                break;
    case "image/gif":
        $src_img = imagecreatefromgif("$sourceFile");
                break;
    case "image/png":
        case "image/x-png": //for IE
        $src_img = imagecreatefrompng("$sourceFile");
                break;
  }
  //rotate the image according to the spcified degree
  $src_img = imagerotate($src_img, $degreeOfRotation, 0);
  //output the image to a file
  imagejpeg ($src_img,$destImageName);
}

The above function takes takes three argument, first one is the source image to be rotated and the second one is the name of file which is resulted after rotating the original image. And, the last parameter is the degree of the rotation of Image.
In this PHP function, first of all the information about the source image is stored in “$imageinfo” array. The MIME type of the file are stored in “mime” key of the “$imageinfo” array. And then appropriate image resource is created using the proper MIME type. And then, imagerotate() function of PHP is used for the rotation of the image then imagejpeg() is used to output the image to a file.

Now let’s look at the php code to call the above function see the result of rotated image with that function

 <?php rotateImage('image.jpg','rotated.jpg',90); ?> 


Simple PHP Caching and Content Retrieval Function

Written By Unknown on Jumat, 28 Mei 2010 | 22.30

One way to make your website exponentially faster is by caching both remote and internal requests. Why request your RSS subscriber count from FeedBurner more than once a day if that count is calculated once per day? Why hit your database on each page load if that content rarely changes?


 I’ve created a primitive request-and-cache function for PHP that checks for fresh content in the cache and retrieves content from a source if fresh content isn’t available.


/* gets the contents of a file if it exists, otherwise grabs and caches */
function get_content($file,$url,$hours = 24,$fn = '',$fn_args = '') {
//vars
$current_time = time(); $expire_time = $hours * 60 * 60; $file_time = filemtime($file);
//decisions, decisions
if(file_exists($file) && ($current_time - $expire_time < $file_time)) {
//echo 'returning from cached file';
return file_get_contents($file);
}
else {
$content = get_url($url);
if($fn) { $content = $fn($content,$fn_args); }
$content.= '';
file_put_contents($file,$content);
//echo 'retrieved fresh from '.$url.':: '.$content;
return $content;
}
}

/* gets content from a URL via curl */
function get_url($url) {
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1); 
curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,5);
$content = curl_exec($ch);
curl_close($ch);
return $content;
}


My get_content function accepts four arguments:
  • The file to grab content from. If the file doesn’t exist, the file is created and content placed into.
  • The URL to get content from if cached content isn’t fresh.
  • A function name to pass the freshly received content to.
  • Arguments to pass to the third argument’s function.
The function is, of course, very primitive. I like that my function handles both retrieval and caching so that I don’t need to repeat code whenever I want cached content.

Sample Usage 1


/* usage */
$TWITTER_FOLLOWERS_FILE_NAME = 'twitter-followers.txt';
$TWITTER_FOLLOWERS_URL = 'http://twitter.com/users/show.json?screen_name=davidwalshblog';

$TWITTER_FOLLOWERS = get_content($TWITTER_FOLLOWERS_FILE_NAME,$TWITTER_FOLLOWERS_URL,3,'format_followers',array('file'=>$TWITTER_FOLLOWERS_FILE_NAME));
/* utility function */
function format_followers($content,$args) {
$content = json_decode($content);
$twitter_subscribers = $content->{'followers_count'};
if($twitter_subscribers) {
$twitter_subscribers = number_format($twitter_subscribers,0,'',',');
file_put_contents($args['file'],$twitter_subscribers);
return $twitter_subscribers;
}
}

The above code retrieves my Twitter follower count, parses the code, and caches the content for three hours.
There are several more advanced PHP caching classes available but the simple function above covers most of my needs — hopefully it can help you out too!

Php function to validate two decimal places of a number

Written By Unknown on Selasa, 04 Mei 2010 | 01.11


If you are looking for the validation of a number which contains only two decimal places. Means you want to accept the values like 0.21 or 1.34 or 12.55 or 445.66 as a input and throw an error when somebody enters the number like 0.2 or 4.678 from a text box. Here is a simple function for you in PHP which validates the number weather it contains exactly two decimal places or not.

Function to validate two decimal places of a number in PHP


function validateTwoDecimals($number)
{
   if(ereg('^[0-9]+\.[0-9]{2}$', $number))
return true;
   else
return false;
}

Well let me explain the fairly simple regular expression inside the ereg() function of PHP.

^[0-9]+\.[0-9]{2}$

The hat(^) represents the start of the string and the [0-9]+ tells that there will be one or more digits at the starting of the the string. “‘\.” represents that there should be a period(.) after that and [0-9]{2} tells that after there should be exactly two digits after period and the dollar sign($) represents the end of the string.


 
Support : Creating Website | Johny Template | Mas Template
Copyright © 2011. Kumpulan Kata Broadcast Blackberry - All Rights Reserved
Template Created by Creating Website Published by Mas Template
Proudly powered by Blogger