reg.php
PHP Code:
//register.php
include "./db.inc";
$link_id = db_connect();
mysql_select_db("sample_db");
$country_array = enum_options('usercountry', $link_id);
mysql_close($link_id);
function in_use($userid) {
global $user_tablename;
$query = "SELECT userid FROM $user_tablename WHERE userid = '$userid'";
$result = mysql_query($query);
if(!mysql_num_rows($result)) return 0;
else return 1;
}
function register_form() {
global $userid, $username, $usercountry, $useremail, $userprofile, $country_array;
global $PHP_SELF;
?>
Create your account!
Desired ID
VALUE=""
SIZE="8" MAXLENGTH="8">
Desired Password
NAME="userpassword" SIZE="15">
Retype Password
NAME="userpassword2" SIZE="15">
Full Name
VALUE="" SIZE="20">
Country
for($i=0; $i < count($country_array); $i++) {
if(!isset($usercountry) && $i == 0) {
echo "
"\">" . $country_array[$i] . "\n";
}
else if($usercountry == $country_array[$i]) {
echo "" .
$country_array[$i] . "\n";
}
else {
echo "" .
$country_array[$i] . "\n";
}
}
?>
Email
VALUE="">
Profile
NAME="userprofile">
}
function create_account() {
global $userid, $username, $userpassword, $userpassword2,
$usercountry, $useremail, $userprofile;
global $default_dbname, $user_tablename;
if(empty($userid)) error_message("Enter your desired ID!");
if(empty($userpassword)) error_message("Enter your desired password!");
if(strlen($userpassword) < 4 ) error_message("Password too short!");
if(empty($userpassword2))
error_message("Retype your password for verification!");
if(empty($username)) error_message("Enter your full name!");
if(empty($useremail)) error_message("Enter your email address!");
if(empty($userprofile)) $userprofile = "No Comment.";
if($userpassword != $userpassword2)
error_message("Your desired password and retyped password mismatch!");
$link_id = db_connect($default_dbname);
if(in_use($userid))
error_message("$userid is in use. Please choose a different ID.");
$query = "INSERT INTO user VALUES(NULL, '$userid',
password('$userpassword'), '$username',
'$usercountry', '$useremail',
'$userprofile', curdate(), NULL)";
$result = mysql_query($query);
if(!$result) error_message(sql_error());
$usernumber = mysql_insert_id($link_id);
html_header();
?>
, thank you for registering with us!
User Number
Desired ID
Desired Password
Full Name
Country
Email
Profile
html_footer();
}
switch($action) {
case "register":
create_account();
break;
default:
html_header();
register_form();
html_footer();
break;
}
?>
db.inc
PHP Code:
$dbhost = 'localhost';
$dbusername = 'phpuser';
$dbuserpassword = 'phppass';
$default_dbname = 'sample_db';
$default_sort_order = 'ASC';
$default_order_by = 'usernumber';
$records_per_page = 5;
$user_tablename = 'user';
$access_log_tablename = 'access_log';
$MYSQL_ERRNO = '';
$MYSQL_ERROR = '';
$new_win_width = 600;
$new_win_height = 400;
function html_header() {
global $new_win_width, $new_win_height;
?>
function open_window(url) {
var NEW_WIN = null;
NEW_WIN = window.open ("", "RecordViewer", "toolbar=no,width="++",height="++",directories=no, status=no,scrollbars=yes,resize=no ,menubar=no");
NEW_WIN.location.href = url;
}
//-->
User Record Viewer
}
function html_footer() {
?>
}
function db_connect($dbname='') {
global $dbhost, $dbusername, $dbuserpassword, $default_dbname;
global $MYSQL_ERRNO, $MYSQL_ERROR;
$link_id = mysql_connect($dbhost, $dbusername, $dbuserpassword);
if(!$link_id) {
$MYSQL_ERRNO = 0;
$MYSQL_ERROR = "Connection failed to the host $dbhost.";
return 0;
}
else if(empty($dbname) && !mysql_select_db($default_dbname)) {
$MYSQL_ERRNO = mysql_errno();
$MYSQL_ERROR = mysql_error();
return 0;
}
else if(!empty($dbname) && !mysql_select_db($dbname)) {
$MYSQL_ERRNO = mysql_errno();
$MYSQL_ERROR = mysql_error();
return 0;
}
else return $link_id;
}
function sql_error() {
global $MYSQL_ERRNO, $MYSQL_ERROR;
if(empty($MYSQL_ERROR)) {
$MYSQL_ERRNO = mysql_errno();
$MYSQL_ERROR = mysql_error();
}
return "$MYSQL_ERRNO: $MYSQL_ERROR";
}
function error_message($msg) {
html_header();
echo "alert(\"Error: $msg\");history.go(-1)";
html_footer();
exit;
}
function enum_options($field, $link_id) {
$query = "SHOW COLUMNS FROM user LIKE '$field'";
$result = mysql_query($query, $link_id);
$query_data = mysql_fetch_array($result);
if(eregi("('.*')", $query_data["Type"], $match)) {
$enum_str = ereg_replace("'", "", $match[1]);
$enum_options = explode(',', $enum_str);
return $enum_options;
} else return 0;
}
?>
login.php
PHP Code:
//auth_user.php
include "./db.inc";
$register_script = "./reg.php";
function auth_user($userid, $userpassword) {
global $default_dbname, $user_tablename;
$link_id = db_connect($default_dbname);
$query = "SELECT username FROM $user_tablename
WHERE userid = '$userid'
AND userpassword = password('$userpassword')";
$result = mysql_query($query);
if(!mysql_num_rows($result)) return 0;
else {
$query_data = mysql_fetch_row($result);
return $query_data[0];
}
}
function login_form() {
global $PHP_SELF;
?>
}
session_start();
if(!isset($userid)) {
login_form();
exit;
}
else {
session_register("userid", "userpassword");
$username = auth_user($userid, $userpassword);
if(!$username) {
session_unregister("userid");
session_unregister("userpassword");
echo "Authorization failed. " .
"You must enter a valid userid and password combo. " .
"Click on the following link to try again.\n";
echo "Login";
echo "If you're not a member yet, click " .
"on the following link to register.\n";
echo "Membership";
exit;
}
else echo "Welcome, $username!";
}
?>
*** i had download this code from vbulletin, but i dont know what is is table it suppose to create,as what i knew was i need to create a table call
and the field inside must have. Isnt it got any fiels i leave? besides that, i also duno what is the attribute suppose to giev to each field.
hope that i can get a full statement for mysql and make it success for my database .10 a lot on helping a mysql newbies..
*****