-
- 15 Posts
Hi there,
I need a little bit of a pointer.
I am using a php function named hash_hmac.
The hash works perfectly when processed straight from a php file, even from the same server that modx is sat on.
When however, this code is ran from a snippet I get a completely wrong hash.
Does anyone know what could be going wrong?
Many thanks
I put the
PHP documentation's sample in a .php file, and the code into a snippet, and they both produced the same hash. This is on wildly different servers; the file on my local Mac and the snippet on a remote Linux hosted Revo 2.2.4
The snippet:
<?php
$output = hash_hmac('ripemd160', 'The quick brown fox jumped over the lazy dog.', 'secret');
return $output;
Ok, the api_key value is not getting loaded into the function. You can add it to the function argument list instead:
$api_key = "1234AbCD";
function get_verification($var_name, $var_value, $var_code, $api_key) {
$encodingval = htmlspecialchars($var_code) . htmlspecialchars($var_name) . htmlspecialchars($var_value);
return '||'.hash_hmac('sha256', $encodingval, $api_key).($var_value == "--OPEN--" ? "||open" : "");
}
$name = get_verification('name', 'My Widget', 'mycode', $api_key);
$code = get_verification('code', 'mycode', 'mycode', $api_key);
$price = get_verification('price', '1.99', 'mycode', $api_key);
return $name;
This may have something to do with the globals issue...
snippet code does not run in the global namespace in the first place.
So before, you were addressing the globals in your function, then outside of the function, the variables were not actually globals as they may have seemed. To make predictable use of globals in a snippet, you really need to address them via the $GLOBAL array e.g. $GLOBAL['myPrice']
http://forums.modx.com/thread/32924/functions-w-switch-case-and-global-variable-errors#dis-post-180705
-
- 15 Posts
Thank you,
Excuse my ignorance but I'm not sure what to do next.
Should I move $api_key into the function?
-
- 15 Posts
Ah I think I get it. I will report back on if it works!
-
- 15 Posts
Its Worked! Thank you, you're wonderful!