Caputo's blog

Informatica, tecnologia, programmazione, fai da te, papercraft e papertoy

Funzioni utili per PHP

Maggio 9th, 2009 by Giovanni Caputo

  1. Generare password random

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    <?php
    
    function generatePassword($length) {
        $character = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
        $password = "";
        for($i=0;$i<$length;$i++) {
            $password .= $character[rand(0, 61)];
        }
        return $password;
    }
    
    echo generatePassword(7);
    
    ?>
  2. Calcolare anni a partire dalla data di nascita

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    <?php
    
    $testcase = array("12-03-1985", "15-01-1987", "01-01-2000");
    
    function getAge($year) {
        return floor(abs(strtotime('now') - strtotime($year))/31536000);
    } 
    
    foreach($testcase as $test) {
        echo "Born in $test, approximately age is " . getAge($test)." years<br/>";
    }
    ?>
  3. Ottenere indirizzo IP dei visitatori

    1
    2
    3
    4
    5
    <?php  
    
    echo "Your IP address is " . $_SERVER['REMOTE_ADDR'];  
    
    ?>
  4. Ottenere l’estensione di un file

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    <?php
    
    $testcase = array("sample.txt", "sample.jpg", "sample.case.txt");
    
    function extension($filename){
        return substr(strrchr($filename, '.'), 1);
    }
    
    foreach($testcase as $test) {
        echo "Extension from $test is " . extension($test) . "<br/>";
    } 
    
    ?>
  5. Ordinamento di un array

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    <?php
    
    $data_list = array("lychee", "pineapple", "apple", "mango", "strawberry", "banana", "orange", "grape", "guava");
    
    sort($data_list);
    foreach($data_list as $data) {
        echo $data . "<br/>";
    } 
    
    ?>
  6. Tempo trascorso da una certa data

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    <?php
    
    $testcase = array("12-03-1985", "15-01-2007", "04-04-2009", "01-01-2009", "30-03-2009", "07-04-2009", "07-04-2009 11:12");
    
    define("MINUTE", 60);
    define("HOUR", 3600); // 60 * 60 
    define("DAY", 86400); //  60 * 60 * 24 
    define("WEEK", 604800); // 60 * 60 * 24 * 7
    define("MONTH", 2592000); // 60 * 60 * 24 * 30
    define("YEAR", 31536000); // 60 * 60 * 24 * 365
    
    function timeSince($date) {
    
        $since = abs(strtotime('now') - strtotime($date));
    
        if($since > YEAR) {
            $year = floor($since / YEAR);
            return "more than $year year(s) ago";
        }    
    
        if ($since > MONTH) {
            $month = floor($since / MONTH);
            return "about $month month(s) ago";
        } 
    
        if ($since > WEEK) {
            $week = floor($since / WEEK);
            $day = floor(($since - ($week * WEEK)) / DAY);
            return "about $week week(s), and $day day(s) ago";
        }
    
        if ($since > DAY) {
            $day = floor($since / DAY);
            $hour = floor(($since - ($day * DAY)) / HOUR);
            return "about $day day(s), $hour hour(s) ago";
        }
    
        if ($since > HOUR) {
            $hour = floor($since / HOUR);
            $minute = floor(($since - ($hour * HOUR)) / MINUTE);
            return "about $hour hour(s), $minute minute(s) ago";
        }
    
        if ($since > MINUTE) {
            $minute = floor($since / MINUTE);
            return "$minute minute(s) ago";
        }
    
        return "under 1 minute ago";	
    
    }
    
    foreach($testcase as $test) {
        echo "Time since $test is " . timeSince($test)."<br/>";
    }   
    
    ?>

Read the rest of this entry »

Category: Programmazione, Siti Web, tutorial | No Comments »

Sliding Doors con CSS

Maggio 9th, 2009 by Giovanni Caputo

Di seguito il link al tutorial sullo sliding doors.

[Ordinary example of CSS-based tabs, using flat colors and squared-off corners.]

[Stylized tabs using rounded corners and subtle three-dimensional shading.]

Tutorial

Category: Programmazione, Siti Web | No Comments »

Gallerie di immagini per il proprio sito web

Maggio 8th, 2009 by Giovanni Caputo

1. Gallery 2

2. Zenphoto

3. Minishowcase

4. TripTracker

5. Dynamic Drive CSS Image Gallery

6. Ajax Photo Gallery

Legge dall’XML di  Picasa

7. SimpleViewer

8. Jalbum

9. Coppermine

10. Plogger

11. CSS Play

12. Polaroid Gallery

13. dfGallery

14. (E)2 Photo Gallery

15. AutoViewer

16. Smooth Gallery

17. Satellite

18. Hoverbox

19. Flash Gallery

20. HighSlide JS

21. Pixelpost

22. Script and Style Auto-Generated Photo Gallery

23. Postcard Viewer

24. Andrew Berg’s Flash Photo Browser

25. Flickr Photo Album for WordPress

26. Galleria

27. Lightview

28. Fancy Zoom

29. Imagevue

30. jpGalScroll Photo Gallery

31. nextgen gallery

