0

Hello i am still learning but was wondering if anyone here could help i have the two function be low i would like to convert them to JS to store in an external file then be able to call one of the function when i need them from PHP. I'm sorry i'm just learning JS and have been looking in to doing them but keep hitting road blocks.

I will be grateful for any help.

//This is the Token decode function
 function security_array_decode($myuuid, $token_salt)
 {
   $s_array = array(); 
   $i = 0;
    do {
     $untime = time() - $i;
     $arr = array($myuuid, $untime, $token_salt);
     $token_key = sha1(join("",$arr));
     $s_array[] = $token_key;
     $s_array[] = $untime;
     ++$i;
       } while ($i <= 5);
   return $s_array;
}


//This is the Token encode function
function security_array_encode($myuuid, $token_salt)
{
     $untime = time();
     $arr2 = array($myuuid, $untime, $token_salt);
     $token_key = sha1(join("",$arr2));
   return $token_key;
}

UPDATE:

Just Updating with code i have worked out, but not sure if it works just yet trying to work out how to call it from the below PHP.

function security_array_decode(myuuid, token_salt)
{
    var s_array = [];
    while (i < 5) {
       var d = new Date();
       var seconds = Math.round(d.getTime() / 1000) - i;
         var keys = [myuuid, seconds, token_salt];
           var token_key = SHA1(keys.join(''));
          s_array.push(token_key);
       i++;
    }

    return s_array;
}

function security_array_encode(myuuid, token_salt)
{
  var d = new Date();
  var seconds = Math.round(d.getTime() / 1000); 
    var keys = [myuuid, seconds, token_salt];
      var token_key = SHA1(keys.join(''));

    return token_key;
}

/**
* Secure Hash Algorithm (SHA1)
* http://www.webtoolkit.info/
**/

This is the php where i need to call one of the function that will be stored in a .js file named security.js

    if(in_array($variables->token, security_array_decode($variables->uuid, $token_salt)))
  {

  }
  • 1
    It's not at all straightforward to call javascript from PHP. Your best bet is to use some sort of lightweight messaging queue like RabbitMQ to send the message from PHP to a node.js function. If you don't want to do that, you could try something like this: github.com/kylekatarnls/nodejs-php-fallback – Arlen Dec 31 '19 at 15:18
  • ok thank you Arlen, Guess i'll just leave it as a PHP function. No big deal just thought it mite save load time or something. – Christopher Cox Dec 31 '19 at 19:44
  • 1
    As a general rule, server-side execution is more secure, client-side execution yields a faster response to the user. So you pick how much of one you want to trade for the other. as for mixing languages without mixing execution location, when I've done it, I've done it more for convenience than speed. In this specific case, you could use PHP to write the javascript that executes on the client, or you could execute the javascript on the server via something like node.js, but there's no "common ground" where those two languages to meet, hence the messaging queue. – Arlen Jan 3 at 14:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.