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: More POST data checks
Date: 3 years ago
Size: 1,745 bytes


Class file image Download
if($_SERVER['REQUEST_METHOD'] == "POST" && isset($_POST) && isset($_POST['action'])){
$luhn = new Luhn();
$_POST['action'] == "calculate" && isset($_POST['gnumber'])){
$gnumber = $_POST['gnumber'];
$gresult = $luhn->calculate($gnumber);
    } elseif(
$_POST['action'] == "validate" && isset($_POST['vnumber'])){
$vnumber = $_POST['vnumber'];
$luhn->validate(substr($vnumber,0,-1), substr($vnumber,-1,1)) == true){
$vresult = "Number is valid";
        } else{
$vresult = "Number is invalid";
?><!DOCTYPE html>
    <title>Luhn's algorithm</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
        <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" />
        <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" />