You are here: Re: Integer to code and back « PHP Programming Language « IT news, forums, messages
Re: Integer to code and back

Posted by Toby Inkster on 01/04/07 10:17

Tom wrote:

> 10 => DFG56JKG4 => 10
>
> How do I accomplish that?

How about this...

define('MYSECRET','Some unguessable string');

function int2code ($i)
{
$md5 = strtoupper(md5(MYSECRET.$i));
$mdStart = substr($md5, 0, 4);
$mdEnd = substr($md5, 28, 4);
return $mdStart.$i.$mdEnd;
}

function code2int ($code)
{
preg_match('/^([A-F0-9]{4})([0-9]+)([A-F0-9]{4})$/', $code, $matches);
$i = $matches[2];

$md5 = strtoupper(md5(MYSECRET.$i));
$mdStart = substr($md5, 0, 4);
$mdEnd = substr($md5, 28, 4);
if ($mdStart.$i.$mdEnd==$code)
return $i;

return FALSE; // Invalid code!
}

--
Toby A Inkster BSc (Hons) ARCS
Contact Me ~ http://tobyinkster.co.uk/contact

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация