?
Solved

Javascript vs PHP giving differnet answers

Posted on 2013-05-10
4
Medium Priority
?
212 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 84

Accepted Solution

by:
Dave Baldwin earned 2000 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 84

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

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
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)
The viewer will learn the basics of jQuery including how to code hide show and toggles. 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…

589 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