im new to php. I have a basic unlink php file which deletes a test.html. Using Apache httpserver
<?php
$fh = fopen('test.html', 'a');
fwrite($fh, '<h1>Hello world!</h1>');
fclose($fh);
unlink('test.html');
?>
So now how do i set a authentication method so that only a person with the correct username/password can access this file?
If you want the protection in PHP, you’ll need to place the unlink code inside a conditional block:
That function might look like this:
And the form:
Putting
unlink()in a conditional block prevents it from being arbitrarily executed by some other means (run from command-line, included in another file, etc.).There are myriad ways to write the
ok_to_delete()function, of course. If you’re curious you might poke around on Google, or check out an open source project.For a full-blown user auth system, there are a lot of concerns regarding security (using https, whether the form can be exploited, whether you can delete arbitrary files, whether passwords are secure, etc.).
For something simple like deleting some cache files, the example code may suffice.
Refs: