PHP Captcha

Derrière ce nom imprononçable ce cache un truc relativement simple. Afin de lutter contre les inscriptions massive de bot et autre joyeuseté webispherique, la majorité des gros sites utilisent ce système (google/yahoo/etc…)

Qu’est ce qu’un captcha pere castor ? Définition sur wikipedia

Donc voila un captcha developpé par moi même

captcha.php

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
class captcha {
 
    public function init() {
        $alpha = array('A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','1','2','3','4','5','6','7','8','9','a','b','c','d',
        'e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z');
        shuffle($alpha);
        $captcha = '';
        for ($j = '0';$j < 7;$j++) {
            $i = rand(0,(count($alpha)-1));
            $captcha .= $alpha[$i];
        }
        $_SESSION['captcha'] = strtoupper($captcha);
        $width = 200;
        $height = 50;
        $image = imagecreatetruecolor($width,$height);
        $background_color = imagecolorallocate($image, rand(240, 255), rand(240, 255), rand(240, 255));
        imagefilledrectangle($image, 0, 0, $width, $height, $background_color);
        $color1 = imagecolorallocate($image, rand(150, 255), rand(150, 255), rand(150, 255));
        $font = imageloadfont('fonts/anonymous.gdf');
        $color2 = imagecolorallocate($image, rand(0, 150), rand(0, 150), rand(0, 150));
        imagestring ($image, $font, 20, 4, $captcha, $color2);
        for($j = '0';$j <= rand(4000, 5000);$j++)
        {
            imagesetpixel($image,rand(1, $width-1),rand(1, $height-1),$color1);
        }
        for($i = 0;$i < $width;$i = $i + 10)
        {
            imageline($image,$i,0,$i,$height,$color1);
        }
        for($i = 0;$i < $height;$i = $i + 10)
        {
            imageline($image,0,$i,$width,$i,$color1);
        }
        header('Content-Type: image/png');
        imagepng($image);
        imagedestroy($image);
    }
}

Utilisation

$captcha = new captcha();
$captcha->init();

Pour l’afficher

<img src="captcha.php" alt="captcha"/>

Et comme tjs le captcha en action sur Quote de Porc

La police utilisé pour le captcha (anonymous.gdf)

PHP Class de pagination

Voila une petite classe bien pratique pour generer une pagination, tres rapide et simple a mettre en place.

je m’en sert une peu partout aussi bien dans mon travail que dans mes projets completements debiles et sans but personelle.

Pagination.class.php

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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
<?php
class pagination {
    private $byPage; // int, Nombre d'enregistrement a afficher par page
    private $rows; // int, Nombre total d'enregistrement
    public $goFirst;// string, Caractere ou chaine de caractere pour aller a la premiere page
    public $goPrevious;// string, Caractere ou chaine de caractere pour aller a la page precedente
    public $goLast;// string, Caractere ou chaine de caractere pour aller a la derniere page
    public $goNext;// string, Caractere ou chaine de caractere pour aller a la page suivante
 
    public function __construct() {
        if(isset($_GET['p'])) {
            $this->currentPage = intval($_GET['p']);
        } else {
            $this->currentPage = '1';
        }
        $this->goFirst = '&lt;&lt;&nbsp;';
        $this->goPrevious = '&lt;';
        $this->goLast = '&nbsp;&gt;&gt;';
        $this->goNext = '&gt;';
    }
 
    public function __set($var, $value) {
        $this->$var = intval($value);
    }
 
