PDA

View Full Version : Simple Login


Saj
05-22-2003, 02:02 PM
Hi.

I am creating a simple login script which doesn't use MySQL or a text file basicly because what I need to use is too simple to waste a MySQL database on. Anyway, here is the code:


$user = $_POST["user"];
$pass = $_POST["pass"];

if (!$submit) {
echo "<form action=\"admin.php\" method=\"post\">
<input type=\"text\" size=\"20\" maxlength=\"19\" name=\"user\" />
<input type=\"password\" size=\"20\" maxlength=\"19\" name=\"pass\" />
<input type=\"submit\" maxlength=\"19\" value=\"Submit\" name=\"submit\" />
</form>";
}
else {
if ($user != "admin" || $pass != "password" ) {
echo "Error! Incorrect Username or Password!
<form action=\"admin.php\" method=\"post\">
input type=\"text\" size=\"20\" maxlength=\"19\" name=\"user\" />
<input type=\"password\" size=\"20\" maxlength=\"19\" name=\"pass\" />
<input type=\"submit\" maxlength=\"19\" value=\"Submit\" name=\"submit\" />
</form>";
}
else {
echo "Login successful<br /> <a href=\"admin.php?page=test\">Test Page</a>";
}
}
if ($page == "test" ) {
echo "This is the test page";
}


Basicly it prints a form, if the login is incorrect, it adds an error message, but if you get it correct, it moves to another page, admin.php?page=test. However, when I go to that page, the login page comes back up again because I "have not submitted" Is there a way to overcome this?

Thanks

thox
05-22-2003, 02:44 PM
you need the cookies (or an equivilent) to remember your login. when you go to the new page the username and password you have just inputted are not carried over.

Fatman
05-22-2003, 03:46 PM
yeah youll either need to set a cookie or a session variable so that the details are carried on.

There some decent examples at www.php.net

Saj
05-22-2003, 03:49 PM
Well I'm not going to need a cookie, since there is only one login, but I'll check out sessions.

Fatman
05-22-2003, 03:51 PM
if you set cookies with no expiration date there only valid for that session anyway :)