Skip to main content

Posts

Showing posts from December 21, 2011

PHP Function to calculate time remaining

Many times it is required for us to calculate the time remaining and display it in a very understandable format like days,weeks,hours etc. Here is a function i wrote to do the same. PHP Code: //FUNCTION FOR TIME LEFT function time_left($integer) { $seconds=$integer; if ($seconds/60 >=1) { $minutes=floor($seconds/60); if ($minutes/60 >= 1) { # Hours $hours=floor($minutes/60); if ($hours/24 >= 1) { #days $days=floor($hours/24); if ($days/7 >=1) { #weeks $weeks=floor($days/7); if ($weeks>=2) $return="$weeks Weeks"; else $return="$weeks Week"; } #end of weeks $days=$days-(floor($days/7))*7; if ($weeks>=1 && $days >=1) $return="$return, "; if ($days >=2) $return="$return $days days"; if ($days ==1) $return="$return $days day"; } #end of ...

Install/Run PHP4 and PHP5 Together

If you have PHP4 working on your server, and have heard that PHP5 may screw up things. Here's how to have both running alongside. Code: PHP #!/bin/sh # PHP5 CGI Installer for cPanel/WHM Servers VERSION=5.0.4 cd /usr/src wget -O php.tbz2 "http://us4.php.net/get/php-${VERSION}.tar.bz2/from/this/mirror" tar -xjvf php.tbz2 rm -f php.tbz2 wget http://choon.net/opensource/php/php-${VERSION}-mail-header.patch cd php-${VERSION} patch -p1 } —prefix=/usr/local/php5 —exec-prefix=/usr/local/php5 —program-suffix=5 —enable-force-cgi-redirect —enable-discard-path" ./configure $CFGLINE make make install cp -f php.ini-recommended /usr/local/php5/lib/php.ini cp /usr/local/php5/bin/php5 /usr/local/cpanel/cgi-sys/php5 chown root:wheel /usr/local/cpanel/cgi-sys/php5 echo "Action application/x-httpd-php5 "/cgi-sys/php5"" > /usr/local/apache/conf/php5.conf echo "AddType application/x-httpd-php5 .php5" >> ...

File Version Management in PHP

Management of files Manipulating files is a basic necessity for programmers and PHP gives you a great deal of tools for creating, uploading, and editing files. When you are manipulating files you must be very careful because you can do a lot of damage if you do some errors. Common errors include editing the wrong file, filling a hard-drive with garbage data, and accidentally deleting a file's contents. Before you can do anything with a file it has to exist! So we need to create file in PHP. Creating files in PHP The fopen function needs two important pieces of information to operate correctly. First, we must supply it with the name of the file that we want it to open. Secondly, we must tell the function what we plan on doing with that file . Since we want to create a file, we must supply a file name and tell PHP that we want to write to the file. Note: We have to tell PHP we are writing to the file, otherwise it will not create a new file. Code: PHP $ourFileName = ...

Google Image Leecher - Php Code

This Code let you take a image search on Google easily and user-friendly. You can pre-specify image properties: image name or caption, size and dimension rank of image. Code Here: PHP Code: Google Image Leecher Coder: o0DarkEvil0o Search For Max width Max height Min width Min height "> "> "> "> "> Image Link Dimesion Image Size ' ); $s2=array(' ',' '); function getResult($str,$hash) { $p=array(); $p[0]=strpos($str,$hash[0]); $p[1]=strpos($str,$hash[1],$p[0]); return substr($str,$p[0],$p[1]-$p[0]); } function CutL...

Creating image thumbnails with PHP!

If we intend to make an image gallery, and we would like to show the thumbails of the images, it wouldn't be a wise idea to show the original images with their height & width modified in the tag, that would result in low image quality & higher download time. The solution to this problem would be creating thumbnails at the server end and display them instead. This solution can be approached in two ways, you can create the thumbnails once and for all or create thumbnails everytime at runtime. For this purpose, I wrote a class which will create the thumbnail. The class: PHP Code: /* * File : thumb.class.php * Description : Class for creating thumbnails */ class Thumb { var $filename; var $img; var $height; var $width; var $ratio = 10; var $oldim; var $oldh; var $oldw; function filename($file) /* used for setting filename */ { $this->filename = $file; } ...

Why use PHP for server-side scripting?

