|
The substr function is just a method of taking each digit of the score in turn. It is an easy way to create a checksum that is hard enough to not be guessed.
You don't actually need to know how the routine works in order to use it, but here is an explanation anyway.
The for() loop takes each digit of the score in turn, and multiplies it by the digit's position (shown in bold).
Say score contains 25700.
2 x 0 = 0
5 x 1 = 5
7 x 2 = 14
0 x 3 = 0
0 x 4 = 0
It then adds up the results, yielding 19.
Your Flash program then sends off the score, 25700, and the checksum, 19, to the PHP script.
The PHP script receives these values using the POST method, storing then in $score and $scorecheck, respectively:
$score=$_POST['score'];
$scorecheck=$_POST['scorecheck'];
It must now calculate the expected checksum for the score it received.
This is done using the following code, which is basically a translation of the similar looking code in your Flash program:
for($c=0;$c<strlen($score);$c++) {
$realscorecheck+=$score{$c}*$c;
}
This takes the score, and carries out the same process of multiplying and adding mentioned earlier.
There are now three variables defined: $score, $scorecheck, and $realscorecheck.
If the score had been tampered with during transit, the $realscorecheck calculated would be different to the one sent by the Flash program.
To see whether the score is genuine, an if() statement is used to check the expected checksum, $realscorecheck, against the received checksum, $scorecheck:
if($scorecheck==$realscorecheck) {
// the checkums match, so the score is genuine
// enter the score into the high score table or whatever
}
else {
// the checksums do not match, indicating that the data has been tampered with
// display a message to this effect or whatever
}
Rufo.
|