    public function __get($var) {
        if(isset($this->$var)) {
            return $this->$var;
        }
    }
    /*
    // Retourne le nombre de page
    // return int
    */
    private function pagesCount()
    {
        $pagesCount = ceil($this->rows / $this->byPage);
        return $pagesCount;
    }
    /*
    // Calcule le point de depart de la LIMIT d'une requete sql
    // en fonction de la page actuelle et du nombre d'enregistrement voulu
    // return int
    */
    public function fromPagination() {
        if(!isset($this->pagesCount)) {
            $this->pagesCount = $this->pagesCount();
        }
        if($this->currentPage > 0 && $this->rows > 0) {
            if($this->currentPage > 0 && $this->currentPage <= $this->pagesCount) {
                $from = ($this->currentPage - 1) * $this->byPage;
            }
            else {
                $from = $this->currentPage * $this->byPage;
            }
        }
        else {
            $from = '0';
        }
        return $from;
    }
    /*
    // La pagination en elle meme
    // @param int $pageBefore: Nombre de page que l'on veu afficher avant la page actuelle (defaut: 10)
    // @param int $pageAfter: Nombre de page que l'on veu afficher apres la page actuelle  (defaut: 10)
    //
    // $array['page']: L'affichage de la page
    // $array['current']: defini a 1 pour la page actuelle, 0 pour les autres
    // $array['p']: Le numero de la page pour les lien
    //
    // return array
    */
    public function pages($pageBefore = '10', $pageAfter = '10') {
        if(!isset($this->pagesCount)) {
            $this->pagesCount = $this->pagesCount();
        }
        if($this->pagesCount > '1') {
            $pages = array();
            if($this->currentPage > $this->pagesCount) {
                $this->currentPage = $this->pagesCount;
            }
            if ($this->currentPage > 1) {
                $pages[] = array('page' => $this->goFirst,
                'current' => '0',
                'p' => '1'
                );
                $previous = $this->currentPage - 1;
                $pages[] = array('page' => $this->goPrevious,
                'current' => '0',
                'p' => $previous
                );
            }
            if($this->pagesCount <= '1') {
                $pages[] = array('page' => '1',
                'current' => '0',
                'p' => '1'
                );
            }
            else {
                for($i = $this->currentPage - $pageBefore,$j = $this->currentPage + $pageAfter; $i < $j; $i++) {
                    if($i >= 1 && $i <= $this->pagesCount) {
                        if($i == $this->currentPage) {
                            $pages[] = array('page' => $this->currentPage,
                            'current' => '1',
                            'p' => $this->currentPage
                            );
                        }
                        else {
                            $pages[] = array('page' => $i,
                            'current' => '0',
                            'p' => $i
                            );
                        }
                    }
                }
            }
            if ($this->currentPage < $this->pagesCount) {
                if ($this->currentPage != $this->pagesCount)
                {
                    $next = $this->currentPage + 1;
                    $pages[] = array('page' => $this->goNext,
                    'current' => '0',
                    'p' => $next
                    );
                }
                $pages[] = array('page' => $this->goLast,
                'current' => '0',
                'p' => $this->pagesCount
                );
            }
            return $pages;
        }
    }
}
?>

Utilisation

1
2
3
4
5
$pagination = new pagination();
$pagination->byPage = 5;
$pagination->rows = 150; // nombre d'enregistrement dans une table retourner par mysql_num_rows() par exemple ou autre , a vous de jouer
$from = $pagination->fromPagination(); // sert pour les requetes sql exemple LIMIT $from, $pagination->byPage
$pages = $pagination->pages();

Affichage

1
2
3
4
5
6
7
8
9
10
11
<?if(isset($pages)) {?>
<div class="pagination">
    <?foreach ($pages as $key){?>
    <?if($key['current'] == 1) {?>
    <a href="?p=<?=$key['p']?>" class="active"><?=$key['page']?></a>
    <?} else {?>
    <a href="?p=<?=$key['p']?>" class="inactive"><?=$key['page']?></a>
    <?}?>
    <?}?>
</div>
<?}?>

Vous pouvez bien sur la voir en action sur Quote de porc

PHP Nuage de tag

Un petit bout de code pour un nuage de tag en php fait par mes soins ^^

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
public function makeCloud() {
    $array = $this->getTags();
    $barriere = array();
    $inc = max($array)/$this->levels;
    $barriere = array();
    $barriere[] = array('value' => 0,
                            'css' => $this->cssClassPrefix.'0');
    $float = 0;
    for($i = 1;$i <= $this->levels;$i++) {
        $float = $float+$inc;
        $barriere[] = array('value' => $float,
                                'css' => $this->cssClassPrefix.$i);
    }
    $tagcloud = array();
    $previous_value = $barriere[0]['value'];
    foreach($barriere as $key) {
        foreach($array as $row => $value) {
            if($key['value'] >= $value && $previous_value <= $value && !key_exists($row, $tagcloud)) {
                $tagcloud[$row] = array('tag' => $row,
                                            'cssclass' => $key['css']
                );
            }
        }
        $previous_value = $key['value'];
    }
    return $tagcloud;
}

Un petit exemple d’utilisation ici, Exemple de nuage de tag.

Petite precision ligne 2

« $array = $this->getTags(); »

retourne un tableau sous cette forme

Array( [sexe] => 1.74246 [humour] => 1.76121 etc … )

je vous met la fonction juste pour information ^^

1
2
3
4
5
6
7
8
9
10
11
public function getTags() {
    $params = array('table' => 'tagcloud',
                    'fields' => 'tag, count',
                    'order' => 'count ASC',
                    'limit' => $this->view_tags);
    $result = $this->db->select($params);
    foreach($result as $key) {
        $tagsArray[$key['tag']] = $key['count'];
    }
    return $tagsArray;
}

Pyramide 2 le retour

Ma pyramide de cannette (la femelle du canard ???) de coca light/pas light/zero

pyramide2

 

Je ne pense pas pouvoir en faire une plus grosse faute de place :(

Pour m’aider:

Envoyer des milliards de mails a coca cola pour leurs demander de faire des canettes plus petite ou bien a Bayard Presse pour leurs demander de me donner un bureau plus grand.

Merci.