Page 1 of 1
[SOLVED]Clean Sweap - Registration page
Posted: Sat Dec 27, 2003 5:49 am
by vigge89
EDIT: Look att the bottom of this page to see updated code
I've got this code for my registration page, but as it is now, when i get errors (when username is empty, password empty, passwords didn't match etc..), they doesn't show up at all, and if I don't get any errors, i get a blank page, and the mysql isn't processed. Here's my "long" code:
register.php:
Code: Select all
<?php
//taken away due to extra loading time
?>
The echo_header() & echo_footer() are functions that echoes the headers and footers.
What's wrong in this code?
Posted: Sat Dec 27, 2003 6:32 am
by Weirdan
It collects error messages in $errormsg var but doesn't echo it anywhere, I guess. Try this:
Code: Select all
function echo_register() { //Function echo_register
global $errormsg;
if(!empty($errormsg)) echo $errormsg;
echo "
<tr>
<td colspan='2'>
<b>Register new account:</b><br>Fields with a star (*) is required, maxlenght for usernames are 12 characters & passwords 12.<br>$errormsg
</td>
</tr><tr><form name='register' method='post' action='register.php'>
<td class='lclmn'>*Username:</td> <td class='rclmn'><input type='text' name='username' size='20'></td>
</tr><tr>
<td class='lclmn'>*Password:</td> <td class='rclmn'><input type='password' name='password' size='20'></td>
</tr><tr>
<td class='lclmn'>*Retype pass:</td> <td class='rclmn'><input type='password' name='retypepass' size='20'></td>
</tr><tr>
<td class='lclmn'>Email:</td> <td class='rclmn'><input type='text' name='email' size='20' value='user@domain.com'></td>
</tr><tr>
<td class='lclmn'>Country:</td> <td class='rclmn'><input type='text' name='country' size='20' value='Sweden'></td>
</tr><tr>
<td colspan='2'><input type='submit' name='submit_reg' value='[ register ]'></form>";
mk_btn("[ go back ]", "index.php");
echo "</td>
</tr>
";
} //Function echo_register
Posted: Sat Dec 27, 2003 6:34 am
by vigge89
notice the function echo_register, where it also echoes $error_msg

, btw, Nay's helping me out right now
Posted: Sat Dec 27, 2003 6:54 am
by Nay
I rewrote it. I never have luck with others scripts =\.
Code: Select all
<?php
require ("config.php");
if(isSet($_SESSION['online']) && $_SESSION['online'] == "yes") {
header("Location: index.php");
}
// no need to add "else {" since once header() is excetuted, php stops and redirects
if(isSet($_POST['submit_reg']) && !empty($_POST['submit_reg'])) {
$errorMsg = null;
if(empty($_POST['username']) || !isSet($_POST['username'])) {
$errorMsg .= "You must enter your username\n";
}
if(empty($_POST['password']) || !isSet($_POST['password'])) {
$errorMsg .= "You must enter a password";
}
if(empty($_POST['retypepass']) || !isSet($_POST['retypepass'])) {
$errorMsg .= "You must confirm your password";
}
if($_POST['password'] !== $_POST['retypepass']) {
$errorMsg .= "You password do not match";
}
if(!empty($errorMsg)) {
echo_header();
$errorMsg;
echo_footer();
exit(); // exits php, you won't need to use a else {
}
// done with validation
$query1 = "SELECT nick FROM keeper WHERE nick = '{$_POST['username']}'";
$result1 = @mysql_query($query1);
$count1 = @mysql_num_rows($result1);
if(empty($result1)) {
echo_header();
echo "Could not select from table becuase of:" . mysql_error();
echo_footer();
exit;
}
if($count1 > 1) {
echo_header();
echo "What the...? There's more than one user with the nick name {$_POST['username']}";
echo_footer();
exit;
}
if($count1 == 1) {
echo_header();
echo "Someone has already taken that name...";
echo_footer();
exit;
}
foreach ($_POST as $key => $value) {
$value = strip_tags($value);
$value = trim($value);
$value = addslashes($value);
$_POST[$key] = $value;
}
$query2 = "INSERT INTO keeper (name, email, pass, country) VALUES ('{$_POST['username']}', '{$_POST['email']}', '{$_POST['password']}', '{$_POST['country']}') LIMIT 1";
$result2 = @mysql_query($query2);
if(empty($result2)) {
echo_header();
echo "Error, can't insert into table because of:" . mysql_error();
echo_footer();
exit;
}
echo_header("<meta http-equiv="refresh" content="5;URL=start.php">");
echo <<< STUFF
<tr>
<td colspan='10'>
You have succesfully registrated!<br>
You will now be redirected to the start page, or<br>
<a href='start.php'>click here if you don't wish to wait.</a>
</td>
</tr>
STUFF;
echo_footer();
} else {
echo_header();
echo_register();
echo_footer();
}
function echo_register() { //Function echo_register
echo "
<tr>
<td colspan='2'>
<b>Register new account:</b><br>Fields with a star (*) is required, maxlenght for usernames are 12 characters & passwords 12.<br>
</td>
</tr><tr><form name='register' method='post' action='register.php'>
<td class='lclmn'>*Username:</td> <td class='rclmn'><input type='text' name='username' size='20'></td>
</tr><tr>
<td class='lclmn'>*Password:</td> <td class='rclmn'><input type='password' name='password' size='20'></td>
</tr><tr>
<td class='lclmn'>*Retype pass:</td> <td class='rclmn'><input type='password' name='retypepass' size='20'></td>
</tr><tr>
<td class='lclmn'>Email:</td> <td class='rclmn'><input type='text' name='email' size='20' value='user@domain.com'></td>
</tr><tr>
<td class='lclmn'>Country:</td> <td class='rclmn'><input type='text' name='country' size='20' value='Sweden'></td>
</tr><tr>
<td colspan='2'><input type='submit' name='submit_reg' value='[ register ]'></form>";
mk_btn("[ go back ]", "index.php");
echo "</td>
</tr>
";
}
?>
CTRL + F5 didn't work in Zend since I needed config.php <_<. Meh.
-Nay
Edit -> I did a CTRL + ALT + A. I wondered. Vigge, you can't do that with functions. $errormsg in a function won't work (I think). Well, Zend told me that 'use of variable before it was defined'. Maybe this is where OOP comes in

