P粉8951872662023-08-18 12:40:57
Guess you have encountered a problem with the execution order. For example, the [2] script has been executed when <div id="secret">
does not even exist in the DOM. You can simply add something like <script>var secret_var at [1] before outputting
<script src="js/checkout.js"></script> = <?= json_encode($secret_var); ?>;</script>
's content, so that it is public and known before checkout.js
is executed, and Already exists as a variable, no need to read the element content. Or just use an event such as window.onload
to perform [2] the reading of the element.
If the secret value changes and you need to update it, you can check for changes periodically, or better still, request the latest value before using it.
Please note that anything you output to the client is no longer a secret, if you really want to protect a value you have to keep it only on the server side, this is not the case in cases like API keys for payment gateways etc. Very important. Server side means storing the secret in a database or session. Then whenever you need to interact with the remote service, just call a PHP script (e.g. ajax) to perform that task on the server side, this way you separate the use of the secret to server side only and then you don't need to pass it to the client terminal or request an update.