Showing posts with label PHP. Show all posts
Showing posts with label PHP. Show all posts

Tuesday, October 23, 2012

Difference between Application server and Web Server

What is Application Server and Web Server? Your search ends here .. 

apache, nginx, IIS are web servers
mongrel, webrick, phusion passenger are app servers

App server is something which works with particular programming language and parses and executes the code
since mongrel and webrick can only work with rails, so they are app servers

Web servers are servers which can take the request from the browser.
Web servers normally works on port 80 though we can change the port in configuration 
since mongrel and webrick can take that request directly, so they can be thought of as web servers but web servers do have a lot of other functionality like request pipeline, load balancing etc.
App servers lack these functionalities.

About Mongrel server:
mongrel work as web as well as app server if you are talking about dev environment
but in production, mongrel alone can not work it will be too slow
so we need a web server in front of mongrel

Friday, March 16, 2012

An Intoduction into FreeBSD ports

Under Linux Platform we usually use rpm ,up2date ,yum or apt-get command to install a package.
Under FreeBSD you can use pkg_add command or ports system.

The FreeBSD Ports Collection is a package management system that provides an easy and consistent way of installing software packages on the FreeBSD. Usually ports is located at /usr/ports directory.

The FreeBSD Ports and Packages Collection offers a simple way for users and administrators to install applications. There are currently 23322 ports available.
Installing an application is as simple as downloading the port, unpacking it and typingmakein the port directory. However, the most convenient (and common) method is to download the framework for the entire list of ports by installing the entire ports hierarchy at FreeBSD installation time, and then have thousands of applications right at your fingertips.

Task: Find out FreeBSD port name or package name

There are 3 different methods available to search a port name. Use any one of the following method only.

#1 : Using whereis command

If you know the exact name of the port, but just need to find out which category it is in, you can use the whereis(1) command. Simply type whereis file, where file is the program you want to install.
# whereis php5
Output:
php5: /usr/ports/lang/php5
# whereis lighttpd
Output:
lighttpd: /usr/ports/www/lighttpd

#2: Using make command

Change directory to /usr/ports
# cd /usr/ports
To search type the command as follows:
# make search name="package-name"
For example search a package called lighttpd or php
# make search name="php"
OR
# make search name="lighttpd"
Output:
Port:   lighttpd-1.4.13_2
Path:   /usr/ports/www/lighttpd
Info:   A secure, fast, compliant, and very flexible Web Server
Maint:  mnag@FreeBSD.org
B-deps: autoconf-2.59_2 libtool-1.5.22_4 m4-1.4.8_1 pcre-7.0_1 perl-5.8.8
R-deps: pcre-7.0_1
WWW:    http://www.lighttpd.net/

#3: Using locate command

You can also use locate command:
# locate php
# locate php | grep php5

Task: Install FreeBSD port

Above output displays port Path - /usr/ports/www/lighttpd. Just change directory to /usr/ports/www/lighttpd
# cd /usr/ports/www/lighttpd
Now install a port:
# make; make install
Clean the source code tree:
# make clean
 Thanks & HAppp Porting..............!

Monday, May 9, 2011

Flipping of image with another using JavaScript

Below is very simple snippet code for flipping of two images using JavaScript. If possible you guys can make it generic snippet code. This code will help in mainly for showing / hiding of div’s, for example if he clicks on ‘Show Image’ it will automatically changes to ‘Hide Image’. Its a simple snippet code.


Below is the div, in div – Initially I am showing the hide image, when user clicks on this image, the image as well as the title of the img will be swapped with show image and the title will be ‘Maximize’… and Vice-Versa.

The below function should be placed in <script></script>.
//This Function is for Flipping of 'Show Image' to 'Hide Image' and Vice-Versa
function flip(imageID) {
var img = document.getElementById(imageID);
if (img.src.indexOf('images/hide.gif') > -1) {
img.src = 'images/show.gif';
img.title='Maximize';
} else {
img.src = 'images/hide.gif'
img.title='Minimize';
}
}
 below div should be placed in <body>.
<div><a href="#" onClick="flip('hide'); return false;" title="Minimize" style="padding:0px"><img src="images/hide.gif" id="hide" border="0"></a></div>

Javascript to strip special characters from a string


 <script language="JavaScript"><!--
