<?php define('TOKEN_HASH_ALGO', 'sha3-512'); function getSalt() { return 'e3ba7756af5c69de0cfdff26b21be3c2'; } function getDefaultExpiryTime() { return time() + 24 * 60 * 60; /* +24 hrs */ } function hashData($data) { return hash(TOKEN_HASH_ALGO, $data); } function generateSecureHash($hashIdentifier, $data) { // mitigate rainbow table attack $halfDataLen = strlen($data) / 2; $stringToHash = $hashIdentifier . substr($data, 0, $halfDataLen) . getSalt() . substr($data, $halfDataLen) ; return hashData($stringToHash); } function generatePasswordResetToken($user, $keySuffix, $expiryTimestamp = null) { /* * Piwik does not store the generated password reset token. * This avoids a database schema change and SQL queries to store, retrieve, and purge (expired) tokens. */ if (!$expiryTimestamp) { $expiryTimestamp = getDefaultExpiryTime(); } $expiry = strftime('%Y%m%d%H', $expiryTimestamp); $token = generateSecureHash( $expiry . $user['login'] . $user['email'] . $user['ts_password_modified'] . $keySuffix, $user['password'] ); return $token; } function isTokenValid($token, $user, $keySuffix) { $now = time(); // token valid for 24 hrs (give or take, due to the coarse granularity in our strftime format string) for ($i = 0; $i <= 24; $i++) { $generatedToken = generatePasswordResetToken($user, $keySuffix, $now + $i * 60 * 60); if ($generatedToken === $token) { return true; } } // fails if token is invalid, expired, password already changed, other user information has changed, ... return false; } $keySuffix = 'mykeysuffix'; $user = ['login'=>'myusername', 'email'=>'[email protected]', 'password'=>'mypassword', 'ts_password_modified'=>time()]; $resetToken = generatePasswordResetToken($user, $keySuffix); echo 'resetToken #1='.$resetToken.PHP_EOL; echo 'resetToken #2='.generatePasswordResetToken($user, $keySuffix).PHP_EOL; echo 'isTokenValid='.isTokenValid($resetToken, $user, $keySuffix).PHP_EOL; echo 'isTokenValid (should fail)='.isTokenValid($resetToken.'_somegarbage', $user, $keySuffix).PHP_EOL; //sleep10; //echo 'isTokenValid='.isTokenValid($resetToken, $user, $keySuffix).PHP_EOL;
Write, Run & Share PHP code online using OneCompiler's PHP online compiler for free. It's one of the robust, feature-rich online compilers for PHP language, running on the latest version 7. Getting started with the OneCompiler's PHP compiler is simple and pretty fast. The editor shows sample boilerplate code when you choose language as PHP
and start coding.
OneCompiler's PHP online editor supports stdin and users can give inputs to programs using the STDIN textbox under the I/O tab. Following is a sample PHP program which takes name as input and prints hello message with your name.
<?php
fscanf(STDIN, "%s\n", $name);
echo "Hello ".$name.".\n";
?>
PHP(Hypertext Preprocessor) is widely used server sripting language by Rasmus Lerdorf in the year 1994.
In PHP, there is no need to explicitly declare variables to reserve memory space. When you assign a value to a variable, declaration happens automatically. Variables are case-sensitive in PHP.
$variable_name = value;
If, If-else, Nested-Ifs are used when you want to perform a certain set of operations based on conditional expressions.
if(conditional-expression){
//code
}
if(conditional-expression){
//code if condition is true
} else {
//code if condition is false
}
if(condition-expression1) {
//code if above condition is true
} elseif(condition-expression2){
//code if above condition is true
}
elseif(condition-expression3) {
//code if above condition is true
}
...
else {
//code if all the conditions are false
}
Switch is used to execute one set of statement from multiple conditions.
switch(conditional-expression) {
case value1:
// code if the above value is matched
break; // optional
case value2:
// code if the above value is matched
break; // optional
...
default:
// code to be executed when all the above cases are not matched;
}
For loop is used to iterate a set of statements based on a condition.
for(Initialization; Condition; Increment/decrement){
// code
}
// you can use any of the below syntax
foreach ($array as $element-value) {
//code
}
foreach ($array as $key => $element-value) {
//code
}
While is also used to iterate a set of statements based on a condition. Usually while is preferred when number of iterations are not known in advance.
while(condition) {
// code
}
Do-while is also used to iterate a set of statements based on a condition. It is mostly used when you need to execute the statements atleast once.
do {
// code
} while (condition);
Function is a sub-routine which contains set of statements. Usually functions are written when multiple calls are required to same set of statements which increases re-usuability and modularity.
function function_name(parameters) {
//code
}
function_name (parameters)