32. Singapore

33. jQuery Cycle

34. Imagin

35. Phormer

36. Gullery

37. How to Build a Photo Gallery with CakePHP and Flickr

38. filebrowser

39. FAlbum

40. Slimbox 2

41. Animated JavaScript Slideshow

42. iPhotoToGallery

43. FrogJS Gallery

44. Create a Spectacular Photo Gallery with MooTools

45. Django-based photo gallery tutorial

46. Zen Gallery

47. folderblog

48. Duh Gallery

49. Original Photo Gallery

50. Making the ultimate dynamic image gallery in Flash 8

Category: Open Source, Programmazione, Siti Web, Tecnologia, tutorial | 1 Comment »

Debuggare e testare codice per il web con Firebugs

Maggio 7th, 2009 by Giovanni Caputo

Segnalo a tutti gli sviluppatori web, un video dal canale di googletechtalks, nel quale viene spiegato come testare ed effettuare il debug di codice web, come javascript e CSS.

Category: Programmazione, Siti Web, tutorial | No Comments »

Validatore sintattico di carte di credito

Maggio 7th, 2009 by Giovanni Caputo

Sastgroup fornisce codice PHP che permette di validare sintatticamente i numeri di carta di credito.

< ?

if( checkCreditCard (“40236007878787”, “Visa”, &$errornumber, &$errortext))
echo “OK”;
else echo”MO”;

function checkCreditCard ($cardnumber, $cardname, &$errornumber, &$errortext) {

$cards = array ( array (‘name’ => ‘American Express’,
‘length’ => ’15’,
‘prefixes’ => ‘34,37’,
‘checkdigit’ => true
),
array (‘name’ => ‘Carte Blanche’,
‘length’ => ’14’,
‘prefixes’ => ‘300,301,302,303,304,305,36,38’,
‘checkdigit’ => true
),
array (‘name’ => ‘Diners Club’,
‘length’ => ’14’,
‘prefixes’ => ‘300,301,302,303,304,305,36,38’,
‘checkdigit’ => true
),
array (‘name’ => ‘Discover’,
‘length’ => ’16’,
‘prefixes’ => ‘6011’,
‘checkdigit’ => true
),
array (‘name’ => ‘Enroute’,
‘length’ => ’15’,
‘prefixes’ => ‘2014,2149’,
‘checkdigit’ => true
),
array (‘name’ => ‘JCB’,
‘length’ => ‘15,16’,
‘prefixes’ => ‘3,1800,2131’,
‘checkdigit’ => true
),
array (‘name’ => ‘Maestro’,
‘length’ => ‘16,18’,
‘prefixes’ => ‘5020,6’,
‘checkdigit’ => true
),
array (‘name’ => ‘MasterCard’,
‘length’ => ’16’,
‘prefixes’ => ‘51,52,53,54,55’,
‘checkdigit’ => true
),
array (‘name’ => ‘Solo’,
‘length’ => ‘16,18,19’,
‘prefixes’ => ‘6334,6767’,
‘checkdigit’ => true
),
array (‘name’ => ‘Switch’,
‘length’ => ‘16,18,19’,
‘prefixes’ => ‘4903,4905,4911,4936,564182,633110,6333,6759’,
‘checkdigit’ => true
),
array (‘name’ => ‘Visa’,
‘length’ => ‘13,16’,
‘prefixes’ => ‘4’,
‘checkdigit’ => true
),
array (‘name’ => ‘Visa Electron’,
‘length’ => ’16’,
‘prefixes’ => ‘417500,4917,4913’,
‘checkdigit’ => true
)
);

$ccErrorNo = 0;

$ccErrors [0] = “Unknown card type”;
$ccErrors [1] = “No card number provided”;
$ccErrors [2] = “Credit card number has invalid format”;
$ccErrors [3] = “Credit card number is invalid”;
$ccErrors [4] = “Credit card number is wrong length”;

// Establish card type
$cardType = -1;
for ($i=0; $i

// See if it is this card (ignoring the case of the string)
if (strtolower($cardname) == strtolower($cards[$i][‘name’])) {
$cardType = $i;
break;
}
}

// If card type not found, report an error
if ($cardType == -1) {
$errornumber = 0;
$errortext = $ccErrors [$errornumber];
return false;
}

// Ensure that the user has provided a credit card number
if (strlen($cardnumber) == 0) {
$errornumber = 1;
$errortext = $ccErrors [$errornumber];
return false;
}

// Remove any spaces from the credit card number
$cardNo = str_replace (‘ ‘, ”, $cardnumber);

// Check that the number is numeric and of the right sort of length.
if (!eregi(‘^[0-9]{13,19}$’,$cardNo)) {
$errornumber = 2;
$errortext = $ccErrors [$errornumber];
return false;
}

// Now check the modulus 10 check digit – if required
if ($cards[$cardType][‘checkdigit’]) {
$checksum = 0; // running checksum total
$mychar = “”; // next char to process
$j = 1; // takes value of 1 or 2

// Process each digit one by one starting at the right
for ($i = strlen($cardNo) – 1; $i >= 0; $i–) {

// Extract the next digit and multiply by 1 or 2 on alternative digits.
$calc = $cardNo{$i} * $j;

// If the result is in two digits add 1 to the checksum total
if ($calc > 9) {
$checksum = $checksum + 1;
$calc = $calc – 10;
}

// Add the units element to the checksum total
$checksum = $checksum + $calc;

// Switch the value of j
if ($j ==1) {$j = 2;} else {$j = 1;};
}

// All done – if checksum is divisible by 10, it is a valid modulus 10.
// If not, report an error.
if ($checksum % 10 != 0) {
$errornumber = 3;
$errortext = $ccErrors [$errornumber];
return false;
}
}

// The following are the card-specific checks we undertake.

// Load an array with the valid prefixes for this card
$prefix = split(‘,’,$cards[$cardType][‘prefixes’]);

// Now see if any of them match what we have in the card number
$PrefixValid = false;
for ($i=0; $i
$exp = ‘^’ . $prefix[$i];
if (ereg($exp,$cardNo)) {
$PrefixValid = true;
break;
}
}

// If it isn’t a valid prefix there’s no point at looking at the length
if (!$PrefixValid) {
$errornumber = 3;
$errortext = $ccErrors [$errornumber];
return false;
}

// See if the length is valid for this card
$LengthValid = false;
$lengths = split(‘,’,$cards[$cardType][‘length’]);
for ($j=0; $j
if (strlen($cardNo) == $lengths[$j]) {
$LengthValid = true;
break;
}
}

// See if all is OK by seeing if the length was valid.
if (!$LengthValid) {
$errornumber = 4;
$errortext = $ccErrors [$errornumber];
return false;
};

// The credit card is in the required format.
return true;
}
?>