var temp = new String('This is a te!!!!st st>ring... So??? What...');
document.write(temp + '<br>');
temp =  temp.replace(/[^a-zA-Z 0-9]+/g,'');
document.write(temp + '<br>');
//--></script>


Remove duplicates from an array using JavaScript

Here is a simple and easily understandable snippet code, to remove duplicates from an existing array using JavaScript. You guys! might have come across in some situation where in which the array has got some duplicates and want to remove those duplicates to show up unique ones. Below simple JavaScript code does the same, initially the respective array has got some value like below:

var sampleArr=new Array(1,1,1,1,1,2,2,3,3,3,4,4,4);

javascript code:

var sampleArr=new Array(1,1,1,1,1,2,2,3,3,3,4,4,4); //Declare array
document.write(uniqueArr(sampleArr)); //Print the unique value
 
//Adds new uniqueArr values to temp array
function uniqueArr(a) {
 temp = new Array();
 for(i=0;i<a.length;i++){
  if(!contains(temp, a[i])){
   temp.length+=1;
   temp[temp.length-1]=a[i];
  }
 }
 return temp;
}
 
//Will check for the Uniqueness
function contains(a, e) {
 for(j=0;j<a.length;j++)if(a[j]==e)return true;
 return false;
}

Tuesday, December 14, 2010

PHP: Make an Alphabetical Selection from Elements of an Array

Here’s a snippet that extracts elements from an array starting with a specific letter (alphabetical search).

$programming_languages = array('A+', 'A++', 'A# .NET', 'A# (Axiom)', 'A-0', 'B', 'Baja', 'BeanShell', 'C#', 'Cayenne', 'JavaScript', 'Java', 'JScript', 'Python', 'Perl', 'PHP', 'Visual Basic .NET', 'Visual FoxPro', 'ZOPL', 'ZPL');

function alpha_search($array, $letter = 'a') // 'a' is the default value
{
return array_filter($array, create_function('$var', 'return (strtolower(trim($var{0})) == strtolower("'.$letter.'"));'));
}

/* Values starting with 'A' */
echo '<pre>'; print_r(alpha_search($programming_languages, 'a'));  echo '</pre>';

/* Values starting with 'V' */
echo '<pre>'; print_r(alpha_search($programming_languages, 'v'));  echo '</pre>';

NOTE: In the second argument you can use either a lower case letter or an upper case letter. It’s case insensitive.


Array
(
    [0] => A+
    [1] => A++
    [2] => A# .NET
    [3] => A# (Axiom)
    [4] => A-0
)

Array
(
    [16] => Visual Basic .NET
    [17] => Visual FoxPro
)

PHP: How to remove empty values from an array

This function removes empty values from an array. This is useful if you’re working with dynamical arrays (for instance data harvested from a web site).



<?php
/*
Credits: Bit Repository
URL: http://www.bitrepository.com/web-programming/php/remove-empty-values-from-an-array.html
*/
function remove_array_empty_values($array, $remove_null_number = true)
{
$new_array = array();

$null_exceptions = array();

foreach ($array as $key => $value)
{
$value = trim($value);

        if($remove_null_number)
{
       $null_exceptions[] = '0';
}

        if(!in_array($value, $null_exceptions) && $value != "")
{
            $new_array[] = $value;
        }
    }
    return $new_array;
}
?>

Example of usage:

$array = array("white", "yellow", 0, "green", " ", "navy");

$remove_null_number = true;
$new_array = remove_array_empty_values($array, $remove_null_number);

echo '<pre>'; print_r($new_array); echo '</pre>';

Array
(
    [0] => white
    [1] => yellow
    [2] => green
    [3] => navy
)

The new array will look like this:

Saturday, December 4, 2010

PHP Programming Language

PHP ( Hypertext Preprocessor ) was released in the 1995PHP was originally known as Personal Home Page written as a set of Common Gateway Interface (CGI) in C Language byDanish/Greenlandic programmer Rasmus Lerdorf. Lerdorf initially created these Personal Home Page Tools to replace a small set of Perl scripts he had been using to maintain his personal homepage and was originally designed to replace a set of Perl scripts to maintain his Personal Home Pages (also known as PHP) in 1994. He combined these with his Form Interpreter to create PHP/FI, which had more functionality. PHP/FI included a larger implementation for the C programming language and could communicate with databases, enabling the building of simple, dynamic web applications. Lerdorf released PHP publicly on June 8, 1995 to accelerate bug location and improve the code.This release was named PHP version 2 and already had the basic functionality that programming php has today.
Mainly , PHP was designed to create dynamic and more interactive web pages. It is a server-side scripting language embed with HTML. Now a days , it is the most widely-used open-source and general-purpose scripting language. PHP code in a script can interect with databases, create images, read and write files and talk to remote servers. So the output from PHP code is combined with HTML in script and the result is sent to the user as a web page.

