Solved

Javascript vs PHP giving differnet answers

Posted on 2013-05-10
4
200 Views
Last Modified: 2013-05-30
I'm trying to get a validation working between java script and PHP, however both calculations are pulling different answers

PHP:-
Input = $_GET['txtChallenge'] = "54321"
Output = 45635
    var work1;
    work1 = String($('#txtChallenge').val().substring(4, 5)) + String($('#txtChallenge').val().substring(1, 2)) + String($('#txtChallenge').val().substring(0, 1)) + String($('#txtChallenge').val().substring(2, 3)) + String($('#txtChallenge').val().substring(3, 4));
    work1 = work1 + 96321 * 42677 * 32332 / 22324;
    work1 = Math.round(work1);
    work1 = String(work1).substring(String(work1).length - 5, String(work1).length);
    work1 = String(work1.substring(4, 5)) + String(work1.substring(2, 3)) + String(work1.substring(1, 2)) + String(work1.substring(3, 4)) + String(work1.substring(0, 1));

    $('#txtKey').val(work1);

Open in new window


JavaScript:-
Input = #txtChallenge = "54321"
Output = 20204
    $work1 = substr($_GET['txtChallenge'],4,1) . substr($_GET['txtChallenge'],1,1) . substr($_GET['txtChallenge'],0,1) . substr($_GET['txtChallenge'],2,1) . substr($_GET['txtChallenge'],3,1);
    $work1 = $work1 + 96321 * 42677 * 32332 / 22324;
    $work1 = round($work1);
    $work1 = substr($work1,strlen($work1)-5);
    $work1 = substr($work1,4,1) . substr($work1,2, 1) . substr($work1,1, 1) . substr($work1,3, 1) . substr($work1,0, 1);

Open in new window


The part the calculations seem to go adrift is at:-
work1 + 96321 * 42677 * 32332 / 22324;

Open in new window


Im a little confused why they are going adrift, can anyone see where it is going wrong????
0
Comment
Question by:tonelm54
  • 2
4 Comments
 
LVL 82

Accepted Solution

by:
Dave Baldwin earned 500 total points
ID: 39157105
It appears that javascript did not like the grouping or maybe it needed to see that 'work1' was a number.  This comes up with the same answer for JS and PHP although JS shows more decimal places in the intermediate number.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
 "http://www.w3.org/TR/html4/loose.dtd">

<html>
<head>
<title>JS-PHP-comparison</title>
</head>
<body>
<script type="text/javascript">
<!--
    var work1;
    // work1 = String($('#txtChallenge').val().substring(4, 5)) + String($('#txtChallenge').val().substring(1, 2)) + String($('#txtChallenge').val().substring(0, 1)) + String($('#txtChallenge').val().substring(2, 3)) + String($('#txtChallenge').val().substring(3, 4));
	work1 = "54321";
    work1 = (work1*1) + ((96321 * 42677 * 32332) / 22324);
		document.write(work1 + "<br>");
    work1 = Math.round(work1);
    work1 = String(work1).substring(String(work1).length - 5, String(work1).length);
    work1 = String(work1.substring(4, 5)) + String(work1.substring(2, 3)) + String(work1.substring(1, 2)) + String(work1.substring(3, 4)) + String(work1.substring(0, 1));
document.write(work1 + "<br>");
    //$('#txtKey').val(work1);

// -->
</script>
<br>
<?php 
//$work1 = substr($_GET['txtChallenge'],4,1) . substr($_GET['txtChallenge'],1,1) . substr($_GET['txtChallenge'],0,1) . substr($_GET['txtChallenge'],2,1) . substr($_GET['txtChallenge'],3,1);
	$work1 = "54321";	
	$work1 = $work1 + ((96321 * 42677 * 32332) / 22324);
	echo ($work1*1)."<br>";
	$work1 = round($work1);
	$work1 = substr($work1,strlen($work1)-5);
	$work1 = substr($work1,4,1) . substr($work1,2, 1) . substr($work1,1, 1) . substr($work1,3, 1) . substr($work1,0, 1);
	echo $work1."<br>";
?>                                  
</body>
</html>

Open in new window

0
 
LVL 4

Expert Comment

by:ramyajanarthanan
ID: 39160971
But which is the correct output that you require
0
 

Author Comment

by:tonelm54
ID: 39162952
Your code worked fine, I don't know if the answer is right, but both PHP and JavaScript gives the same answer which is all I need.

Im trying to build a authenticator login service, so the JavaScript side works on a phone standalone, and the PHP side works on the web server.

The PHP script generates a random 5 digit number which is typed into the standalone webpage (currently installed on phone) and then the resulting number is typed into the webpage and authenticated. Finally I want the actual sum to be different for each user, so a separate install needs for each phone.

The old site (which I made previously) authenticated via an email sent via the web server to the users email account with a second part of a key generated by a random number, the issue being it took too long for the emails to be delivered.

I had looked at using SMS, but I'm a cheap skate and don't want to pay anything, plus security isn't too much of a concern, just a nice addin.
0
 
LVL 82

Expert Comment

by:Dave Baldwin
ID: 39162984
Basically you are generating a simple 'captcha' but you're going thru a lot of work to do it.
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now