.
Posted: Sat Dec 27, 2003 7:01 am
by vigge89
mayb use echo_register($errormsg); then?
i'll try out your code soon

thanks!
Posted: Sat Dec 27, 2003 7:08 am
by Nay
Ah, there's an idea:
Code: Select all
<?php
function say($message = 'nothing') {
echo $message;
}
say(); // will print out "nothing" since an argument was not defined
say("heredoc rulessssssssssss"); // will print out.......well, you can guess it
?>
-Nay
Posted: Sat Dec 27, 2003 10:19 am
by vigge89
yeah, that's what i ment

Posted: Sat Dec 27, 2003 10:28 am
by Nay
nuh it wasn't
*brain-washes you*
-Nay
Posted: Sat Dec 27, 2003 10:49 am
by vigge89
Code: Select all
<?php
// also took away this (hehe)
?>
still won't work, no errors appear at all when something is wrong, and when it should process and add the user, nothing appears in the browser, and nothing is added in the database

Posted: Sat Dec 27, 2003 1:04 pm
by vigge89
anyone spotted anything wrong?
Posted: Sat Dec 27, 2003 1:21 pm
by vigge89
go it to work now, but when the user should be added, i get the "user could not be added: ".mysql_error();
There is nothing after "added: ", and the user isn't of course added, becuase of that $result2 is empty;
Code: Select all
<?php
$query2 = "INSERT INTO keeper (name, email, pass, country) VALUES ('{$_POST['username']}', '{$_POST['email']}', '{$_POST['password']}', '{$_POST['country']}') LIMIT 1";
$result2 = mysql_query($query2);
if(empty($result2)) {
echo_header();
echo_register("<br>* Couldn't add user:" . mysql_error());
echo_footer();
exit();
}
?>
Should i take away the "empty($result2)" and user "if ($result2 == "False")" or something?
Posted: Sat Dec 27, 2003 4:07 pm
by vigge89
got everything to work at last!
thanks everyone for helpting me with this

Posted: Sat Dec 27, 2003 10:41 pm
by Nay
So what
did you do anyway vigge?
-Nay
Posted: Sun Dec 28, 2003 3:57 am
by vigge89
Posted: Sun Dec 28, 2003 4:00 am
by Nay
rofl
Kidding

. I was killing myself over something that didn't worked yesterday:
Code: Select all
if($this = 1) {
// then do that...
}
took me a while to finaly realise that I needed 2 arrows in the if statement.
We always make mistakes.......dont we?
-Nay
or is it only me?
