Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Javascript vs PHP giving differnet answers

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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

This article discusses how to create an extensible mechanism for linked drop downs.
A while back, I ran into a situation where I was trying to use the calculated columns feature in SharePoint 2013 to do some simple math using values in two lists. Between certain data types not being accessible, and also with trying to make a one to…
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)
Suggested Courses

926 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