Indeed you will need PHP and MySQL.
I'm going to write these scripts on the assumption that you want to have a unique key for each user that registers. If that is not the case, tell me and I'll change it.
You're going to have to add a bunch of registration keys to the MySQL database if you're going to do it that way, so that it can see if they're valid.
You'll need to create a MySQL table first. I recommend you look up some tutorials on basic MySQL, it's very simple to learn and very useful.
You will need two MySQL tables. One for users, and one for the registration keys.
For the users, paste this code into your MySQL database:
Code:
CREATE TABLE users (users_id INT NOT NULL AUTO_INCREMENT PRIMARY KEY, username VARCHAR(50) NOT NULL, password VARCHAR(32) NOT NULL, regkey VARCHAR(20) NOT NULL)
For the registration keys, paste this code into your MySQL database:
Code:
CREATE TABLE reg_keys (key_id INT NOT NULL AUTO_INCREMENT PRIMARY KEY, regkey VARCHAR(20) NOT NULL)
Now you'll need to make a mysql.php file, so you can connect to your database and run your queries. You'll have to fill in the blank ' 's with your database information.
Code:
<?php
/* Connect To MySQL */
$host = 'localhost'; // your host name
$dbuser = ''; // your database username
$dbpass = ''; // your database user password
$dbname = ''; // your database name
$connect = @mysql_connect ($host, $dbuser, $dbpass) or die ('Could not connect to MySQL!'); // attempt to connect
$select_db = @mysql_select_db ($dbname) or die ('Could not select database!'); // attempt to select database
?>
Next, you'll need to make your register.php script.
Code:
<?php
/* Registration Script */
require ('mysql.php');
if (isset ($_POST['submit'])) { // Check to see if the form has been submitted
$username = $_POST['username'];
$password = md5 ($_POST['password']); // MD5 encrypt the password so it is more secure
$regkey = $_POST['regkey'];
// See if the key is valid
$sql = "SELECT * FROM reg_keys WHERE regkey='$regkey' LIMIT 1";
if ($r = mysql_query ($sql)) {
$num = mysql_num_rows ($r);
if ($num > 0) { // if there is a row with that key, the key is valid
// The key is valid, add user to the database
$add_sql = "INSERT INTO users (users_id, username, password, regkey) VALUES (0, '$username', '$password', '$regkey')";
// We make the first value (for users_id) 0 because it is set to auto increment,
// so the users_id will be assigned to the next available number
if ($add_r = mysql_query ($add_sql)) {
// The user successfully registered
print 'Thank you for registering!';
// Delete the key from the key database, so it cannot be used again
mysql_query ("DELETE FROM reg_keys WHERE regkey='$regkey'");
} else {
// The user did not successfully register
print 'Error:' . mysql_error(); // print the MySQL error
}
} else {
// The key is not valid
print 'The registration key you entered was not valid!';
}
} else {
// The regkey check query failed
print 'Error:' . mysql_error(); // print the MySQL error
}
} else { // The form was not submitted
// Display the form
print '<form action="register.php" method="post">
<table border="0">
<tr>
<td align="right">Username:</td>
<td align="center"><input type="text" name="username" /></td>
</tr>
<tr>
<td align="right">Password:</td>
<td align="center"><input type="password" name="password" /></td>
</tr>
<tr>
<td align="right">Registration Key:</td>
<td align="center"><input type="text" name="regkey" /></td>
</tr>
<tr>
<td></td>
<td align="center"><input type="submit" name="submit" value="Submit" /></td>
</tr>
</table>
</form>';
}
?>
Now you need a login script:
Code:
<?php
/* Login Script */
require ('mysql.php');
session_start();
if (isset ($_POST['submit'])) { // Check to see if the form has been submitted
$username = $_POST['username'];
$password = md5 ($_POST['password']); // we MD5 encrypted the password at registration,
// so we must do this on the login as well
// See if the user exists
$sql = "SELECT * FROM users WHERE username='$username' AND password='$password'";
if ($r = mysql_query ($sql)) {
$row = mysql_fetch_array ($r);
$num = mysql_num_rows ($r);
if ($num > 0) { // if there is a row with that username/password, the user exists
// Now we can assign some SESSIONS, so we can verify on later pages that the user is "logged in"
$_SESSION['users_id'] = $row['users_id'];
$_SESSION['username'] = $row['username'];
$_SESSION['loggedin'] = TRUE; // This is where we will check to see if the user is logged in
print 'Thank you for logging in!';
} else {
// User does not exist
print 'Invalid Username/Password!';
}
} else {
// The query failed
print 'Error:' . mysql_error(); // print the MySQL error
}
} else { // The form was not submitted
// Display the form
print '<form action="login.php" method="post">
<table border="0">
<tr>
<td align="right">Username:</td>
<td align="center"><input type="text" name="username" /></td>
</tr>
<tr>
<td align="right">Password:</td>
<td align="center"><input type="password" name="password" /></td>
</tr>
<tr>
<td></td>
<td><input type="submit" name="submit" value="Submit" /></td>
</tr>
</table>
</form>';
}
?>
Now, a logout script:
Code:
<?php
/* Logout Script */
session_start();
unset ($_SESSION);
session_destroy();
print 'You are now logged out!';
?>
And finally, you need a way to see if the user is logged in. For every page that you need the user logged in, you must start the page with session_start()
Code:
<?php
/* Is User Logged In? */
session_start();
// this is just your every day page where you would want someone logged in to view the info
if ($_SESSION['loggedin'] == TRUE) { // user is logged in
print 'If you can read this, you are logged in!';
} elseif ($_SESSON['loggedin'] == FALSE) { // user is not logged in
print 'You must be logged in to read this!';
}
?>
Well, there you have it. A basic register/login system. Feel free to steal this code and do whatever you want with it.
If you need more help, make sure to ask!
Enjoy.