I am trying to implement a simple login form. Here is the of layout my localhost server folder structure:
www/
html/
index.php
home.php
register.php
controller/
authenticate.php
my index php file is a simple login form. See code below:
<?php
// enable session
session_start();
// redirect user to homepage if he is logged in (i.e. if his session is authenticated)
if (isset($_SESSION["authenticated"]) && $_SESSION["authenticated"]==true){
$host = $_SERVER["HTTP_HOST"];
$path = rtrim(dirname($_SERVER["PHP_SELF"]), "/\\");
header("Location: http://$host$path/home.php");
exit;
}
?>
<html>
<head>
<title>Log In</title>
</head>
<body>
<form action="../controller/authenticate.php" method="POST">
<table>
<tr>
<td>Username:</td>
<td>
<input name="username" type="text"></td>
</tr>
<tr>
<td>Password:</td>
<td><input name="password" type="password"></td>
</tr>
<tr>
<td></td>
<td><input type="submit" value="Log In"></td>
</tr>
</table>
</form>
<a href="register.php"> <br> Register </a>
</body>
</html>
My authenticate.php file is just a php document that checks if the database has a row with the username and password that was posted. And if the username and password are correct it redirects the user to home.php. When I try submit the login form on page localhost/index.php I get an error 404 saying that the url http://localhost/controller/authenticate.php was not found on the server (but its definitely there).
I know the problem is with the form action attribute but I can't figure out why it does not work. The path seems correct and the url in the browser shows the correct path too.
There are similar posts on the topic of the form action attribute landing on a script file in another folder (see links below). I went through them but still can't figure out my problem. Please help :)
Also, is there maybe a way to use the server superglobal inside the form action attribute rather than manually typing up the path?
Similar posts: