Help with Javascript to PHP conversion [Updated]

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
is_blank
Forum Commoner
Posts: 36
Joined: Sat Jun 25, 2005 6:05 pm
Location: Tennessee, USA

Help with Javascript to PHP conversion [Updated]

Post by is_blank »

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:

Code: Select all

function get_name( form ) 
{
  var alphanum=&quote;0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz&quote;
  var s = form.textfield.value
  var t = form.textfield2.value
  var address = &quote;r&quote;
  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() statement
Mine outputs this, right now:
t: 55
p: 51
e: 40
a: 36
s: 54
s: 54
t: 55
s: 54
w: 58
o: 50
r: 53
d: 39
Things I don't quite understand yet:
(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&gt;0 ) address += alphanum.substring( code,code+1 )
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.
Last edited by is_blank on Tue Jul 12, 2005 4:58 pm, edited 1 time in total.
User avatar
is_blank
Forum Commoner
Posts: 36
Joined: Sat Jun 25, 2005 6:05 pm
Location: Tennessee, USA

Javascript to PHP [Updated]

Post by is_blank »

Ok, I think I've mostly figured out the conversion, except I've got an error somewhere. In a test case with login ($s) 'Student' and password ($t) 'password', the page my program exports is called rObnFDVqg.html. (This is what I need my code to match.) The string my converted PHP function derives is rObnFDVqU. Can anyone spot where I've made a mistake? Here's the Javascript, followed by my PHP translation:

Code: Select all

function get_name( form ) {
  var alphanum=&quote;0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz&quote;
  var s = form.textfield.value
  var t = form.textfield2.value
  var address = &quote;r&quote;
  var len,i,n,p,code

  if ( s.length &amp;&amp; t.length ) {
    len = ( s.length &gt; t.length ) ? s.length : t.length
    if ( len &gt; 24 ) len=24

    s = stripSpaces( s )
    t = t.toLowerCase()
    t = stripSpaces( t )

    for ( i = 0; i &lt; len; i++ ){
      n = p = 0
      if ( i &lt; s.length ) n = alphanum.indexOf(s.substring( i,i + 1 ))
      if ( i &lt; t.length ) p = alphanum.indexOf(t.substring( i,i + 1 ))

      if ( n &lt; 0 ) n = 0
      if ( p &lt; 0 ) p = 0

      code = ((2*n+p)^13) % (alphanum.length-1)
      if ( code&gt;0 ) address += alphanum.substring( code,code+1 )
    } 
  }
return ( address )
}
And the PHP:

Code: Select all

$alphanum = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
$s = 'Student';
$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."--"; //Debug
			$n = strpos($alphanum, $char);
			echo $n."\n"; //Debug
		}
		
		if($count < $t_len) {
			$tchar = substr($t, $count,1);
			echo $tchar."--"; //Debug
			$p = strpos($alphanum, $tchar);
			echo $p."\n"; //Debug
		}
		
		$code = ((2*$n+$p)^13)%(strlen($alphanum)-1);
		echo $code."\n"; //Debug
		
		if($code > 0){
		$code_char = substr($alphanum, $code, 1);
		$address .= $code_char;
		} 
	} //End main for() loop

echo $address."\n";

} //End main if() statement
Thanks!
Jeff.
Post Reply