Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

basic php password protect

What i have right now is an apache server that is holding some folders containing pictures and some home videos. I have port forwarded and got it to show the folders when i type in my ip. My only problem is that anyone can access it from all over the world.i found this php code online so i can password protect these files:

<?php 

// Define your username and password 
$username = "someuser"; 
$password = "somepassword"; 

if ($_POST['txtUsername'] != $username || $_POST['txtPassword'] != $password) { 

?> 

<h1>Login</h1> 

<form name="form" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>"> 
    <p><label for="txtUsername">Username:</label> 
    <br /><input type="text" title="Enter your Username" name="txtUsername" /></p> 

    <p><label for="txtpassword">Password:</label> 
    <br /><input type="password" title="Enter your password" name="txtPassword" /></p> 

    <p><input type="submit" name="Submit" value="Login" /></p> 

</form> 

<?php 

} 
else { 

?> 

<p>This is the protected page. Your private content goes here.</p> 

<?php 

} 

?> 

I found the line where it says to put my private content but i dont understand how to do this. im not trying to protect any html or php page im trying to protect a few folders. PS i can do this fine by ftping into my server to access the files but i want to be able to also access them from any browser.

like image 950
Thomas Avatar asked Mar 24 '23 17:03

Thomas


1 Answers

Try to learn htpasswd and implement it.

You can create htpasswd file through this.Just enter username and password and an entry for a htpasswd file is generated. You can use the htaccces Authentication generator to create a htaccess file that will password protect your site or a directory. This htpasswd generator creates passwords that are hashed using the MD5 algorithm, which means that you can use it for sites hosted on any platform, including Windows and Linux. You can also create htpasswd passwords with PHP on your own server – this technique only works on Linux though. Read more about htpasswd files.

Learn more about htpasswd from here.

like image 178
Jerin K Alexander Avatar answered Apr 01 '23 03:04

Jerin K Alexander