Skip to main content

Posts

Showing posts from November, 2010

PHP Function to return upper and lower time stamp of a given date

Function to return upper and lower time stamp of a given date

Function returns the upper and lower time stamp of a given date in dd/mm/yyyy format.

checks date in dd/mm/yyy format is valid and return upper/lower limit of timestamp of that day as integer

if date is invalid then returns 0 // $upper_or_lower = U or L

if date is null this will return current time stamp






function getTimeStamp($Date="",$upper_or_lower="l"){
//checks date in dd/mm/yyy format is valid and return upper/lower limit of timestamp of that day as integer
//if date is invalid then returns 0 // $upper_or_lower = U or L
//if date is null thsi will return current timestamp
$timemsg="";
if(trim($Date)=="")
$timemsg=time();
else{
//spilit dates in parts
$DateParts = split( "[/-]" , $Date );

if( checkdate($DateParts[1],$DateParts[0],$DateParts[2]) ){
if( strtolower($upper_or_lower)=="u" )
$timemsg=mktime( 23,59,59,$DateParts[1],$DateParts[0], $DateParts[2]…

Javascript function to enable a particular select box option or all the options in a select box

Javascript function to enable a particular select box option or all the options in a select box

Function takes parameters as
selboxId - Id of the select box
selIndex - the index position of the option element to be enabled

JS function to enable a particular select box option by passing its selected index. if no selected Index is passed to the function it enables all the options of select box.

It check whether the selectbox ID is not null
it checks whether the select Index is specified, if not it is set to -1
It checks whether the object is present in the form
It loops through the options in select box
if selected index is -1 then all options are enabled
if select index is given that option alone is enabled

Scroll down for the JS Code:






JS Code



// function to enable a particular select box option by passing its selected index
//if no selected Index is passed to the function it enables all the options of select box

function enableOption(selboxId,selIndex){

//checks whether the select …

Looping through directory and list files after sorting - php function

PHP Code for simply looping and listing the files names in a directory into a select box.


<select>
<?php

$logFolder="../csvfolder";
if ( $handle = opendir($logFolder) ) {
/* loop over the directory. */
while (false !== ($file = readdir($handle))) {
if( stripos( $file,".csv" ) > 0 )
echo "<option value=\"$file\">$file</option>\n";
}

closedir($handle);
}

?>
</select>


Here I am checking whether the file in the folder is a csv file, if it is a csv it is listed.




PHP function to loop through the files in a directory and list the file names after sorting it.


<ul>
<?php
$dir = "directory";
if ($handle = opendir ($dir)) {
while (false !== ($file = readdir ($handle))) {
if (preg_match ("/.inc$/i", $file)) {
$file = str_replace ("_", " ", $file);
$file = explode (".", $file);
$file = $file[0];
$filelist[] = trim ($file);
}
}
sort ($filelist);
reset ($filelist);
w…

mod rewrite - checking query string in HTACCESS file

Using mod rewrite rule in htaccess

Using Query string check in htaccess


Question:
Say I want to forward this URL: /cansas.php?m=2&id=2-0-0-0&sid=cansas to /cansas-is-good-for-you and let the header respond with a 301, or just update the URL through [R].

I have this in my .htaccess:

Options +FollowSymlinks

RewriteEngine on

RewriteRule ^cansas.php?m=2&id=2-0-0-0&sid=cansas$ cansas-is-good-for-you [NC,R=301]

I figured I could just do a simple forwarding, but somewhere along the way it stops working. If I cut out the ?m=2&id= etc, it forwards just the cansas part to the new part so it looks like this: cansas-is-good-for-you?m=2&id=2-0-0-0&sid=cansas.

How can I forward it when I have several dynamic parameters in the URL string? Example on pages I need to forward:

/cansas.php?m=2&id=2-0-0-0&sid=cansas

/cansas.php?m=2&id=2-1-0-0&sid=cansas

/cansas.php?m=2&id=2-2-0-0&sid=cansas

Any help would be greatly appreciated :)

Maybe it isn't pos…

Php function to format the input values before inserting to the Db tables

Given below is a simple php function to format the input to the database inorder to avoid the SQL injection.



function formatInput($str_value){
//function to format strings in a query
$str_value = (!get_magic_quotes_gpc()) ? addslashes($str_value) : $str_value;
$str_value = trim($str_value);
$str_value = ($str_value != "") ? "'" . $str_value . "'" : "NULL";
return $str_value;
}


As you can see the function checks whether the magic quote feature of server is on and if not it adds slashes to the single and double quotes. If magic quotes is on the input value t the function will be having the single and double quotes escaped with a backslah.

before returning the value, single quotes are appended and prepending to the text.



SAMPLE USAGE


$myQry=" SELECT * FROM TBL_INVENTORY WHERE PURCHASE_CODE = ".formatInput($purchase_code);



Image Cropper - Javascript PHP free package

Source : http://uvumitools.com/crop.html

As the web grows and becomes more dynamic ("Web 2.0"), more and more websites have user-generated content and boast tools to let users sumbit text, images, videos, and more, providing content for entire web networks. Most of the time, people submit photos straight from their camera because they don't have photo editing programs or they don't know how to use them. This simplified tool gives your users the ability to create a selection area that can be used to crop an image live on your web site. This is an extremely useful too for any site that provides image hosting.


VISIT THE PACKAGE



Features:


# NEW! From simple to advanced
# Set minimum sizes
# Keep aspect ratio (or not)
# Real time preview
# Support HTML resizing
# Callback function
# Can be toggled on and off
# Modify style with CSS
# Fast, even on slow computers
# New! From 0 to 8 resizing handles
# New! Maximize selection on double-click
# New! Edit the selection dimensi…

Google search integration class - php class

This class can be used to search a site and retrieve results from Google.

It can send HTTP requests to Google search site to perform searches for given keywords restricted to a given site domain.

The class retrieves the result pages, extract the results and display them with configurable presentation styles.

Getting Actual IP Address for geo tracking

Getting Actual IP Address

How to get Actual IP address when the website is accessed using a proxy server?

I had to face a situation in which i had added a geo targeting code in my web app which blocks the users from out side India to access it.
it worked fine until there was a situation where some users from outside india tried to access the app using a proxy server in India.

Although there are some snippets available in web ( or you can code it yourself) which checks for the flag HTTP_X_FORWARDED_FOR in the header information.

Some example snippets:

################################################

FUNCTION #1

function GetUserIP() {

if (isset($_SERVER)) {

if (isset($_SERVER["HTTP_X_FORWARDED_FOR"]))
return $_SERVER["HTTP_X_FORWARDED_FOR"];

if (isset($_SERVER["HTTP_CLIENT_IP"]))
return $_SERVER["HTTP_CLIENT_IP"];

return $_SERVER["REMOTE_ADDR"];
}

if (getenv('HTTP_X_FORWARDED_FOR'))
return getenv('HTTP_X_FORWARDED_FOR'…

Getting the Orginal IP Address - PHP function

How to get Actual IP address when the website is accessed using a proxy server?


Although there are some snippets available in web ( or you can code it yourself) which checks for the flag HTTP_X_FORWARDED_FOR in the header information.

Some example snippets:

################################################

FUNCTION #1

function GetUserIP() {

if (isset($_SERVER)) {

if (isset($_SERVER["HTTP_X_FORWARDED_FOR"]))
return $_SERVER["HTTP_X_FORWARDED_FOR"];

if (isset($_SERVER["HTTP_CLIENT_IP"]))
return $_SERVER["HTTP_CLIENT_IP"];

return $_SERVER["REMOTE_ADDR"];
}

if (getenv('HTTP_X_FORWARDED_FOR'))
return getenv('HTTP_X_FORWARDED_FOR');

if (getenv('HTTP_CLIENT_IP'))
return getenv('HTTP_CLIENT_IP');

return getenv('REMOTE_ADDR');
}




FUNCTION #2

function get_real_IP_address()
{
if (!empty($_SERVER['HTTP_CLIENT_IP'])) //…

PHP function to add watermark on an image

How to apply watermark on a image?

Watermarking means applying an image with a transparent/non transparent background on another image to protect the image from unauthorised use of it by other parties which doesn;t own the image.

The following function can be used to add watermark on images.





The function accepts 2 parameters

1. the source image - the image on which watermark has to be applied
2. the position of the watermark image on the source image.





*****************CODE START***************

<?php

function apply_watermark($imagesource, $position = 'bottomright'){
$info = getimagesize($imagesource);
if($info){
switch($info[2]){
case 1 : { $image = imagecreatefromgif($imagesource); break;}//gif
case 2 : { $image = imagecreatefromjpeg($imagesource); break; }//jpg
case 3 : { $image = imagecreatefrompng($imagesource); break; }//png
default : { exit("Unsupporter file type"); }
}
}
else{
print "Can't read required image data";
}

PHP function to generate rounded edge image from a source image

Function for creating rounded edge image from a source image. The parameters to the function are corner radius (the degree to which the corner gets rounded). Also you can specify which of the corners of the image you want to get it as a rounded edge


*********************CODE START********************

function image_round_edges($image_file, $corner_radius=20, $angle= 0, $topleft=true, $topright=true, $bottomleft=true, $bottomright=true){ $info = getimagesize($image_file); if($info){ switch($info[2]){ case 1 : { $images = imagecreatefromgif($image_file); break;}//gif case 2 : { $images = imagecreatefromjpeg($image_file); break; }//jpg case 3 : { $images = imagecreatefrompng($image_file); break; }//png default : { exit("Unsupporter file type"); } } } else{ print "Can't read required image data"; }
$backcolor='FFFFFF'; $endsize=$corner_radius; $startsize=$endsize*3-1; $arcsize=$startsize*2+1;


// Top-left corner $background = imagecreatetruecolor($info[0],$info[1]); imageco…

RSS Feeds - Tips for promoting the RSS feed XML

What is RSS?
RSS (Rich Site Summary) is a format for delivering regularly changing web content. Many news-related sites, weblogs and other online publishers syndicate their content as an RSS Feed to whoever wants it.

Why RSS? Benefits and Reasons for using RSS
RSS solves a problem for people who regularly use the web. It allows you to easily stay informed by retrieving the latest content from the sites you are interested in. You save time by not needing to visit each site individually. You ensure your privacy, by not needing to join each site's email newsletter. The number of sites offering RSS feeds is growing rapidly and includes big names like Yahoo News.


What do I need to do to read an RSS Feed? RSS Feed Readers and News Aggregators
Feed Reader or News Aggregator software allow you to grab the RSS feeds from various sites and display them for you to read and use.

A variety of RSS Readers are available for different platforms. Some popular feed readers include Amphetadesk (Windo…

itunes rss feed podcast sample

Sample iTunes RSS Feed Podcast



iTunes
iTunes is a proprietary digital media player application, used for playing and organizing digital music and video files. The application is also an interface to manage the contents on Apple's iPod and iPhone lines, as well as the iPad. iTunes can connect to the iTunes Store via the Internet to purchase and download music, music videos, television shows...etc

Users can subscribe to any podcast by entering its RSS feed URL, but also by browsing the podcast directory within iTunes Music Store.

RSS Feed Information

The iTunes RSS feed is based on RSS 2.0 specifications, with a few notable additions. The additional tags, specific to iTunes are:

<itunes:category>
<itunes:explicit>
<itunes:subtitle>
<itunes:summary>
<itunes:author>
<itunes:keywords>
<itunes:duration>
<itunes:owner>
<itunes:name>
<itunes:email>
<itunes:image>
<itunes:block>
<copyright>
<link>








Sample feed



PHP function to convert duration into seconds

 Function to convert duration into seconds. Function accepts duration in the format hour:minutes:seconds (hh:mm:ss)  and this is converted into seconds.





functionconvertDurationToSeconds($dur){
// the input should be in format hh:mm:ss (example: 01:05:09)
  $DurationInSeconds=0;

// checks the presence of ":" twice
    if( substr_count($dur,":" ) == 2 ){
        $arrDuration= @explode( ":",$dur );
      if( is_array( $arrDuration ) && count($arrDuration) == 3 ){

// multiplies the hour part with 3600  , multiplies the minutes part with 60 and add all this with second part value
         $DurationInSeconds = ( intval( $arrDuration[0] ) * 3600 ) + ( intval( $arrDuration[1] ) * 60 ) + intval( $arrDuration[2] );
        }     

    }

        return $DurationInSeconds;
}

// End of function 

?>


Urgent Openings for PHP trainees, Andriod / IOS developers and PHP developers in Kochi Trivandrum Calicut and Bangalore. Please Send Your updated resumes to recruit.vo@gmail.com   Read more »
Member
Search This Blog