0

I have an PHP guestbook. So I can write nothing into the form and submit, then it posts. Can I avoid this?

Here is my PHP code:

if (isset($_POST["Name"]) &&
    isset($_POST["Email"]) &&
    isset($_POST["Überschrift"]) &&
    isset($_POST["Kommentar"])) {
        $daten = array("überschrift" => $_POST["Überschrift"],
                       "eintrag" => $_POST["Kommentar"],
                       "autor" => $_POST["Name"],
                       "email" => $_POST["Email"],
                       "datum" => date("d.m.Y"));
        $daten = base64_encode(serialize($daten));
        if(!file_exists("gaestebuch.txt")) {
            $datei = fopen("gaestebuch.txt", "xb");
            fclose($datei); 
        }
        $altdaten = file_get_contents("gaestebuch.txt");
        if (file_put_contents("gaestebuch.txt", "$daten\r\n$altdaten") ) {
            echo "Eintrag hinzugefügt!";
        } else {
            echo "Fehler!";
        }
}
halfer
  • 19,824
  • 17
  • 99
  • 186
c.Bauer
  • 52
  • 11

4 Answers4

1

Check the the first value ISSET so you know it is a POST request then check if the rest are empty

if (isset($_POST["Name"]) &&
    !empty($_POST["Email"]) &&
    !empty($_POST["Überschrift"]) &&
    !empty($_POST["Kommentar"])) {
        $daten = array("überschrift" => $_POST["Überschrift"],
                       "eintrag" => $_POST["Kommentar"],
                       "autor" => $_POST["Name"],
                       "email" => $_POST["Email"],
                       "datum" => date("d.m.Y"));
        $daten = base64_encode(serialize($daten));
        if(!file_exists("gaestebuch.txt")) {
            $datei = fopen("gaestebuch.txt", "xb");
            fclose($datei); 
        }
        $altdaten = file_get_contents("gaestebuch.txt");
        if (file_put_contents("gaestebuch.txt", "$daten\r\n$altdaten") ) {
            echo "Eintrag hinzugefügt!";
        } else {
            echo "Fehler!";
        }
}

also consider using OR instead of &&

!empty($_POST["Email"]) OR
!empty($_POST["Überschrift"]) OR
!empty($_POST["Kommentar"]))

This means if only one of the fields is empty it will fail instead of only when all of the fields are empty

Luke Bradley
  • 326
  • 5
  • 16
0

By nothing do you mean white space? If so just check so in a if statement. (credit)

if (trim($str) == '')
{
    //string is only whitespace
}
Community
  • 1
  • 1
0

Consider below lines.

php > $a = '';
php > echo isset($a) ? 'a is set' : 'a is not set';
a is set
php > echo empty($a) ? 'a is empty' : 'a is not empty';
a is empty

setting something doesn't mean that it is not empty. so, use isset($foo) && !empty($foo) .

better create a function,

function present($x) {
  return (isset($x) && !empty($x));
}

and use it.

if (present($_POST['foo'])) {
 /* do something */ 
}

or do it for whole $_POST array,

function present($somevar) {
  return (isset($somevar) && !empty($somevar));
}

function all_vars_are_ok($arr) {
  $x = array_map("present", $arr);
  return (array_filter($x) == $x);
}

then,

if(all_vars_are_ok($_POST)) {
  /*
  *  do something 
  */
}
marmeladze
  • 6,468
  • 3
  • 24
  • 45
0

Solutions:

  1. Your can put attribute required in input tag.
  2. You can add JavaScript validation.
  3. In PHP you can check like this: !empty($variable)
Sazzad Hussain
  • 321
  • 1
  • 3
  • 11