Home > Net >  Variables not saving after reloading a page
Variables not saving after reloading a page

Time:01-28

When I ran the code below, it would usually add a number to testUses if there was a successful login. However, when I reloaded, you could still access home.html and the variable testUses still stayed 0 which meant that for some reason, it did not change. Should I put testUses outside the function? Please answer a fix or tip to change this.

<!DOCTYPE html>
<html>
  <head>
    <script>window.history.replaceState(null, document.title, "/");</script>
    <meta http-equiv="Cache-Control" content="no-cache, no-store, must-revalidate"/>
    <meta http-equiv="Pragma" content="no-cache"/>
    <meta http-equiv="Expires" content="0"/>
    <title>A-ENGINE</title>
    <meta name="viewport" content="width=device-width">

    <link href='https://fonts.googleapis.com/css?family=Quicksand' rel='stylesheet'>
    <style>
      .full {
        width: 100%;
        height: 100%;
        top: 0px;
        left: 0px;
        position: absolute;
        overflow: hidden;
      }
      .center {
        left: 50%;
        top:50%;
        transform: translate(-50%, -50%);
        position: absolute;
      }
    </style>
  </head>
  <body style='overflow:hidden;padding:0px;visibilty:hidden'>
    <div class='full'>
      <div class='center' style='width:300px;padding:2px'>        
<div ><div ></div></div>

<input type= password id="access" placeholder="Access Key" onchange="access()"><br>

     <script>

function access() {
let input = document.getElementById("access").value;

let testUses =  localStorage.getItem('testUses') || 0;

const testHash = "cyrb53('a') -> 4625896200565286";
const adminHash = "cyrb53('a') -> 4413594719508086"; 

const cyrb53 = (str, seed = 0) => {
  let h1 = 0xdeadbeef ^ seed,
    h2 = 0x41c6ce57 ^ seed;
  for (let i = 0, ch; i < str.length; i  ) {
    ch = str.charCodeAt(i);
    h1 = Math.imul(h1 ^ ch, 2654435761);
    h2 = Math.imul(h2 ^ ch, 1597334677);
  }
  
  h1 = Math.imul(h1 ^ (h1 >>> 16), 2246822507) ^ Math.imul(h2 ^ (h2 >>> 13), 3266489909);
  h2 = Math.imul(h2 ^ (h2 >>> 16), 2246822507) ^ Math.imul(h1 ^ (h1 >>> 13), 3266489909);
  
  return 4294967296 * (2097151 & h2)   (h1 >>> 0);
};

let inputHash = (`cyrb53('a') -> ${cyrb53(input)}`);

function home() {
  document.write('<iframe src="home.html" height="100%" width="100%"></iframe>');
}
  
if (inputHash == adminHash || inputHash == testHash) { 
  home();
} else if (inputHash == testHash && testUses <= 1) {
  home();
  testUses  ;
localStorage.setItem('testUses', testUses);
} else {
  document.getElementById('access').value = '';

  }
} 


       
</script>     

    </div>  
  </body>
</html>
    

CodePudding user response:

You can store testUses in localStorage.

let testUses =  localStorage.getItem('testUses') || 0;
// update value:
testUses  ;
localStorage.setItem('testUses', testUses);
  • Related