Solved

Javascript vs PHP giving differnet answers

Posted on 2013-05-10
4
201 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 83

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 83

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

919 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

17 Experts available now in Live!

Get 1:1 Help Now