I need help with simple PHP login script?

cmaclean

New member
Hi there,

I'm working on a class assignment where I need to create a simple PHP login script. I've got it basically completed, but I'm having trouble with 1 thing. See details:
1. index.php page will first check to see if a user is logged in. If so, display a logout link.
2. If user is not logged in, display a form using the get method. The action for the form must be set to this same file (index.php).
3. The username and password needs to be validated. If there's no username/password or and incorrect username/password, display and error.

Everything is basically set, but for some reason, the text 'Please enter a username and password' ALWAYS shows when I first visit the file.
Here is the code:

<?php

session_start();

if(isset($_SESSION["username"]) && ($_SESSION["password"])){
echo '<a href="logout.php">log out</a>';

}else{
?>
<form action="index.php" method="get">
<table>
<tr>
<td>Username</td>
<td><input type="text" name="username">
</tr>
<tr>
<td>Password</td>
<td><input type="text" name="password"></td>
</tr>
<tr>
<td>Â*</td>
<td><input type="submit" value="Log in"></td>
</tr>
</table>
</form>
<?php
$username = $_GET["username"];
$password = $_GET["password"];

if(($username == "admin") && ($password == "admin")){
$_SESSION["username"] = $username;
$_SESSION["password"] = $password;
echo "thanks for logging in!";
}
elseif(!(($username)&&($password))){
echo "Please enter a username and password";
}
else{
echo "Incorrect username/password combination";
}
}
?>
 
Back
Top