Well, first thing is the form, it can be like this:
<form method="post" action="login.php">
<input type="text" name="username" value="Username" />
<input type="password" name="password" value="Password" />
<input type="submit" value="Login" name="Submit" />
</form>
Now what everything means:
<form method="post" action="login.php">
"method" = is a method what the form uses to pass on the variables, best to use post always.
"action" = the file that the form passes the variables to.
<input type="text" name="username" value="Username" />
"type" = text, its just plain text :)
"name" = is the name of the variable that is passed on, and in PHP we can access it like this:
[PHP]$_POST["username"][/PHP]
"value" = this is the text that is the default text in the forms field.
<input type="password" name="password" value="Password" />
Same goes for here, except the type is "password" it just shows on the form like: ****** instead of plain text, but it sends it just like plain text :)
Now for the PHP part.
index.php
[PHP]<?php
session_start();
?>
This is the index.php.<br />
<?php
if (strlen($_SESSION["loggedin"]) > 1) {
echo "<br />Welcome, ".$_SESSION["loggedin"]."!";
} else {
echo '<br />Please login:<br />
<form method="post" action="login.php">
<input type="text" name="username" id="username" maxlength="30" value="Username" />
<input type="password" name="password" id="password" maxlength="30" value="Password" />
<input type="submit" value="Kirjaudu" name="Submit" />
</form>';
}
?>[/PHP]
login.php
[PHP]<?php
session_start();
if (isset($_POST["username"], $_POST["password"]) && strlen($_POST["username"]) > 1 && strlen($_POST["password"]) > 1) {
$username = $_POST["username"];
$password = md5($_POST["password"]);
$account["test"] = "098f6bcd4621d373cade4e832627b4f6";
$account["test2"] = "ad0234829205b9033196ba818f7a872b";
$account["test3"] = "8ad8757baa8564dc136c1e07507f4a98";
if ($password == $account[$username]) {
$_SESSION["loggedin"] = $username;
header("Location: index.php");
} else {
header("Location: error.php");
}
} else {
header("Location: error.php");
}
?>[/PHP]
error.php
[PHP]There has been an error with your login.[/PHP]
I dont have time to explain the whole PHP part. But its pretty easy to understand. Just string lengths, arrays and if..then sentences.
I put the files on my host also:
http://www.frement.net/srl/death12652/index.php