Search code examples
phpimei

IMEI check digit on more than one IMEI at a time with PHP


I am trying to make a website that you input a list of non check digit IMEI's on one page (below)

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<link href='http://fonts.googleapis.com/css?family=Reenie+Beanie' rel='stylesheet' type='text/css'>
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
    <title>IMEI check digit</title> 
    <link rel="shortcut icon" href="https://c2.staticflickr.com/8/7203/6890418511_fc92574c8a.jpg"/>
    <link rel="alternate" type="application/rss+xml" href="{RSS}" />
        <style type="text/css">
        /*Main CSS for the body*/
        body {
            background: url("http://static.tumblr.com/225d38dfe37f2d9a7edb3e1fbfcee711/ufgtjry/8J2nks995/tumblr_static_dnfrmr3xbjwckcwckckwocgo0.jpg")
            no-repeat center center fixed; /*CHANGE BACKGROUND IMAGE HERE*/
            -webkit-background-size: cover;
            -moz-background-size: cover;
            -o-background-size: cover;
            background-size: cover;
            font-style:none;
            font-family:calibri;
            color:{color:text};
            font-size:12px;}

        body, a, a:hover {
            cursor: url(http://img69.imageshack.us/img69/7673/cursorw.png), progress;}

        html, body { height: 100%; margin: 0; }

        /*CSS used for box itself*/

        #transbox {
            opacity: 0.7;
            filter: alpha(opacity=60);
            position: relative;
            width: 600px;
            background-color: #fff;
            border: 1px solid #000;
            padding:4px;}

        #transbox a:hover {
            opacity: 1.0;
            filter: alpha(opacity=100);}

        #container {
            height: 100%;
            display: flex;
            align-items: center;
            justify-content: center;}

        /*CSS used for title*/

        #title {
            font-family: calibri;
            font-size:18px;
            position: relative;
            text-transform:uppercase;
            top: 8px;
            left: 8px;
            letter-spacing: 3px;
            color:#000;
            margin-left:-5px;
            z-index:1;
            text-align: center;}

        #title:hover {
            font-style:italic;}

        /*CSS used for nav links*/

        #navigation a {
            font-family:'calibri';
            display:block;
            color:#000;
            font-size:12px;
            margin-top: 22px;
            padding:6px;
            text-align:center;
            letter-spacing:2px;
            text-transform:uppercase;
            text-decoration:underline;
            margin-bottom:5px;
            -webkit-transition: all 0.4s ease-in-out;
            -moz-transition: all 0.4s ease-in-out;
            -o-transition: all 0.4s ease-in-out;
            -ms-transition: all 0.4s ease-in-out;
            transition: all 0.4s ease-in-out;}


        #navigation a:hover {
            background:#999;
            color:#fff;
            opacity: 1.0;
            text-decoration: none;
            filter: alpha(opacity=100);}

        #right{ 
            text-align:right;}

        #center{ 
            text-align:center;}

        p{
            text-align:center;}
        </style>
</head>
<body>
    <div id="container">
    <div id="transbox">
    <div id="title">Check digit generator</div>
    <form action="output.php" method="POST">
        <p><strong>The 14 digit imei</strong></p>
        <textarea required id="IMEI" rows="16" class="font-fixed-width" style="width: 90%;margin : 0 auto; display: block" name="IMEI"></textarea>
        </br>
        <p><div id="center"><input type="submit"></div></p>
    </form>
    </div> 
    </div>
</body>
</html>

and then it submits to this page

<?php
$imei = $_POST['IMEI']; //chk has to be 8

//select numbers from imei and add to array
for ($i = 0; $i <= 14; $i++) {
    $D[]=$imei;
}
$sumeven = 0;
// adding even numbers together
for ($i = 1; $i <= 14; $i=$i+2) {
    $sum = $D[$i]*2;

        //seperate numbers that become double digits
        if ($D[$i] > 4) {
            //convert int to string
            $sumS = (string)$sum;
            //clear array
            unset($splitt);
            //seperate the numbers and add them together
            for ($j = 0; $j <= 1; $j++) {
                $splitt[]=$sumS[$j];
                $sumeven = $sumeven + $splitt[$j];
            }
        //else just add the single number to the last   
        } else {
            $sumeven = $sumeven + $sum;
        }
}
// adding odd numbers together
$sumodd = 0;
$sum = 0;
for ($i = 0; $i <= 13; $i=$i+2) {
    $sum = $D[$i];
    $sumodd = $sumodd + $sum;
}
// add odd and even numbers
$total = $sumodd + $sumeven;
//convert int to string
$totalS = (string)$total;
//Split total into two variables
for ($i = 0; $i <= 1; $i++) {
    $split[]=$totalS[$i];
}
if ($split[1] == 0) {
    $zero = 0;
    $IMEIZERO = $imei.$zero;
    //print $IMEIZERO;
} else {
    $split[0] = $split[0] + 1;
    $totalNew = $split[0]."0";
    $chk = $totalNew - $total;

    $IMEICHK = $imei.$chk;
    //print $IMEICHK;
} 


?>
<html>
<head>
</head>
<body>
<textarea id="txtList" rows="16" class="font-fixed-width" style="width: 100%;" name="txtList">
<?php print $IMEICHK OR $IMEIZERO?>
</textarea>
</body>
</html>

However I am having a hard time thinking about how I can make it print or echo more than once and print/echo unique values into the textarea for example if I wanted to get the check digit of the following 14 digit IMEI's 15832859381748 18247395837243 52835621351219 12583582923852

I would like to put them into the first page, press submit and have them on the second page with the check digits on the end. So like this

158328593817484 182473958372434 528356213512196 125835829238525

Any ideas?

Thank you very much


Solution

  • Have the user enter one number per line, then when you receive it, use PHP's explode() function. You'll end up with an array of all the IMEIs.

    I couldn't resist the urge to fix the checksum calculation, there are comments inline to explain what some of the functions do.

    $imeis = explode("\n", $_POST['IMEI']);
    $output = "";
    foreach ($imeis as $imei) {
        $sum = 0;
        //str_split converts strings to arrays
        //array_reverse reverses arrays; that is how the checksum should be done
        $imeiRev = array_reverse(str_split(trim($imei)));
        //loop through each digit in the now-reversed IMEI
        foreach ($imeiRev as $i=>$digit) {
            if ($i % 2 === 0 && $digit > 4) {
                //it's even and greater than 4
                $digit = array_sum(str_split($digit * 2));
            } elseif ($i % 2 === 0) {
                //it's even
                $digit = $digit * 2;
            }
            //add to our running total
            $sum += $digit;
        }
        $sum = str_split($sum * 9);
        //array_pop just "pops" the last value off
        $checkdigit = array_pop($sum);
        //add it to the IMEI
        $output .= "$imei$checkdigit\n";
    }
    
    ?>
    <html>
        <head>
            <title>Checksum Results</title>
        </head>
        <body>
            <textarea id="txtList" rows="16" class="font-fixed-width" style="width: 100%;" name="txtList">
            <?php echo $output?>
            </textarea>
        </body>
    </html>