Computer Forums

Member Login

Remember Me? Sign Up! | Forgot Password
 
Slogan
 
Closed Thread
Old 09-24-2007, 07:26 AM   #1 (permalink)
 
Newb Techie

Join Date: Sep 2007

Posts: 31

cfgcjm is on a distinguished road

Default Log in and verification

Hi everyone, I'm fairly new to web design so i'll apologize now for my lack of knowledge...let me explain what i need to do and maybe someone can help me out.

I need to create a website login area...where users can register and then log in via username and password to a secure section of the site. There is one little curve ball. What i need to happen is that we are controlling the people who are allowed to register. We are giving them a piece of paper at my place of work which has a code number on it. What i need to have the user registration form do is cross check first name last name and this verification code to allow the registration to continue. I've never built a site with user restrictions and i'm less than familiar with the software i'm using.

I have Expression 07 and Dreamweaver CS3

thanks a ton!
cfgcjm is offline  
Old 09-26-2007, 01:32 AM   #2 (permalink)
 
Newb Techie

Join Date: Sep 2007

Posts: 11

litebulb1 is on a distinguished road

Default Re: Log in and verification

you will need to use a little php and my sql. The book 'php and my sql for dynamic websites' has a companion website with all the scripts that are used in the book. (the .zip file ) You can look through the scripts and you will find that some of them will do exactly what you what except the verification number. Checking to make sure the verification number that you give them matches their name seems like a bit of work for me. that means every time you hand out a card you will have to go add their name and verification number manually. You should just have a random list of verification numbers.
litebulb1 is offline  
Old 09-26-2007, 12:47 PM   #3 (permalink)
CrazeD's Avatar
 
Wizard Techie

Join Date: Feb 2006

Location: Maine

Posts: 3,690

CrazeD will become famous soon enough

Send a message via AIM to CrazeD Send a message via MSN to CrazeD
Default Re: Log in and verification

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.
__________________

Need website help? PM me!
CrazeD is offline  
Old 09-28-2007, 01:59 PM   #4 (permalink)
 
Newb Techie

Join Date: Sep 2007

Posts: 31

cfgcjm is on a distinguished road

Default Re: Log in and verification

I'm getting the following parse error for the registration script

"Parse error: parse error, unexpected '<' in /homepages/6/d122159774/htdocs/stjohns/portal/register.php on line 79"

any ideas
cfgcjm is offline  
Old 09-29-2007, 11:48 AM   #5 (permalink)
 
Newb Techie

Join Date: Sep 2007

Posts: 31

cfgcjm is on a distinguished road

Default Re: Log in and verification

ok, i fixed the error but i don't understand how to lock out a page...

i put this script:

<?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!';
}

?>

on the page but when logged out it still shows the page and the "you must be logged in" text
http://www.stjohnsuccjonestown.org/portal/showlog.php
cfgcjm is offline  
Old 09-29-2007, 07:19 PM   #6 (permalink)
CrazeD's Avatar
 
Wizard Techie

Join Date: Feb 2006

Location: Maine

Posts: 3,690

CrazeD will become famous soon enough

Send a message via AIM to CrazeD Send a message via MSN to CrazeD
Default Re: Log in and verification

Quote:
Originally Posted by cfgcjm View Post
ok, i fixed the error but i don't understand how to lock out a page...

i put this script:

<?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!';
}

?>

on the page but when logged out it still shows the page and the "you must be logged in" text
http://www.stjohnsuccjonestown.org/portal/showlog.php
It is supposed to say you must be logged in when you are logged out.

Does it work when you login? Did you set up the MySQL tables?
__________________

Need website help? PM me!
CrazeD is offline  
 
Closed Thread

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On