---------------------------STEP ONE-----------
1st - create a table in your db named "users_table" with 3 fields, the first being "id" (INT, autoincrement, unique) the second being "username" (CHAR, unique) and the third being "password" (char)
2nd - create a new document, add a form, and within the form add two text boxes and a submit button. The first text field should be named "username" and the second "password"
3rd - set the form action to "signup.php?submit_reg=true"
4th, add the following code somewhere in "signup.php"
---------STEP 2 - REGISTRATION PROCESSING-------
<?php
$conn = mysql_connect("host", "user", "pass") or die(mysql_error());
@mysql_select_db("db_name", $conn) or die (mysql_error());
$sql = "INSERT INTO users_table (Id, username, password) VALUES ('', '$_POST[username]', '$_POST[password]'";
if ($_GET[submit_reg] == true) {
if ($_POST[username] == "" or $_POST[password] == "")
print "You must fill in both text fields!";
} else {
mysql_query($sql, $conn);
print "You are registered! <p> CLICK HERE to login!";
}
}
?>
You could add a bunch of error checking and stuff to that but i dont want to type much and its easier for beginners this way.
-------------------------STEP 3 -------LOGIN-------------
1st - create new document, named "login.php"
2nd - add a form, and within the form two text fields and a submit button. Again, name the first text field "username" and the second "password"
3rd - make the form action "login.php?submit_login=true
4th - add the following code at the top of the page/code
------ simple login script------
<?php session_start(); ?>
<?php
$conn = mysql_connect("host", "user", "pass") or die(mysql_error());
@mysql_select_db("db_name", $conn) or die (mysql_error());
$sql = "SELECT * FROM users_table WHERE username = '$_POST[username]' and password = '$_POST['password']'";
$query_sql = mysql_query($sql, $conn) or die (mysql_error());
if ($_GET[submit_login] == true) {
if (mysql_num_rows($query_sql) == 1) {
print "Your are now logged in!";
$_SESSION[username] = $_POST[username];
} else {
print ("incorrect username or password");
}
}
?>
Now upload both files to your server and test it out.
If there are any parse errors, post what happened here and ill revise the code (i wrote it quickly and did not proofread at all)
--------ALL FINISHED!-----------ADDITIONAL NOTES:----------
-make sure to put "session_start();" on every page
and to tell if they are logged in, just use:
if ($_SESSION[username]) {
// actions
} else {
print "not logged in";
}
-make sure your host supports php and includes a db
-------------FOR MORE PHP TUTORIALS : www.phpfreak.com ------
This PHP Tutorial brought to you by: www.myfreesite.cjb.net