Ok I have been asked by my college lecturer to create a login system using PHP. Now I read up about PHP seeing as Im not familiar to it. I used this code and coded some myself.
session_start();
$username = $_POST ['username'];
$password = $_POST ['password'];
if ($username&&$password) {
$connect = mysql_connect("localhost","root","") or die("Couldn't Connect");
mysql_select_db("phplogin") or die ("Couldn't find Database");
$query = mysql_query("SELECT * FROM users WHERE username='$username'")'
$numrows = mysql_num_rows ($query);
if ($numrows!=0) {
while ($row = mysql_fetch_assoc()) {
$dbusername = $row['username'];
$dbpassword = $row['password'];
}
if ( ($username==$dbusername) && ($password==$dbpassword) ) {
echo "you are in, Click here to enter member page";
$_SESSION['username']=$dbusername;
} else {
echo "Incorrect password";
} else {
die ("That user does not exist");
}
echo $numrows;
} else {
die("Please enter a username and password")
}
?>
This is the Index.php I am using.
My login page is simple with 2 fields: Username and Password, with a login and register button.
Once I enter details into the fields that I have taken from my database, it displays this at the very top of the page.
Click here to enter member page"; $_SESSION['username']=$dbusername; } else echo "Incorrect password"; } else die ("That user does not exist"); echo $numrows; } else die("Please enter a username and password") ?>
I also have a problem with my register page which has 4 fields that you MUST fill in: Username, Email, Password and Confirm Password, along with a Register button to "Register" as a user.
//form data $name = strip_tags ($_POST['name']); $username =
strip_tags ($_POST['username']); $password = md5 (strip_tags
($_POST['password'])); $confirmpassword = strip_tags
($_POST['confirmpassword']); $date = date("Y-m-d");
if ($submit) {
if ($username&&$password&&$confirmpassword) { //encrypt password
$password = md5 ($password); $confirmpassword = md5
($confirmpassword);
if ($password==$confirmpassword) { //check character length if (strlen ($username) >25 || strlen ($name) >25) { echo
"length of Username or Name is too long"; } else {
//checks password length
if (strlen ($password) >20||strlen($password)<5)
{
echo "password must be between 5 and 20 characters";
}
else
{
//registers user
} } } else echo "Your passwords to not match!";} else echo "Please fill in all feidls!"; } ?>
Your full Name:
Choose a Username:
Choose a Password:
Confirm your Password:
HOWEVER, even before entereing detail to hte fields, this large bulk of code appaears at the top of the page.
25 || strlen ($name) >25) { echo "length of Username or Name is too long"; } else { //checks password length if (strlen ($password) >20||strlen($password)<5) { echo "password must be between 5 and 20 characters"; } else { //registers user } } } else echo "Your passwords to not match!"; } else echo "Please fill in all feidls!"; } ?>
Can someone please tell me what I have done wrong here? I use Xampp to make my database.
No comments:
Post a Comment