XHTMLforum

XHTMLforum (http://xhtmlforum.de/index.php)
-   Serveradministration und serverseitige Scripte (http://xhtmlforum.de/forumdisplay.php?f=80)
-   -   Kontaktformular von fox - Anpassungen (http://xhtmlforum.de/showthread.php?t=61680)

Sand10 07.07.2010 13:41

Kontaktformular von fox - Anpassungen
 
Hallo nochmal :)

"Wenn sie so anfängt, dann will sie wieder was..." :oops:

Bitte bitte wimmelt mich nicht ab. Ich weiß, dass mich etliche Grundlagen fehlen. Ich bin auch schon fleißig am Lernen, aber das geht kleider nicht über Nacht...


Unter Kontaktformular mit Spamschutz habe ich das Kontaktformular von fox eingebaut (vielen Dank für deine Vorarbeit für das script!).

Ich habe ein weiteres Feld hinzugefügt (phone).

Außerdem habe ich gelernt, dass diese Zeile für den Emailtext verantwortlich ist. "phone" habe ich bereits eingefügt:

PHP-Code:

mail ('abc@abc.de''Nachricht über das Kontaktformular von ' $name$name ' (Email:' $email  $phone ") hat eine Nachricht geschrieben:\n" $nachricht,'From: kontaktformular@abc.de' ); 

Ich bekomme es aber leider nicht vor "$phone" ein "Telefon:" einzufügen.

In der Email soll stehen:
$name (Email: $email / Telefon: $phone) hat eine Nachricht geschrieben:


Wie muss ich das schreiben?



Leider muss ich euch noch eine Fragen stellen:

Was muss ich ändern, dass in der Email noch das Absendedatum+Uhrzeit eingetragen wird?




ICH DANKE EUCH für Eure Hilfsbereitschaft
Liebe Grüße
Nadine










Anbei der komplette code.


index.php

PHP-Code:

<?php

require_once 'secureForm.php';

$form_sent false

// Eine neue Instanz der Klasse wird erzeugt 
$form = new SecureForm(); 

// Für jedes Formfeld wird eine zufällige (CSS-)ID erzeugt und in den Variablen gespeichert 
$id_name $form->getID('name'); 
$id_email $form->getID('email');
$id_phone $form->getID('phone');  
$id_nachricht $form->getID('nachricht'); 

// Der Submit-Button muss nicht zwangsweise "anonymisiert" werden 
$id_submit $form->getID('submit'); 


// Die CSS-Datei wurde vom Browser angefordert und wird abgeschickt 
if (isset ($_GET['getCSS'])) { 
    
header ("Content-Type: text/css"); 
    
$form->generateCSS(); 
    exit; 


// Es wird nun geprüft, ob der Submit-Knopf gedrückt und das Formular abgesendet wird. 
// Die Formularfelder sind mit den verschiedenen IDs als Namen gespeichert. 
if (!empty ($_POST[$id_submit])) { 
    
$form_sent true
     
    
// Zuerst überprüfen wir, ob alle gewünschten Felder ausgefüllt sind 
    
if (!empty ($_POST[$id_name]) && !empty ($_POST[$id_email]) && !empty ($_POST[$id_phone]) && !empty ($_POST[$id_nachricht])) { 
         
        
// Danach wird geprüft, ob das versteckte Feld ausgefüllt wurde. 
        // Sollte das der Fall sein haben wir es höchstwahrscheinlich mit einem Spambot zu tun 
        // Hier könnten auch weitere Aktionen unternommen werden, z.B. loggen und/oder sperren der IP des Bots, o.ä. 
        
if (!empty ($_POST[$form->getSecretID()])) { 
            
session_destroy (); 
            echo 
'Spambot erkannt.'
            exit; 
        } 
         
        
// Nun können wir die E-Mail versenden 
        
$name $_POST[$id_name]; 
        
$email $_POST[$id_email];
        
$phone $_POST[$id_phone]; 
        
$nachricht $_POST[$id_nachricht]; 
         
        
// E-Mail verschicken...  
         
         
mail ('abc@abc.de''Nachricht über das Kontaktformular von ' $name$name ' (Email:' $email  $phone ") hat eine Nachricht geschrieben:\n" $nachricht,'From: kontaktformular@abc.de' );  
         
         
         
         
        
session_destroy (); 
        exit; 
    } 
     
    
// Falls nicht alle Felder ausgefüllt sind sollte die Session-ID sowie die geheimen IDs erneuert werden, 
    // um mögliche Brut-Force-Angriffe von Bots aus dem Weg zu gehen. 
    // WICHTIG: Vor dem erneuern dürfen die Header noch nicht gesendet werden! 
    
$form->regenerateIDs(); 


/* Nun wird das Formular erzeugt: 
Eventuell wird es auch in Templates eingebunden o.ä. 
Vorher sollten normalerweise der HTML-Kopf, etc. kommen 
Wichtig ist, dass die Session-ID übergeben wird! 
Alternativ kann diese auch mit einem versteckten Feld und POST übergeben werden, 
PHP erkennt diese automatisch. 
Außerdem dürfen die beiden Stylesheets nicht fehlen! 

Das erste Stylesheet ist die Vorgabe für die normalen Felder. 
Da diese Angabe *vor* den anderen kommt, gilt diese. 
Bei den versteckten Feldern wird zusätzlich ein !important hingehängt, damit werden die Felder unsicht- und -hörbar.*/ 

echo '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>Kontaktformular mit Spamschutz</title>
    <style type="text/css">
    /*<[!CDATA[*/
        body { 
            color: #555;
            font: 0.8em \'Lucida Grande\', Verdana, Tahoma, sans-serif;
        }
        fieldset {
            border: 1px dotted #A6A6A6;
            padding: 10px;
            margin: 5px;
            margin-bottom: 20px;
        }
        fieldset legend {
            font-weight: bold;
        }
        
        /* Dieser Abschnitt ist fuer das Skript wichtig */
        .form input, .form textarea { 
            position: static !important; 
            display: block !important; 
            speak: normal !important; 
        }
    /*]]>*/
    </style>
    <!-- Hier nicht die Session-ID vergessen -->
    <link rel="stylesheet" type="text/css" href="index.php?getCSS&amp;' 
SID '" media="all" /> 
</head>

<body>

<fieldset>
    <legend>Kontaktformular</legend>

    <!-- Auch hier ist die Session-ID wichtig, um die IDs spaeter wieder den Feldern zuzuordnen -->
    <form action="index.php?' 
SID '" method="POST" class="form">'

// Nun werden die einzelnen Eingabefelder ausgegeben. 
// Dabei wird gleich überprüft, ob das Formular bereits abgesendet wurde und ob Felder fehlen. 
echo '
    <label>Ihr Name' 
. ((($form_sent && empty ($_POST[$id_name]))) ? ' (fehlt)' '') . ':</label> <input type="text" id="' $id_name '" name="' $id_name '" value ="' . ((!empty ($_POST[$id_name])) ? htmlspecialchars ($_POST[$id_name]) : '') . '" /><br /> 
    <label>Ihre E-Mail-Adresse' 
. ((($form_sent && empty ($_POST[$id_mail]))) ? ' (fehlt)' '') . ':</label> <input type="text" id="' $id_email '" name="'  .$id_email '" value="' . ((!empty ($_POST[$id_email])) ? htmlspecialchars ($_POST[$id_email]) : '') . '" /><br /> 
        <label>Ihre Telefonnummer' 
. ((($form_sent && empty ($_POST[$id_phone]))) ? ' (fehlt)' '') . ':</label> <input type="text" id="' $id_phone '" name="'  .$id_phone '" value="' . ((!empty ($_POST[$id_phone])) ? htmlspecialchars ($_POST[$id_phone]) : '') . '" /><br /> 
    <label>Ihre Nachricht' 
. ((($form_sent && empty ($_POST[$id_nachricht]))) ? ' (fehlt)' '') . ':</label><br /> 
    <textarea name="' 
$id_nachricht '" id="' $id_nachricht '">' . ((!empty ($_POST[$id_nachricht])) ? htmlspecialchars ($_POST[$id_nachricht]) : '') . '</textarea><br />
    <input type="text" name="' 
$form->getSecretID() . '" id="' $form->getSecretID() . '" value="" />' // Auch das versteckte Feld bekommt seinen Platz 
    
'
    <input type="submit" name="' 
$id_submit '" id="' $id_submit '" value="Formular absenden" />
    
    </form>

</fieldset>

</body>
</html>'


// Fertig. :) 
// Man könnte das versteckte Feld noch an zufälligen Positionen erscheinen lassen, um es den Spambots nicht zu leicht zu machen.


secureForm.php:

PHP-Code:

<?php

/**
 * No more SPAM: Secure your form...
 * 
 * INSTALL:
 * You *must* include a css-definition for the form fields like this:
 * input, textarea { position: static !important; display: block !important; speak: normal !important; }
 * 
 * This is very important, otherwise the user wont see ANY fields!
 * You also have to include the class-generated css-definitions in your document.
 *
 * @author Steffen Manzer, noecho.de <noecho.smanzer@gmail.com>
 * @license    GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
 * @see [url]http://xhtmlforum.de/51007-bitte-austesten-formular-spamschutz.html[/url]
 *
 */
class SecureForm



{
    
    private
        
$secret        null,
        
$keys        = array (),
        
$confusing    = array ();
    
    
    
    public function 
__construct () {
        
// make sure that the session id will not be saved in cookies
        
@ini_set('session.use_cookies'0);
        @
ini_set('session.use_only_cookies'0);
    
    
session_start();    
        
        if (!empty (
$_SESSION['secret'])) {
            
$this->secret $_SESSION['secret'];
        } else {
            
$this->secret $this->generateID();
        }
        
        if (!empty (
$_SESSION['keys'])) {
            
$this->keys $_SESSION['keys'];
        }
        
        if (!empty (
$_SESSION['confusing'])) {
            
$this->confusing $_SESSION['confusing'];
        }
        
        
// dont cache this or you'll be shot!
        
if (!headers_sent ()) {
            
header('Cache-Control: no-cache, must-revalidate');
            
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
        }
    }
    
    
/**
     * Save keys in Session
     *
     */
    
public function __destruct ()
    {
        
$_SESSION['secret'] = $this->secret;
        
$_SESSION['keys'] = $this->keys;
        
$_SESSION['confusing'] = $this->confusing;
    }
    
    
/**
     * Generate a random ID
     *
     * @return string
     */
    
protected function generateID ()
    {
        return 
substr (chr (rand (97122)) . sha1 (uniqid ()), 0rand (58));
    }

    
/**
     * Add new field
     *
     * @param $label label for the field
     * @return string $key
     */
    
public function getID ($label)
    {
        if (empty (
$this->keys[$label])) {
            
$this->keys[$label] = $this->generateID();
        }
        return 
$this->keys[$label];
    }
    
    
/**
     * return the secret key
     *
     * @return unknown
     */
    
public function getSecretID ()
    {
        return 
$this->secret;
    }
    
    
/**
     * Generate new Session-ID and new keys
     * WARNING: This function must be called before any output.
     * The headers may not be sent yet!
     *
     * @return bool
     */
    
public function regenerateIDs ()
    {
        if (
headers_sent ()) return false;
        @
session_regenerate_id (true);
        
$this->secret $this->generateID();
        foreach (
$this->keys as $key => $value) {
            
$this->keys[$key] = $this->generateID();
        }
        
$this->confusing = array ();
        return 
true;
    }
    
    
/**
     * Generate the css necessary for the forms
     *
     * @param $return return the generated css or output it directly
     * @return mixed string|null
     */
    
public function generateCSS ($return false)
    {
        
$content '@media all{';
        
        
$ids array_values ($this->keys);
        if (empty (
$this->confusing)) {
            for (
$i 0$i < (rand(815)); $i++) {
                
$id $this->generateID();
                
$ids[] = $id;
                
$this->confusing[] = $id;
            }
        } else {
            
$ids $ids $this->confusing;
        }

        
sort ($ids);    
        
$num count($ids);
        
$pos array_rand ($ids);
        
        
$displayed false;
        for (
$i 0$i $num$i++) {
            
            
// hidden field
            
if (($pos == $i) && (!$displayed)) {
                
$content .= '#' $this->secret ' {';
                
$content .= "position:absolute !important;left:-99999px;speak:none !important;display:none !important;}";
                
$displayed true;
                
$i--;
            
            
// any other form field
            
} elseif (in_array ($ids[$i], $this->keys)) {
                
$content .= '#' $ids[$i] . " {";
                
$content .= "position:absolute;left:-99999px;speak:none;display:none;}";
        
            
// random, confusing field
            
} else {
                
$r = (rand (01) == 1);
                
$content .= '#' $ids[$i] . " {";
                
$content .= "position:absolute" . ($r ' !important' '') . ";left:-99999px;speak: none" . ($r ' !important' '') . ";display: none" . ($r ' !important' '') . ';}';
            }
        }
        
$content .= '}';
        if (
$return) {
            return 
$content;
        } else {
            echo 
$content;
        }
    }
}


Praktikant 07.07.2010 15:45

So könnte das gehen:
PHP-Code:

mail ('deine@email.de''Nachricht über das Kontaktformular von ' $name$name ' (Email:' $email  ' Telefon: ' $phone ") hat um ' . date('d.m.Y H:i:s', time()) . ' eine Nachricht geschrieben:\n" $nachricht,'From: ' $email); 

Sowas ist aber auch bei google zu finden ;)

Sand10 07.07.2010 17:35

Du hast recht, das gibts bei google, aber als Anfänger weiß ich garnicht wonach ich suchen soll.

Danke für deine Mühe, aber das mit dem Datum hat noch nicht funktioniert...



Jetzt hab ich mal gegooglt und vermute, dass ich noch mehr hinzufügen muss als du geschrieben hast. Die Variablen sind noch nicht beschrieben, oder?


Folgende Fetzen habe ich gefunden und dazugeschrieben:

PHP-Code:

$day      $name_tag[$num_tag];
$year      date("Y");
$day_num   date("d");
$month      date("m");
$time      = (date("H:i")); 


Das war aber leider der falsche Ansatz...

Praktikant 07.07.2010 18:34

Ich habe doch gar keine Variablen genutzt :shock:

Aber einen Fehler habe ich gemacht.... Probiers mal bitte hier mit:
PHP-Code:

mail ('deine@email.de''Nachricht über das Kontaktformular von ' $name$name ' (Email:' $email  ' Telefon: ' $phone ") hat um " date('d.m.Y H:i:s'time()) . " eine Nachricht geschrieben:\n" $nachricht'From: ' $email); 

date() und time() sind beides php-Funktionen 'd.m.Y H:i:s' gibt das Datum im Format tt.mm.YYYY hh:mm:ss an, also deutsches Datumsformat mit Uhrzeit inklusive Sekunden.

Sand10 08.07.2010 13:26

Funktioniert super! DANKE!!!


Alle Zeitangaben in WEZ +2. Es ist jetzt 07:23 Uhr.

Powered by vBulletin® Version 3.8.11 (Deutsch)
Copyright ©2000 - 2024, vBulletin Solutions, Inc.

© Dirk H. 2003 - 2023