Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: Formularz kontaktowy z tokenem.
Pozycjonowanie i Optymalizacja > Projektowanie stron > PHP i MySQL
dwain
Witam.

Nie znam się na php dlatego prosiłbym o pomoc.
Dostaje sporo SPAMU przez formularz kontaktowy, które są wysyłane przez boty.

Obecnie korzystam z :

contactscript.php
KOD
<?php
    // VALUES FROM THE FORM
    $name        = $_POST['name'];
    $email        = $_POST['email'];
    $message    = $_POST['msg'];

    // ERROR & SECURITY CHECKS
    if ( ( !$email ) ||
         ( strlen($_POST['email']) > 200 ) ||
         ( !preg_match("#^[A-Za-z0-9](([_\.\-]?[a-zA-Z0-9]+)*)@([A-Za-z0-9]+)(([\.\-]?[a-zA-Z0-9]+)*)\.([A-Za-z]{2,})$#", $email) )
       )
    {
        print "Error: Invalid E-Mail Address";
        exit;
    }
    if ( ( !$name ) ||
         ( strlen($name) > 100 ) ||
         ( preg_match("/[:=@\<\>]/", $name) )
       )
    {
        print "Error: Invalid Name";
        exit;
    }
    if ( preg_match("#cc:#i", $message, $matches) )
    {
        print "Error: Found Invalid Header Field";
        exit;
    }
    if ( !$message )
    {
        print "Error: No Message";
        exit;
    }
    if (eregi("\r",$email) || eregi("\n",$email)){
        print "Error: Invalid E-Mail Address";
        exit;
    }
    if (FALSE) {
        print "Error: You cannot send to an email address on the same domain.";
        exit;
    }


    // CREATE THE EMAIL
    $headers    = "Content-Type: text/plain; charset=iso-8859-1\n";
    $headers    .= "From: $name <$email>\n";
    $recipient    = "mojmail@gmail.com";
    $subject    = "Mail z strony";
    $message    = wordwrap($message, 1024);

    // SEND THE EMAIL TO YOU
    mail($recipient, $subject, $message, $headers);

    // REDIRECT TO THE THANKS PAGE
    header("location: thanks.php");
?>


oraz kodu w kontakt.html
KOD
<form name="form" method="post" action="contactform/contactscript.php">
<table width="400" border="0" cellspacing="2" cellpadding="2">
  <tr>
    <td width="200" valign="top" align="right">Imię i nazwisko: </td>
    <td width="200" valign="top" align="left">
      <input type="text" name="name" size="25" maxlength="200" />
    </td>
  </tr><tr>
    <td width="200" valign="top" align="right">Twój Email: </td>
    <td width="200" valign="top" align="left">
      <input type="text" name="email" size="25" maxlength="100" />
    </td>
  </tr><tr>
    <td width="200" valign="top" align="right">Treść wiadomości: </td>
    <td width="200" valign="top" align="left">
      <textarea name="msg" cols="25" rows="4"></textarea>
    </td>
  </tr><tr>
    <td width="200" valign="top"> </td>
    <td width="200" valign="top" align="left">
      <input type="reset" name="Reset" value="Wyczyść" />
      <input type="submit" name="Submit" value="Wyślij" />
    </td>
  </tr>
</table>


Znalazłem w Google, taki skrypt :

captcha.php
KOD
<?      
        session_start();
        $pool = '0123456789abcdefghijklmnopqrstuvwxyz';
        $img_width = 120;
        $img_height = 30;

        $str = '';
        for ($i = 0; $i < 7; $i++){
                $str .= substr($pool, mt_rand(0, strlen($pool) -1), 1);
        }

        $string = $str;
        $_SESSION['captcha'] = $string;

        $im = imagecreate($img_width, $img_height);

        $bg_color        = imagecolorallocate($im,163,163,163);
        $font_color   = imagecolorallocate($im,252,252,252);
        $grid_color   = imagecolorallocate($im,31,0,0);
        $border_color = imagecolorallocate ($im, 174, 174, 174);

        imagefill($im,1,1,$bg_color);

        for($i=0; $i<1600; $i++){

                $rand1 = rand(0,$img_width);
                $rand2 = rand(0,$img_height);
                imageline($im, $rand1, $rand2, $rand1, $rand2, $grid_color);

        }

        $x = rand(5, $img_width/(7/2));

        imagerectangle($im, 0, 0, $img_width-1, $img_height-1, $border_color);

        for($a=0; $a < 7; $a++){

                imagestring($im, 5, $x, rand(6 , $img_height/5), substr($string, $a, 1), $font_color);
                $x += (5*2); #odstęp

        }

        header("Content-type: image/gif");
        imagegif($im);
        imagedestroy($im);

?>



+index.php

KOD
<?
session_start();

if($_POST['captcha'] != $_SESSION['captcha']){

        echo '<font style="color:#c03400;">Niepoprawnie przepisałeś kod z obrazka</font><br>';

}else{

        #coś robisz :)
        
}
?>


