Browse Source

better check input forms

improve-form-code
Michael Boeyens 3 years ago
parent
commit
c6860c92e1
6 changed files with 48 additions and 34 deletions
  1. BIN
      db.sqlite
  2. 21
    22
      gamefunction.inc
  3. 4
    5
      index.php
  4. 16
    5
      login.inc
  5. 2
    2
      register.php
  6. 5
    0
      style.css

BIN
db.sqlite View File


+ 21
- 22
gamefunction.inc View File

@@ -1,5 +1,6 @@
<?php
require_once "dbase.inc";
require_once "login.inc";

class game
{
@@ -61,30 +62,27 @@ class game
}

//after shot, check if ship was hit
public function hitCheck()
public function hitCheck($shot)
{
if (isset($_POST["shot"])) {

$this->shotCoor[] = $_POST["shot"];

$this->ammo--;
for ($i = 0; $i < count($this->ships); $i++) {
$result = $this->ships[$i]->compareShot($_POST["shot"]);
$this->hitCount += $result;
if ($result == 1) {
array_pop($this->shotCoor);
$this->hitCoor[] = $_POST["shot"];
}
$this->shotCoor[] = $shot;

$this->ammo--;
for ($i = 0; $i < count($this->ships); $i++) {
$result = $this->ships[$i]->compareShot($shot);
$this->hitCount += $result;
if ($result == 1) {
array_pop($this->shotCoor);
$this->hitCoor[] = $shot;
}
}

if ($this->hitCount == $this->ships[0]->printLengte() + $this->ships[1]->printLengte()) {
if ($this->hitCount == $this->ships[0]->printLengte() + $this->ships[1]->printLengte()) {

$this->gameResult = 1;
$this->saveResult();
} elseif ($this->ammo == 0) {
$this->gameResult = 0;
$this->saveResult();
}
$this->gameResult = 1;
$this->saveResult();
} elseif ($this->ammo == 0) {
$this->gameResult = 0;
$this->saveResult();
}
}

@@ -117,7 +115,8 @@ function runGame()
if (isset($_POST["difficulty"])) {
gameStartup();
} elseif (isset($_POST["shot"])) {
$_SESSION["game"]->hitCheck();
$shot = test_input($_POST["shot"]);
$_SESSION["game"]->hitCheck($shot);
} elseif (isset($_SESSION["game"]) && $_SESSION["game"]->gameResult !== null) {
unset($_SESSION["game"]);
}
@@ -127,7 +126,7 @@ function runGame()
function validateInput()
{
if ((isset($_SESSION["game"]) && $_SESSION['game']->gameResult === null) && ((int)$_POST["shot"] < 0 || (int)$_POST["shot"] > $_SESSION["game"]->ships[0]->printDokLengte() - 1)) {
return "Verkeerde input. Cijfer moet tussen 0 en " . (string)($_SESSION["game"]->ships[0]->printDokLengte() - 1) . " zijn.";
return "<span class=\"errormessage\">Verkeerde input. Cijfer moet tussen 0 en " . (string)($_SESSION["game"]->ships[0]->printDokLengte() - 1) . " zijn</span>";
} else {
runGame();
}

+ 4
- 5
index.php View File

@@ -5,7 +5,7 @@ session_start();
unset($_SESSION['game']);

require_once "dbase.inc";
include "login.inc";
require_once "login.inc";

$loginInfo = loginUser();
?>
@@ -18,15 +18,14 @@ $loginInfo = loginUser();
<main class="loginmain">
<h1>Play battleship</h1>
<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="POST" class="loginform__input">
<label for="login" class="loginform__input__items">Login: </label><br>
<label for="login" class="loginform__input__items">Login: </label>
<input type="text" id="login" name="login" class="loginform__input__items loginform__input__items--height" required>
<br>
<label for="password" class="loginform__input__items">Password: </label><br>
<label for="password" class="loginform__input__items">Password: </label>
<input type="password" id="password" name="password" class="loginform__input__items loginform__input__items--height" required>
<input type="submit" class="loginform__input__items loginform__input__items--height " value="submit">
<?= $loginInfo ?>
</form>
<a href="register.php">Nog geen account? Registreer je hier!</a>
<?= $loginInfo ?>
</main>
</body>


+ 16
- 5
login.inc View File

@@ -21,6 +21,10 @@ function loginUser()
$login = test_input($_POST["login"]);
$password = test_input($_POST["password"]);

if (!preg_match("/^[a-zA-Z0-9]+$/", $login)) {
return "<span class=\"loginform__input__items errormessage\">ongeldige loginnaam, enkel letters en cijfers toegelaten</span>";
}

$loginQuery = $dbase->loginQuery($login, $password);

// apart resultaat als spelerId niet in db
@@ -30,7 +34,7 @@ function loginUser()
header('Location: game.php');
exit();
} else {
return "<p class=\"loginform__input__items\">foutief paswoord</p>";
return "<span class=\"loginform__input__items errormessage\">Foutieve login of paswoord</span>";
}
}
}
@@ -38,15 +42,22 @@ function loginUser()
//send register info to db
function registerUser()
{
if (isset($_POST['login']) && isset($_POST['password'])) {
if ($_SERVER["REQUEST_METHOD"] == "POST") {
global $dbase;

$queryResult = $dbase->registerQuery($_POST['login']);
$login = test_input($_POST["login"]);
$password = test_input($_POST["password"]);

if (!preg_match("/^[a-zA-Z0-9]+$/", $login)) {
return "<span class=\"loginform__input__items errormessage\">ongeldige loginnaam, enkel letters en cijfers toegelaten</span>";
}

$queryResult = $dbase->registerQuery($login);

if ($queryResult) {
return "<p class=\"loginform__input__items\">loginnaam al in gebruik</p>";
return "<span class=\"loginform__input__items errormessage\">loginnaam al in gebruik</span>";
} else {
$dbase->registerInsert($_POST['login'], $_POST['password']);
$dbase->registerInsert($login, $password);
header('Location: index.php');
exit();
}

+ 2
- 2
register.php View File

@@ -1,7 +1,7 @@
<html>
<?php
require_once "dbase.inc";
include "login.inc";
require_once "login.inc";

$registerResult = registerUser();

@@ -21,8 +21,8 @@ $registerResult = registerUser();
<label for="password" class="loginform__input__items">Password: </label><br>
<input type="password" id="password" name="password" class="loginform__input__items loginform__input__items--height" required>
<input type="submit" class="loginform__input__items loginform__input__items--height" value="submit">
<?= $registerResult; ?>
</form>
<?= $registerResult; ?>
<a href="index.php">Keer terug naar loginpagina</a>
</main>
</body>

+ 5
- 0
style.css View File

@@ -56,6 +56,11 @@
padding: 5px;
}

.errormessage {
color: red;
font-weight: 600;
}

main a {
font-size: 1.2em;
}

Loading…
Cancel
Save