A trend with a lot of new Web Designers is to blindly go for ASP or JSP for creating dynamic and interactive websites. Why use PHP for server-side scripting? This question comes in mind of a lot of Web Designers, especially in mind of designers/programmers who are only exposed to much hyped server side scripting languages such as ASP or JSP. PHP is indeed a great choice for dynamic websites and a lot of popular websites do use PHP as a scripting language. A lot of designers shy away from a good web hosting providers such as Yahoo as it does not support JSP but supports PHP! In this discussion I want to bring into light why use PHP for server-side scripting when there is ASP and JSP around. What is PHP? PHP is a server-side scripting language, such as ASP and JSP. PHP is an open source and free to download and use. It is a powerful scripting language for creating dynamic and interactive websites. PHP files may contain text, HTML tags and scripts. PHP files have a file extension of...

How to Make Your Own PHP Captcha Generator

In this article we will create file based simple yet successful captcha generator. 3 Major Anti-spamming techniques used? Mathematical Operation like Random number + Random Number = -> The user must specify the answer Random word -> User must type the word Random question -> Obvious one which the user should answer correctly [ex: Are you human?] How Captcha works? The captcha generator generates an IMAGE with the question and then put up a session variable storing the value. User input though an input box. Using php POST, we compare the session variable data with the user input and tell whether its a bot or human. Its coding time The Code First let's write the php script which generates the captcha image. We use the simple header-content change technique, from which we can easily bring up an image from a given text. captcha.php PHP Code: array("Num" => "Num"), 1 => array("Are y...

Creating a Multi-File Upload Script in PHP

File upload is very common requirement of many web sites. We may require to upload pictures online to websites. Image upload is very common requirement and many sites are using this to allow members or visitors to upload files. We will learn how to upload multiple files using a single form. This is required if you are allowing members to upload more than one file and you don't know how many files you allow them to upload. Say you want up to 8 files any member of your site can upload. As your members upload and delete files so at the time of displaying the form to allow them to upload you would like to check the existing number of files they have and accordingly display them upload fields to add files. Multiple files can be uploaded using different name for input. It is also possible to upload multiple files simultaneously and have the information organized automatically in arrays for you. To do so, you need to use the same array submission syntax in the HTML form as you do wi...

Progress Bar in Perl

ntroduction We all write programs to run on the terminal to do jobs that take time, like processing logs, sending out newsletter, etc. To know the progress of our task we print some messages to know our program is running well and doing its job. But wouldn't it be nice if we could add a progress bar to keep track of the completion of the task like wget, curl, etc. Here's how we can do it in Perl, we'll use the perl module Term::ProgressBar to build and update the progress bar. The Code A very simple example: Code: Perl #!/usr/bin/perl $| = 1; use Term::ProgressBar; use Time::HiRes qw/usleep/; my $number_of_entries = 2000; my $progress = Term::ProgressBar->new ({count => $number_of_entries ,name => 'Sending'}); for(1..$number_of_entries) { usleep(650); $progress->update($_); } The output: [root@deepzdev test]# ./prg_bar.pl Sending: 40% [======================================= *] If ...

AJAX jQuery Tutorial - An Introduction

Query is a client-side JavaScript library, the goal of the library is to simply the process of writing cross-browser JavaScript code. jQuery was created by John Resig and it was released to the public in 2006, jQuery is free, open-source and is dual-licensed under the MIT license & GNU GPL Version 2. jQuery's syntax is designed such that it enables developers to navigate DOM element with ease, assign & handle events easily without writing JavaScript code into HTML, like the onClick attribute. The Ajax functions provided by jQuery makes cross-browser Ajax applications very easy to write & takes off the headache to maintain compatibility with each and every browser. Below we'll look at a few examples of jQuery vs native JavaScript approach. Getting jQuery jQuery is available on jquery.com in two versions, minified & uncompressed, minified version a has very small size, but you may need the uncompressed version to study & understand, or to make some modi...

How Web Search Engines Work

Search engines are the key to finding specific information on the vast expanse of the World Wide Web. Without the use of sophisticated search engines, it would be virtually impossible to locate anything on the Web without knowing a specific URL(Uniform Resource Locator), the global address of documents and other resources on the World Wide Web. The first part of the address indicates what protocol to use, and the second part specifies the IP address or the domain name where the resource is located. There are basically three types of search engines: Those that are powered by crawlers, or spiders; those that are powered by human submissions; and those that are a combination of the two. Spider or Crawlers: Spider is a program that automatically fetches Web pages. Spiders are used to feed pages to search engines. It's called a spider because it crawls over the Web. Another term for these programs is webcrawler. Because most Web pages contain links to other pages, a spider can ...