Help with Javascript to PHP conversion [Updated]
Posted: Tue Jul 12, 2005 10:47 am
I'm trying to convert a Javascript function to PHP, so I can work with it better. (Javascript is pretty incomprehensible to me at this point, grr.
) It's a sort of validation function that converts a login/password (s, t) to a text string that it matches with a pre-generated HTML document containing that user's data.
Here's tha Javascript, followed by what I've worked out so far in PHP:
Here's my PHP translation so far (I'm using preset values for $s and $t, so I can watch what happens):
Mine outputs this, right now:
(1) Are n and p supposed to be figured concurrently, or consecutively? (Is the Javascript doing all of p first, and then n, or p and n at the same time?)
(2) Is it running 'code =' every time it goes through the loop? Or afterwords?
(3) How in the world can I translate
into PHP? (I don't even really understand what the formula is doing...
I assume at the end there, it's taking the result of 'code' and adding a corresponding value from 'alphanum' to build 'address' one character at a time...
If anyone feels like helping me out on this, I'd be most appreciative. I can say that using the values 'test' and 'password' for $s and $t, the function should return 'royrlt2ug'.
Thanks!
Jeff.
Here's tha Javascript, followed by what I've worked out so far in PHP:
Code: Select all
function get_name( form )
{
var alphanum="e;0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"e;
var s = form.textfield.value
var t = form.textfield2.value
var address = "e;r"e;
var len,i,n,p,code
if ( s.length && t.length )
{
s = s.toLowerCase()
s = stripSpaces( s )
t = t.toLowerCase()
t = stripSpaces( t )
len = ( s.length>t.length ) ? s.length : t.length
if ( len>24 ) len=24
for ( i=0;i<len;i++ )
{
n=p=0
if ( i<s.length ) n = alphanum.indexOf(s.substring( i,i+1 ))
if ( i<t.length ) p = alphanum.indexOf(t.substring( i,i+1 ))
if ( n<0 ) n=0
if ( p<0 ) p=0
code = ((2*n+p)^13) % (alphanum.length-1)
if ( code>0 ) address += alphanum.substring( code,code+1 )
}
}
return ( address )
}Here's my PHP translation so far (I'm using preset values for $s and $t, so I can watch what happens):
Code: Select all
$alphanum = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
$s = 'test';
$t = 'password';
$address = 'r';
$s_len = strlen($s);
$t_len = strlen($t);
if($s_len && $t_len) {
$s = strtolower($s);
$s = trim($s);
$t = strtolower($t);
$t = trim($t);
if($s_len > $t_len) {
$len = $s_len;
} else {
$len = $t_len;
}
if($len > 24) {
$len = 24;
}
for($count = 0; $count < $len; $count++) {
if($count < $s_len) {
$char = substr($s, $count,1);
echo $char.": ";
$n = strpos($alphanum, $char);
echo $n."\n";
}
if($count < $t_len) {
$tchar = substr($t, $count,1);
echo $tchar."--";
$p = strpos($alphanum, $tchar);
echo $p."\n";
}
}
} //End main if() statementThings I don't quite understand yet:t: 55
p: 51
e: 40
a: 36
s: 54
s: 54
t: 55
s: 54
w: 58
o: 50
r: 53
d: 39
(1) Are n and p supposed to be figured concurrently, or consecutively? (Is the Javascript doing all of p first, and then n, or p and n at the same time?)
(2) Is it running 'code =' every time it goes through the loop? Or afterwords?
(3) How in the world can I translate
Code: Select all
code = ((2*n+p)^13) % (alphanum.length-1)
if ( code>0 ) address += alphanum.substring( code,code+1 )If anyone feels like helping me out on this, I'd be most appreciative. I can say that using the values 'test' and 'password' for $s and $t, the function should return 'royrlt2ug'.
Thanks!
Jeff.