Próbowałem je połączyć, bez skutku.Pierwszy wysyła maila, ale nie wyświetla tokenu.Drugi za to wymaga tokenu, ale nie wysyła maila - nie znalazłem w nim też miejsca, w którym mógłbym wpisać adres maila, na który ma wysyłać pocztę.
Byłbym wdzięczny za pomoc osobie, która powie co mam zmienić w jednym z nich, aby w trakcie wysyłania wiadomości przez formularz kontaktowy, trzeba było przepisać token.
mrbox
A nie lepiej tak?
KOD
<?php
    // VALUES FROM THE FORM
    $name        = $_POST['name'];
    $email        = $_POST['email'];
    $message    = $_POST['msg'];
    $answer = $_POST['answer']; // ZMIANA NR !

    // ERROR & SECURITY CHECKS
    if ( ( !$email ) ||
         ( strlen($_POST['email']) > 200 ) ||
         ( !preg_match("#^[A-Za-z0-9](([_\.\-]?[a-zA-Z0-9]+)*)@([A-Za-z0-9]+)(([\.\-]?[a-zA-Z0-9]+)*)\.([A-Za-z]{2,})$#", $email) )
       )
    {
        print "Error: Invalid E-Mail Address";
        exit;
    }
    if ( ( !$name ) ||
         ( strlen($name) > 100 ) ||
         ( preg_match("/[:=@\<\>]/", $name) )
       )
    {
        print "Error: Invalid Name";
        exit;
    }
    if ( preg_match("#cc:#i", $message, $matches) )
    {
        print "Error: Found Invalid Header Field";
        exit;
    }
    if ( !$message )
    {
        print "Error: No Message";
        exit;
    }
    if (eregi("\r",$email) || eregi("\n",$email)){
        print "Error: Invalid E-Mail Address";
        exit;
    }
        if ($answer != "6"){                      // ZMIANA 2 POCZĄTEK
                print "Error: Invalid answer";
                exit;
    }                                                      // ZMIANA 2 KONIEC
    if (FALSE) {
        print "Error: You cannot send to an email address on the same domain.";
        exit;
    }


    // CREATE THE EMAIL
    $headers    = "Content-Type: text/plain; charset=iso-8859-1\n";
    $headers    .= "From: $name <$email>\n";
    $recipient    = "mojmail@gmail.com";
    $subject    = "Mail z strony";
    $message    = wordwrap($message, 1024);

    // SEND THE EMAIL TO YOU
    mail($recipient, $subject, $message, $headers);

    // REDIRECT TO THE THANKS PAGE
    header("location: thanks.php");
?>


oraz kodu w kontakt.html
KOD
<form name="form" method="post" action="contactform/contactscript.php">
<table width="400" border="0" cellspacing="2" cellpadding="2">
  <tr>
    <td width="200" valign="top" align="right">Imię i nazwisko: </td>
    <td width="200" valign="top" align="left">
      <input type="text" name="name" size="25" maxlength="200" />
    </td>
  </tr><tr>
    <td width="200" valign="top" align="right">Twój Email: </td>
    <td width="200" valign="top" align="left">
      <input type="text" name="email" size="25" maxlength="100" />
    </td>
  </tr><tr>
    <td width="200" valign="top" align="right">Treść wiadomości: </td>
    <td width="200" valign="top" align="left">
      <textarea name="msg" cols="25" rows="4"></textarea>
    </td>
  </tr>ZMIANA 3 POCZĄTEK<tr>
    <td width="200" valign="top" align="right">Ile to jest trzy dodać trzy? </td>
    <td width="200" valign="top" align="left">
      <textarea name="answer" cols="25" rows="4"></textarea>
    </td>
  </tr>ZMIANA 3 KONIEC<tr>
    <td width="200" valign="top"> </td>
    <td width="200" valign="top" align="left">
      <input type="reset" name="Reset" value="Wyczyść" />
      <input type="submit" name="Submit" value="Wyślij" />
    </td>
  </tr>
</table>


I z botami się nie musisz użerać- i ludzie, nawet mający problemy ze wzrokiem też sobie poradzą. Najprostsze rozwiązania są często najlepsze wink.gif
dwain
Dziękuje bardzo.
Wszystko teraz działa elegancko =]

Pozdrawiam
Zybex
witam
korzystając z założonego już posta zapytam czy jest jakaś opcja by ten skrypt usprawnić o dynamiczne pytania sprawdzające ?? nie chce używać CAPTCHA, ze względu na trudność czasem z odczytaniem, najlepsze są tokeny typu 2+3, 6*2 itp gdyby dało się je jakoś generować dynamicznie było by już całkiem pozytywnie, więc może ktoś z Was zna jakieś nie skomplikowane rozwiązanie ??

Pozdrawiam
To jest wersja lo-fi głównej zawartości. Aby zobaczyć pełną wersję z większą zawartością, obrazkami i formatowaniem proszę kliknij tutaj.
Invision Power Board © 2001-2012 Invision Power Services, Inc.