PHP Classes

File: example.php

Recommend this page to a friend!
  Classes of Rolands Kusins   PHP Luhn Algorithm   example.php   Download  
File: example.php
Role: Example script
Content type: text/plain
Description: Example
Class: PHP Luhn Algorithm
Calculate and validate a Luhn check digit
Author: By
Last change: Update of example.php
Date: 11 months ago
Size: 1,761 bytes
 

Contents

Class file image Download
<?php
if ($_SERVER['REQUEST_METHOD'] == "POST" && isset($_POST) && isset($_POST['action'])) {
    include_once
'Luhn.php';
   
$luhn = new Luhn();
    if (
$_POST['action'] == "calculate" && isset($_POST['gnumber'])) {
       
$gnumber = $_POST['gnumber'];
       
$gresult = $luhn->calculate($gnumber);
    } elseif(
$_POST['action'] == "validate" && isset($_POST['vnumber'])) {
       
$vnumber = $_POST['vnumber'];
        if (
$luhn->validate(substr($vnumber, 0, -1), substr($vnumber, -1, 1)) == true) {
           
$vresult = "Number is valid";
        } else {
           
$vresult = "Number is invalid";
        }
    }
}
?><!DOCTYPE html>
<html>
<head>
    <title>Luhn's algorithm</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
    <div>
        <h1>Check digit calculation</h1>
        <form method="post">
            <input type="hidden" name="action" value="calculate" />
            <label for="gnumber">Number </label><input type="text" style="width: 300px;" id="gnumber" name="gnumber" value="<?php if (isset($gnumber)) echo $gnumber; ?>" /><br />
            Result <input type="text" style="width: 300px;" disabled="disabled" value="<?php if (isset($gresult)) echo $gresult; ?>" /><br />
            <input type="submit" value="Calculate" />
        </form>
        <h1>Number validation</h1>
        <form method="post">
            <p>Number must include check digit - last digit of number is check digit</p>
            <input type="hidden" name="action" value="validate" />
            <label for="vnumber">Number </label><input type="text" style="width: 300px;" id="vnumber" name="vnumber" value="<?php if (isset($vnumber)) echo $vnumber; ?>" /><br />
            Result <input type="text" style="width: 300px;" disabled="disabled" value="<?php if (isset($vresult)) echo $vresult; ?>" /><br />
            <input type="submit" value="Calculate" />
        </form>
    </div>
</body>
</html>