All major operating systems including LinuxMicrosoft Windows, Mac OS X, and RISC OS , you can use PHP in all these operating systems. As you know there are procedural programming or object oriented programming. PHP uses either procedural programming or object oriented programming. Also you can mix them and use both ( procedural programming or object oriented programming ). As i mention above, PHP is used mainly in server-side scripting,command line interface and writing desktop applications. PHP also supports ODBC, theOpen Database Connection standard which allows you to connect to other databasessupporting this world standard. For database , phpMyAdmin is most widely used for PHP developement. Server-side scripting is the most traditional one for PHP development. In order to use PHP for server-side scripting you need
1. PHP parser
2. Web server
3. Web browser.

The PHP codes entered with the parser on a web server will be translated into a PHP page that can be viewed on your web browser. Main reason behind popularity of PHP language is , it can be embedded directly into HTML coding. It has more benefits such as the following

1. It can be used on all major operating systems.
2. It can be supported by most of the web servers.
3. The latest version of PHP development is a very stable and grown-up language used for web programming like Java and C#.

Monday, November 29, 2010

PHP Pagination

This is a guide how to create a pagination in PHP like digg.com pagination style  


Overview
This is a script to make pagination in PHP like digg.com or flickr.com.
This script was written by a guy from Stranger Studios. In this page I've edit the script a little bit to make it working and easy to edit. You can grab the original script at Digg-Style Pagination. My tutorial is just a guide how to implemente in your PHP script.
This is what you'll get after finished this code
PHP Pagination Script





The Code
Grab the code and paste in your text editor. 
You have to customize 7 spots in this code (see images below).
1. Code to connect to your DB - place or include your code to connect to database.
2. $tbl_name - your table name.
3. $adjacents - how many adjacent pages should be shown on each side.
4. $targetpage - is the name of file ex. I saved this file as pagination.php, my $targetpage should be "pagination.php".
5. $limit - how many items to show per page.
6. "SELECT column_name - change to your own column.
7. Replace your own while..loop here - place your code to echo results here.
After all, save and test your script

