简体   繁体   中英

Check if the value of the local storage is equal or not to that of a Php variable

hello I have this php file with the name ** request.php ** that has this Code:

 <?php $Password_user = 'AIzaSyA'; ?>

and I have a Index.html file, and I also have a Local Storage with that name user_pass so I created a variable with the name get_spass that looks like this: var get_spass= localStorage.getItem('user_pass'); so how can i create a request in Ajax that is able to check if the value of the Storage location is equal or not gives it Variable ** Password_user of request.php ?

You have to change your php file to:

<?php
$Password_user = 'AIzaSyA';
echo $Password_user;
?>

In your html file you can fetch the file and check the password value with this code:

var get_spass= localStorage.getItem('user_pass');
var passIsCorrect = checkPassword(get_spass);

if (passIsCorrect) {
    //do stuff...
}

async function checkPassword(get_spass) {
    const passwordPhp = await fetch("request.php");
    if (get_spass === passwordPhp) {
       return true;
    }
    return false;
}

Note that everyone can get the request.php content. If the password need to be secret you have to implement some sort of authentication to assure the request.php file can be read only by authorized users or person.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM