简体   繁体   中英

Javascript Login for securing assets folder on hosted domain

I want to create an index.html which fires when someone tries to access files & folders that are on my domain. For ex.

www.example.com/user_ini/somefile

Now i want to keep a index file that validates a user using javascript and if validated allows to access that folder and files else stops them.

Something similar to router admin access.

Can .htaccess help me here ?

I want to do this using javascript and html. I tried doing this:

<form name="login">
Username<input type="text" name="userid"/>
Password<input type="password" name="pswrd"/>
<input type="button" onclick="check(this.form)" value="Login"/>
<input type="reset" value="Cancel"/>
</form>
<script language="javascript">
function check(form)/*function to check userid & password*/
{
 /*the following code checkes whether the entered userid and password are matching*/
 if(form.userid.value == "myuserid" && form.pswrd.value == "mypswrd")
  {
    window.open('target.html')/*opens the target page while Id & password matches*/
  }
 else
 {
   alert("Error Password or Username")/*displays error message*/
  }
}
</script>

Now the folder might have multiple files. So basically what i want is if the user is valid he can have access to all folders and files or else denied.

You can use PHP as a server side to authenticate the user. You can use form action that will call the PHP code on submit the form to authenticate the user and revert back with the success or failure response.

Form :

<form name="login" action="<?php echo $_SERVER['PHP_SELF'];?>" method = "post">
Username<input type="text" name="userid"/>
Password<input type="password" name="pswrd"/>
<button type="submit" name="login">Login</button>
</form> 

PHP File :

     <?php
        $msg = '';

        if (isset($_POST['login']) && !empty($_POST['userid']) 
           && !empty($_POST['pswrd'])) {

           if ($_POST['userid'] == 'testuser' && 
              $_POST['pswrd'] == '1234') {
              $_SESSION['valid'] = true;
              $_SESSION['timeout'] = time();
              $_SESSION['userid'] = 'testuser';

              echo 'You have entered valid use name and password';
           }else {
              $msg = 'Wrong username or password';
           }
        }
     ?>

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