Read the rest of this entry »

Category: Programmazione, Siti Web, Tecnologia | No Comments »

Piattaforma WAMPP da cd o usb

Maggio 4th, 2009 by Giovanni Caputo

Server2Go è una soluzione WAMPP (Windows, Apache, MySQL, Php, Perl) che può girare da un drive rimovibile come un cd, dvd, penna usb o flash card.

Category: Open Source, Programmazione, Siti Web | No Comments »

Tutorial per convertire file psd in html e CSS

Maggio 2nd, 2009 by Giovanni Caputo

1.  The Design Lab: PSD Conversion

2. From PSD to HTML, Building a Set of Website Designs Step by Step

3.  Coding: Design Lab TV Styled Layout

4. PSD to CSS/HTML in Easy Steps – Part 1

5. Slice and Dice that PSD

6.  Converting a Photoshop Mockup: Part Two, Episode One

7.  Build a Sleek Portfolio Site from Scratch

8. Portfolio Layout – Learn To Code It

9. Converting a Design From PSD to HTML

10.  How to Convert a PSD to XHTML

11. How to Convert PSd to Html

12.  Creating a CSS Layout from scratch

13. PSD > HTML/CSS

14. Encoding a Photoshop Mockup into XHTML & CSS

15.  Css Template Tutorial

16. Portfolio Layout 4: The Code

17. Tutorial Coding a Layout

18.  Converting a Photoshop Mockup

19.  Killer site – From Illustrator to Xhtm to Drubal

20.  From PSD to HTML

21.  Coding a Car Sales Layout

Read the rest of this entry »

Category: Programmazione, Siti Web | No Comments »

Script per ritagliare immagini

Aprile 29th, 2009 by Giovanni Caputo

  1. cfImageCropper -Scritto in Coldfusion.
  2. jsCropper – Basato su  Prototype e script.aculo.us.
  3. mooImageCrop – basato su mootools.
  4. Image Crop – Scritto in Javascript e php ma richiede l’installazione di ImageMagick.
  5. Ajax Image Editor – Script di editing di immagini che permette taglio, ridimensionamento e rotazione.
  6. Flash Image Crop – Scritto in Actionscript e PHP.
  7. Flash Based Cropping tool – Anche questo scritto in Actionscript e PHP.
  8. MooCrop Usa mootools e permette di modificare il colore della maschera.

Category: Open Source, Programmazione, Siti Web, Tecnologia, tutorial | No Comments »

Scroller di immagini in JQuery

Aprile 23rd, 2009 by Giovanni Caputo

Finished Product

Se volete realizzare uno scroller di immagini in jquery potete sfruttare il seguente tutorial:

LINK

Category: Programmazione, Siti Web, Tecnologia, tutorial | No Comments »

Realizzare una tag-cloud con GWT (Google Web Toolkit)

Aprile 21st, 2009 by Giovanni Caputo

Ciao a tutti, volevo segnalare il seguente tutorial dove viene spiegato come realizzare una tag-cloud in GWT (Google Web Toolkit).

Tag Cloud

Con lo stesso tutorial si può capire anche come far interagire AJAX realizzato in GWT ad un server che risponde con dati in formato JSON.

Lo consiglio a tutti gli sviluppatori che vogliono realizzare codice in Javascript conoscendo solamente Java.

TUTORIAL

Category: programmi, Siti Web, Tecnologia, tutorial | No Comments »