Apply CSS Style to your code
Save this code to style.css and link to your pagination page.
To get more CSS style click here
PHP pagination
<?php
    /*
        Place code to connect to your DB here.
    */
    include('config.php');    // include your code to connect to DB.

    $tbl_name="";        //your table name
    // How many adjacent pages should be shown on each side?
    $adjacents = 3;
  
    /*
       First get total number of rows in data table.
       If you have a WHERE clause in your query, make sure you mirror it here.
    */
    $query = "SELECT COUNT(*) as num FROM $tbl_name";
    $total_pages = mysql_fetch_array(mysql_query($query));
    $total_pages = $total_pages[num];
  
    /* Setup vars for query. */
    $targetpage = "filename.php";     //your file name  (the name of this file)
    $limit = 2;                                 //how many items to show per page
    $page = $_GET['page'];
    if($page)
        $start = ($page - 1) * $limit;             //first item to display on this page
    else
        $start = 0;                                //if no page var is given, set start to 0
  
    /* Get data. */
    $sql = "SELECT column_name FROM $tbl_name LIMIT $start, $limit";
    $result = mysql_query($sql);
  
    /* Setup page vars for display. */
    if ($page == 0) $page = 1;                    //if no page var is given, default to 1.
    $prev = $page - 1;                            //previous page is page - 1
    $next = $page + 1;                            //next page is page + 1
    $lastpage = ceil($total_pages/$limit);        //lastpage is = total pages / items per page, rounded up.
    $lpm1 = $lastpage - 1;                        //last page minus 1
  
    /*
        Now we apply our rules and draw the pagination object.
        We're actually saving the code to a variable in case we want to draw it more than once.
    */
    $pagination = "";
    if($lastpage > 1)
    {  
        $pagination .= "<div class=\"pagination\">";
        //previous button
        if ($page > 1)
            $pagination.= "<a href=\"$targetpage?page=$prev\">« previous</a>";
        else
            $pagination.= "<span class=\"disabled\">« previous</span>";  
      
        //pages  
        if ($lastpage < 7 + ($adjacents * 2))    //not enough pages to bother breaking it up
        {  
            for ($counter = 1; $counter <= $lastpage; $counter++)
            {
                if ($counter == $page)
                    $pagination.= "<span class=\"current\">$counter</span>";
                else
                    $pagination.= "<a href=\"$targetpage?page=$counter\">$counter</a>";                  
            }
        }
        elseif($lastpage > 5 + ($adjacents * 2))    //enough pages to hide some
        {
            //close to beginning; only hide later pages
            if($page < 1 + ($adjacents * 2))      
            {
                for ($counter = 1; $counter < 4 + ($adjacents * 2); $counter++)
                {
                    if ($counter == $page)
                        $pagination.= "<span class=\"current\">$counter</span>";
                    else
                        $pagination.= "<a href=\"$targetpage?page=$counter\">$counter</a>";                  
                }
                $pagination.= "...";
                $pagination.= "<a href=\"$targetpage?page=$lpm1\">$lpm1</a>";
                $pagination.= "<a href=\"$targetpage?page=$lastpage\">$lastpage</a>";      
            }
            //in middle; hide some front and some back
            elseif($lastpage - ($adjacents * 2) > $page && $page > ($adjacents * 2))
            {
                $pagination.= "<a href=\"$targetpage?page=1\">1</a>";
                $pagination.= "<a href=\"$targetpage?page=2\">2</a>";
                $pagination.= "...";
                for ($counter = $page - $adjacents; $counter <= $page + $adjacents; $counter++)
                {
                    if ($counter == $page)
                        $pagination.= "<span class=\"current\">$counter</span>";
                    else
                        $pagination.= "<a href=\"$targetpage?page=$counter\">$counter</a>";                  
                }
                $pagination.= "...";
                $pagination.= "<a href=\"$targetpage?page=$lpm1\">$lpm1</a>";
                $pagination.= "<a href=\"$targetpage?page=$lastpage\">$lastpage</a>";      
            }
            //close to end; only hide early pages
            else
            {
                $pagination.= "<a href=\"$targetpage?page=1\">1</a>";
                $pagination.= "<a href=\"$targetpage?page=2\">2</a>";
                $pagination.= "...";
                for ($counter = $lastpage - (2 + ($adjacents * 2)); $counter <= $lastpage; $counter++)
                {
                    if ($counter == $page)
                        $pagination.= "<span class=\"current\">$counter</span>";
                    else
                        $pagination.= "<a href=\"$targetpage?page=$counter\">$counter</a>";                  
                }
            }
        }
      
        //next button
        if ($page < $counter - 1)
            $pagination.= "<a href=\"$targetpage?page=$next\">next »</a>";
        else
            $pagination.= "<span class=\"disabled\">next »</span>";
        $pagination.= "</div>\n";      
    }
?>

    <?php
        while($row = mysql_fetch_array($result))
        {
  
        // Your while loop here
  
        }
    ?>

<?=$pagination?>

   




These are where you have to customize the script


PHP paginationPHP paginationPHP pagination

Friday, November 26, 2010

Special Thanks To Bilu

Special Thanks to bilu, the man behind the beautiful logo...and banner

PHP - Getting notice Undefined index

 The PHP notice errors are frustrating and you are 
tired of seeing them when you are working on your scripts.
 They are showed at the beggining of your pages and may 
reveal confidential information to the visitor like the path 
to the file or the php file name in some cases 

// Turn off all error reporting
error_reporting(0);

// Report simple running errors
error_reporting(E_ERROR | E_WARNING | E_PARSE);

// Reporting E_NOTICE can be good too (to report uninitialized
// variables or catch variable name misspellings ...)
error_reporting(E_ERROR | E_WARNING | E_PARSE | E_NOTICE);

// Report all errors except E_NOTICE
// This is the default value set in php.ini
error_reporting(E_ALL ^ E_NOTICE);

// Report all PHP errors (bitwise 63 may be used in PHP 3)
error_reporting(E_ALL);

// Same as error_reporting(E_ALL);
ini_set('error_reporting', E_ALL);