When a user logs in, I save a token (hashed password) to the user's folder on the server and send the token also to the app which is saved in localStorage.
I need to check if the user's login token is valid upon every request made to the server. It's requested via AJAX using setTimeout, so next request is made only if previous call is completed (every 15 seconds).
Currently I have this function that verifies the token i.e checks if the token from the app equals the one on the server:
<?php if( verifyToken()) { //Success
} else {
//die
}?>
Below is the function verifyToken
function verifyToken() {
if ( empty( $_REQUEST['token'] )
|| empty($_REQUEST['username'] ) )
{
return false;
}
$username = $_REQUEST['username'];
$user_token = $_REQUEST['token'];
$secure_file = getUserDir( $username) . "/secure__.php";
if ( !file_exists( $secure_file) )
{
return false;
}
$token = file_get_contents( $secure_file);
if ( !$token )
{
return false;
}
if( $token==$user_token )
{
return true;
}
else
{
return false;
}
}
Does my approach above have disadvantages or should I consider checking against the database every 15 seconds?
I don't want to use sessions.
$_REQUEST
? Are you not in complete control regarding how this payload of data is being sent to your server? Have you read PSR-12 coding standards? \$\endgroup\$