Hi
Well, firstly, thought I would try Kevin's session tutorial.
I have the two required includes:
PHP Code:
<?php // db.php
$dbhost = "localhost";
$dbuser = "******";
$dbpass = "******";
function dbConnect($db="") {
global $dbhost, $dbuser, $dbpass;
$dbcnx = @mysql_connect($dbhost, $dbuser, $dbpass)
or die("The site database appears to be down.");
if ($db!="" and ![email=@mysql_select_db($db]!@mysql_select_db($db[/email]))
die("The site database is unavailable.");
return $dbcnx;
}
?>
PHP Code:
<?php // common.php
function error($msg) {
?>
<html>
<head>
<script language="JavaScript">
<!--
alert("<?=$msg?>");
history.back();
//-->
</script>
</head>
<body>
</body>
</html>
<?
exit;
}
?>
Then comes the access control script:
PHP Code:
<?php // accesscontrol.php
session_start();
include("common.php");
include("db.php");
if(!isset($uid)) {
?>
<html>
<head>
<title> Please Log In for Access </title>
</head>
<body>
<h1> Login Required </h1>
<p>You must log in to access this area of the site. If you are
not a registered user, <a href="signup.php">click here</a>
to sign up for instant access!</p>
<p><form method="post" action="<?=$PHP_SELF?>">
User ID: <input type="text" name="uid" size="8"><br>
Password: <input type="password" name="pwd" SIZE="8"><br>
<input type="submit" value="Log in">
</form></p>
</body>
</html>
<?php
exit;
}
session_register("uid");
session_register("pwd");
dbConnect("sessions");
$sql = "SELECT * FROM user WHERE
userid = '$uid' AND password = PASSWORD('$pwd')";
$result = mysql_query($sql);
if (!$result) {
error("A database error occurred while checking your ".
"login details.\\nIf this error persists, please ".
"contact [email=kevin@sitepoint.com]kevin@sitepoint.com[/email].");
}
if (mysql_num_rows($result) == 0) {
session_unregister("uid");
session_unregister("pwd");
?>
<html>
<head>
<title> Access Denied </title>
</head>
<body>
<h1> Access Denied </h1>
<p>Your user ID or password is incorrect, or you are not a
registered user on this site. To try logging in again, click
<a href="<?=$PHP_SELF?>">here</a>. To register for instant
access, click <a href="signup.php">here</a>.</p>
</body>
</html>
<?php
exit;
}
$username = mysql_result($result,0,"fullname");
?>
FINALLY, the actual sign-up script:
PHP Code:
<?php // signup.php
include("common.php");
include("db.php");
if (!isset($submitok)):
// Display the user signup form
?>
<html>
<head><title>New User Registration</title></head>
<body>
<h3>New User Registration Form</h3>
<p><font color=orangered size=+1><TT><B>*</B></TT></font>
indicates a required field</p>
<form method=post action="<?=$PHP_SELF?>">
<table border=0 cellpadding=0 cellspacing=5>
<tr>
<td align=right>
<p>User ID</p>
</td>
<td>
<input name=newid type=text maxlength=100 size=25>
<font color=orangered size=+1><TT><B>*</B></TT></font>
</td>
</tr>
<tr>
<td align=right>
<p>Full Name</p>
</td>
<td>
<input name=newname type=text maxlength=100 size=25>
<font color=orangered size=+1><TT><B>*</B></TT></font>
</td>
</tr>
<tr>
<td align=right>
<p>E-Mail Address</p>
</td>
<td>
<input name=newemail type=text maxlength=100 size=25>
<font color=orangered size=+1><TT><B>*</B></TT></font>
</td>
</tr>
<tr valign=top>
<td align=right>
<p>Other Notes</p>
</td>
<td>
<textarea wrap name=newnotes rows=5 cols=30></textarea>
</td>
</tr>
<tr>
<td align=right colspan=2>
<hr noshade color=black>
<input type=reset value="Reset Form">
<input type=submit name="submitok" value=" OK ">
</td>
</tr>
</table>
</form>
</body>
</html>
<?php
else:
// Process signup submission
dbConnect('sessions');
if ($newid=="" or $newname=="" or $newemail=="") {
error("One or more required fields were left blank.\\n".
"Please fill them in and try again.");
}
// Check for existing user with the new id
$sql = "SELECT COUNT(*) FROM user WHERE userid = '$newid'";
$result = mysql_query($sql);
if (!$result) {
error("A database error occurred in processing your ".
"submission.\\nIf this error persists, please ".
"contact [email=kevin@sitepoint.com]kevin@sitepoint.com[/email].");
}
if (@mysql_result($result,0,0)>0) {
error("A user already exists with your chosen userid.\\n".
"Please try another.");
}
$newpass = substr(md5(time()),0,6);
$sql = "INSERT INTO user SET
userid = '$newid',
password = PASSWORD('$newpass'),
fullname = '$newname',
email = '$newemail',
notes = '$newnotes'";
if (!mysql_query($sql))
error("A database error occurred in processing your ".
"submission.\\nIf this error persists, please ".
"contact [email=kevin@sitepoint.com]kevin@sitepoint.com[/email].");
// Email the new password to the person.
$message = "G'Day!
Your personal account for the Project Web Site
has been created! To log in, proceed to the
following address:
[url=http://www.theproject.com/]http://www.theproject.com/[/url]
Your personal login ID and password are as
follows:
userid: $newid
password: $newpass
You aren't stuck with this password! Your can
change it at any time after you have logged in.
If you have any problems, feel free to contact me at
<[email=kevin@sitepoint.com]kevin@sitepoint.com[/email]>.
-Kevin Yank
Project Webmaster
";
mail($newemail,"Your Password for the Project Website",
$message, "From:Kevin Yank <[email=kevin@sitepoint.com]kevin@sitepoint.com[/email]>");
?>
<html>
<head><title> Registration Complete </title></head>
<body>
<p><strong>User registration successful!</strong></p>
<p>Your userid and password have been emailed to
<strong><?=$newemail?></strong>, the email address
you just provided in your registration form. To log in,
click <a href="index.php">here</a> to return to the login
page, and enter your new personal userid and password.</p>
</body>
</html>
<?php
endif;
?>
Now, I have the DB (called 'sessions) created as well as the table 'user':
PHP Code:
CREATE TABLE `user` (
`ID` int(11) NOT NULL auto_increment,
`userid` varchar(100) NOT NULL default '',
`password` varchar(16) NOT NULL default '',
`fullname` varchar(100) NOT NULL default '',
`email` varchar(100) NOT NULL default '',
`notes` text,
PRIMARY KEY (`ID`),
UNIQUE KEY `userid` (`userid`)
) TYPE=MyISAM AUTO_INCREMENT=1 ;
The sign-up script works in that it loads, however, when I type some details and press 'OK' to submit, nothing actually happens?!
Can you guys see what I am doing wrong?
Thanks.
Mak
Bookmarks