Dienstag, 11 November 2025

Top 5 diese Woche

Ähnliche Tutorials

Kreditrechner Online

Unser Script ist selbstinstallierend, mit dem Besucher ganz einfach Kreditraten, Gesamtkosten und Rückzahlungssummen für Ratenkredite berechnen können. Beim ersten Start erstellt es automatisch alle benötigten Dateien, Module und das Design, komplett ohne zusätzliche Installation oder Datenbank.

Der Kreditrechner zeigt sofort eine Vorschau der monatlichen Rate, Gesamtzinsen und Rückzahlung an. Standardwerte für Kreditsumme, Zinssatz und Laufzeit lassen sich frei anpassen. Ideal für Finanzseiten, Vergleichsportale oder als praktisches Tool auf der eigenen Website. 😉

<?php
session_start();
date_default_timezone_set('Europe/Berlin');
$SITE_TITLE = "Kreditrechner";
$LINK_DREAMCODES = "http://www.dreamcodes.net";
$INSTALL_MARKER = __DIR__ . '/.dreamcodes_loan_installed';
$BASE = __DIR__;
$DIRS = [
    'assets',
    'assets/css',
    'assets/js',
    'modules',
    'templates',
];
function ensure_dirs($dirs) {
    foreach($dirs as $d) {
        if(!is_dir($d)) mkdir($d, 0755, true);
    }
}
function safe_float($v) {
    $v = str_replace(',', '.', trim($v));
    return is_numeric($v) ? (float)$v : 0.0;
}
function send_flash($msg, $type='ok') {
    $_SESSION['flash'] = ['msg'=>$msg,'type'=>$type];
}
function get_and_clear_flash() {
    if(isset($_SESSION['flash'])) {
        $f = $_SESSION['flash'];
        unset($_SESSION['flash']);
        return $f;
    }
    return null;
}
if(!file_exists($INSTALL_MARKER)) {
    ensure_dirs($DIRS);
    file_put_contents('assets/css/style.css', <<<CSS
:root{--bg:#f5f7fb;--card:#fff;--accent:#0a74da;--muted:#6b7280}
*{box-sizing:border-box}body{font-family:Inter,Segoe UI,Helvetica,Arial,sans-serif;background:var(--bg);margin:0;color:#111}
header{background:linear-gradient(90deg,var(--accent),#005bb5);color:white;padding:18px 28px}
.container{max-width:1000px;margin:28px auto;padding:0 18px}
.card{background:var(--card);padding:18px;border-radius:12px;box-shadow:0 6px 20px rgba(2,6,23,0.06);margin-bottom:18px}
.grid{display:grid;grid-template-columns:1fr 320px;gap:20px}
.field{margin-bottom:12px}
label.small{display:block;font-size:13px;color:var(--muted);margin-bottom:6px}
input[type=text],input[type=number],select,textarea{width:100%;padding:10px;border:1px solid #e6e9ef;border-radius:8px}
button{background:var(--accent);color:white;padding:10px 14px;border-radius:8px;border:0;cursor:pointer}
.small{font-size:13px;color:var(--muted)}
.result {padding:12px;border-radius:8px;background:#f3f9ff;border:1px solid #e6f0ff}
.footer{padding:18px;text-align:center;color:#6b7280;font-size:13px}
.flex{display:flex;gap:8px;align-items:center}
.grid-two{display:grid;grid-template-columns:1fr 1fr;gap:8px}
CSS
    );

    // JS
    file_put_contents('assets/js/app.js', <<<JS
function formatNumber(num) {
    return (Math.round((num + Number.EPSILON) * 100) / 100).toLocaleString();
}
function calculatePreview() {
    var principal = parseFloat(document.getElementById('principal').value.replace(',', '.')) || 0;
    var rate = parseFloat(document.getElementById('rate').value.replace(',', '.')) || 0;
    var years = parseFloat(document.getElementById('years').value.replace(',', '.')) || 0;
    var n = Math.max(1, Math.round(years * 12));
    var r = rate / 100 / 12;
    var monthly = 0;
    if(r === 0) monthly = principal / n;
    else monthly = principal * r / (1 - Math.pow(1 + r, -n));
    var total = monthly * n;
    var interest = total - principal;
    document.getElementById('preview_monthly').innerText = formatNumber(monthly) + ' €';
    document.getElementById('preview_total').innerText = formatNumber(total) + ' €';
    document.getElementById('preview_interest').innerText = formatNumber(interest) + ' €';
}
document.addEventListener('DOMContentLoaded', function(){
    var elems = ['principal','rate','years'];
    elems.forEach(function(id){
        var el = document.getElementById(id);
        if(el) el.addEventListener('input', calculatePreview);
    });
    calculatePreview();
});
JS
    );
    file_put_contents('modules/calculator.php', <<<'PHP'
<?php
if(!defined('DREAMCODES_LOAN_MASTER')) die('no direct');
$defaults = [
  'principal' => 10000,
  'rate' => 3.5,      // yearly percent
  'years' => 5
];
// can be extended to read from config files
?>
<div class="card">
  <h2>Kreditrechner</h2>
  <p class="small">Gib die Kreditsumme, den Jahreszins und die Laufzeit ein. Standardwerte sind vorgefüllt und anpassbar.</p>

  <form method="post" class="card" style="padding:12px 18px;margin-top:12px">
    <div class="field">
      <label class="small">Kreditsumme (€)</label>
      <input type="text" id="principal" name="principal" value="<?php echo htmlspecialchars($_POST['principal'] ?? $defaults['principal']); ?>" required>
    </div>
    <div class="grid-two">
      <div class="field">
        <label class="small">Jahreszins (%)</label>
        <input type="text" id="rate" name="rate" value="<?php echo htmlspecialchars($_POST['rate'] ?? $defaults['rate']); ?>" required>
      </div>
      <div class="field">
        <label class="small">Laufzeit (Jahre)</label>
        <input type="number" id="years" name="years" min="1" step="1" value="<?php echo htmlspecialchars($_POST['years'] ?? $defaults['years']); ?>" required>
      </div>
    </div>

    <div style="display:flex;gap:8px;margin-top:8px">
      <button type="submit" name="calc">Berechnen</button>
      <button type="button" onclick="document.getElementById('calc-form').reset()">Zurücksetzen</button>
    </div>
  </form>

  <div style="margin-top:12px">
    <h3>Vorschau (sofort)</h3>
    <div class="result">
      <div class="flex"><div class="small" style="width:160px">Monatliche Rate:</div><div id="preview_monthly">—</div></div>
      <div class="flex"><div class="small" style="width:160px">Gesamtrückzahlung:</div><div id="preview_total">—</div></div>
      <div class="flex"><div class="small" style="width:160px">Gesamtzinsen:</div><div id="preview_interest">—</div></div>
    </div>
  </div>
</div>
PHP
    );
    file_put_contents('templates/info.html', <<<HTML
<div class="card">
  <h3>Info</h3>
  <p class="small">Dieses Modul berechnet die monatliche Rate (Annuität), die Gesamtrückzahlung und die gesamten Zinsen für Ratenkredite. Standardeinstellungen können im Formular angepasst werden.</p>
</div>
HTML
    );
    file_put_contents($INSTALL_MARKER, "installed: " . date('c') . PHP_EOL);
}

/* ---------------- Boot / Include ---------------- */
define('DREAMCODES_LOAN_MASTER', true);
ensure_dirs($DIRS);

/* ---------------- Handle calculation (server-side) ---------------- */
$flash = null;
$calc_result = null;
if(isset($_POST['calc'])) {
    $principal = safe_float($_POST['principal'] ?? 0);
    $rate = safe_float($_POST['rate'] ?? 0);       // yearly %
    $years = (int)max(1, (float)($_POST['years'] ?? 1));
    $months = $years * 12;
    $r = $rate / 100 / 12; // monthly rate (decimal)

    if($months <= 0) {
        send_flash('Ungültige Laufzeit.', 'err');
        header("Location: " . $_SERVER['PHP_SELF']); exit;
    }

    if($r == 0.0) {
        $monthly = ($months > 0) ? ($principal / $months) : 0;
    } else {
        $monthly = $principal * $r / (1 - pow(1 + $r, -$months));
    }
    $total = $monthly * $months;
    $interest = $total - $principal;

    $calc_result = [
        'principal' => $principal,
        'rate' => $rate,
        'years' => $years,
        'months' => $months,
        'monthly' => $monthly,
        'total' => $total,
        'interest' => $interest,
    ];
    // flash a message
    send_flash('Berechnung durchgeführt.', 'ok');
    // no redirect: show on same page
}
function render_flash() {
    $f = get_and_clear_flash();
    if(!$f) return '';
    $cls = $f['type'] === 'err' ? 'error' : 'message';
    return "<div class=\"$cls\">".htmlspecialchars($f['msg'])."</div>";
}
?><!doctype html>
<html lang="de">
<head>
  <meta charset="utf-8">
  <title>Dreamcodes - <?php echo htmlspecialchars($SITE_TITLE); ?></title>
  <meta name="viewport" content="width=device-width,initial-scale=1">
  <link rel="stylesheet" href="assets/css/style.css?ver=1">
  <script src="assets/js/app.js?ver=1" defer></script>
  <style>
    .result-card {padding:12px;margin-top:12px}
    .note {font-size:13px;color:#6b7280}
  </style>
</head>
<body>
<header>
  <div style="max-width:1000px;margin:0 auto;display:flex;align-items:center;justify-content:space-between">
    <div><strong style="font-size:18px">Dreamcodes</strong> <span class="small">Kreditrechner</span></div>
    <div class="small"><?php echo htmlspecialchars(date('d.m.Y H:i')); ?></div>
  </div>
</header>

<div class="container">

  <?php echo render_flash(); ?>

  <div class="grid">
    <main>
      <?php
        if(file_exists('modules/calculator.php')) include 'modules/calculator.php';
        else echo '<div class="card">Modul fehlt.</div>';
      ?>

      <?php if($calc_result): ?>
        <div class="card result-card">
          <h3>Ergebnis</h3>
          <div class="flex"><div class="small" style="width:180px">Kreditsumme:</div><div><?php echo number_format($calc_result['principal'],2,',','.'); ?> €</div></div>
          <div class="flex"><div class="small" style="width:180px">Jahreszins:</div><div><?php echo htmlspecialchars($calc_result['rate']); ?> %</div></div>
          <div class="flex"><div class="small" style="width:180px">Laufzeit:</div><div><?php echo (int)$calc_result['years']; ?> Jahre (<?php echo (int)$calc_result['months']; ?> Monate)</div></div>
          <hr>
          <div class="flex"><div class="small" style="width:180px">Monatliche Rate:</div><div><strong><?php echo number_format($calc_result['monthly'],2,',','.'); ?> €</strong></div></div>
          <div class="flex"><div class="small" style="width:180px">Gesamtrückzahlung:</div><div><?php echo number_format($calc_result['total'],2,',','.'); ?> €</div></div>
          <div class="flex"><div class="small" style="width:180px">Gesamtzinsen:</div><div><?php echo number_format($calc_result['interest'],2,',','.'); ?> €</div></div>
        </div>
      <?php else: ?>
        <div class="card">
          <h3>Was berechnet wird</h3>
          <p class="small">Der Rechner ermittelt die monatliche Rate (Annuität), die Gesamtrückzahlung sowie die gesamten Zinskosten für einen Ratenkredit. Du kannst die voreingestellten Werte im Formular anpassen.</p>
        </div>
      <?php endif; ?>

    </main>

    <aside style="width:320px">
      <div class="card">
        <h3>Kurzanleitung</h3>
        <ol class="small">
          <li>Kreditsumme eingeben (zB. 10.000).</li>
          <li>Jahreszins in Prozent (zB. 3.5).</li>
          <li>Laufzeit in Jahren auswählen.</li>
          <li>Auf "Berechnen" klicken — die Ergebnisse erscheinen rechts oben.</li>
        </ol>
        <div style="margin-top:10px" class="note">Die Vorschau aktualisiert sich sofort beim Eingeben von Werten (clientseitig).</div>
      </div>
      <div class="card">
        <h3>Vorbelegte Werte</h3>
        <div class="small">Standard-Kreditsumme, Zinssatz und Laufzeit sind im Formular voreingestellt und können beliebig verändert werden.</div>
      </div>
      <div class="card">
        <h3>Weitere Optionen</h3>
        <div class="small">Das Modul ist modular erweiterbar — zB. Tilgungspläne, Gebühren oder Sondertilgungen können ergänzt werden.</div>
      </div>
      <div class="card">
        <h3>Links</h3>
        <div style="display:flex;flex-direction:column;gap:8px">
          <a href="<?php echo htmlspecialchars($LINK_DREAMCODES); ?>" target="_blank"><button>Mehr von Dreamcodes</button></a>
        </div>
      </div>
    </aside>
  </div>
  <div class="footer">
    &copy; <?php echo date('Y'); ?> | <a href="<?php echo htmlspecialchars($LINK_DREAMCODES); ?>" target="_blank">Powered by Dreamcodes</a>
  </div>
</div>
</body>
</html>
Vorheriges Tutorial

Lesetipps für dich