Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Javascript vs PHP giving differnet answers

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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.
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
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)

809 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