Sunday, November 4, 2007

Validity of username and passwords

We have assumed that you have created a table called "users", and that table contain as much fields as you want, you can insert recordsinto that table using a form in html. But be sure to include two fields:
name= it is the username of the user
password= it is the password of each user
Simply create a form, using html, and set the action of that forum to a php file where you will include this small piece of code:

  1. //First lets get the username and password from the user
  2. $username=$_POST["username"];
  3. $password=$_POST["password"];
  4. //Second let's check if that username and password are correct and found in our database
  5. $sql1=mysql_query("SELECT name, password FROM users WHERE name='$username' AND password='$password'")
  6. if (mysql_num_rows($sql1)==0 mysql_num_rows($sql1)>1)
  7. {
  8. echo "Sorry, the username and password you submitted are not present in our database";
  9. }
  10. //if there are found in our database, and there is only one occurence of that username and password
  11. //thus making them valid, so inside, you can include the webpage you want to open
  12. if(mysql_num_rows($sql1)==1){include("the webpage");
  13. //open up the secure page
  14. //instead of "the webpage" type in the path your secure website is located in
  15. }
  16. ?>
I know it is a beginner level way to restrict access to a webpage, but it seems to work fine, for a single webpage. Check it out, might be useful.