[SOLVED]Clean Sweap - Registration page

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
vigge89
Forum Regular
Posts: 875
Joined: Wed Jul 30, 2003 3:29 am
Location: Sweden

[SOLVED]Clean Sweap - Registration page

Post 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?
Last edited by vigge89 on Sat Dec 27, 2003 4:09 pm, edited 3 times in total.
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post 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
User avatar
vigge89
Forum Regular
Posts: 875
Joined: Wed Jul 30, 2003 3:29 am
Location: Sweden

Post by vigge89 »

notice the function echo_register, where it also echoes $error_msg :roll: :wink: , btw, Nay's helping me out right now
Nay
Forum Regular
Posts: 951
Joined: Fri Jun 20, 2003 11:03 am
Location: Brisbane, Australia

Post 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 ;).
User avatar
vigge89
Forum Regular
Posts: 875
Joined: Wed Jul 30, 2003 3:29 am
Location: Sweden

Post by vigge89 »

mayb use echo_register($errormsg); then?

i'll try out your code soon :D
thanks!
Nay
Forum Regular
Posts: 951
Joined: Fri Jun 20, 2003 11:03 am
Location: Brisbane, Australia

Post 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
User avatar
vigge89
Forum Regular
Posts: 875
Joined: Wed Jul 30, 2003 3:29 am
Location: Sweden

Post by vigge89 »

yeah, that's what i ment ;)
Nay
Forum Regular
Posts: 951
Joined: Fri Jun 20, 2003 11:03 am
Location: Brisbane, Australia

Post by Nay »

nuh it wasn't ;) :lol:

*brain-washes you*

-Nay
User avatar
vigge89
Forum Regular
Posts: 875
Joined: Wed Jul 30, 2003 3:29 am
Location: Sweden

Post 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 :cry:
Last edited by vigge89 on Sat Dec 27, 2003 1:34 pm, edited 1 time in total.
User avatar
vigge89
Forum Regular
Posts: 875
Joined: Wed Jul 30, 2003 3:29 am
Location: Sweden

Post by vigge89 »

anyone spotted anything wrong?
User avatar
vigge89
Forum Regular
Posts: 875
Joined: Wed Jul 30, 2003 3:29 am
Location: Sweden

Post 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?
User avatar
vigge89
Forum Regular
Posts: 875
Joined: Wed Jul 30, 2003 3:29 am
Location: Sweden

Post by vigge89 »

got everything to work at last!
thanks everyone for helpting me with this :D
Nay
Forum Regular
Posts: 951
Joined: Fri Jun 20, 2003 11:03 am
Location: Brisbane, Australia

Post by Nay »

So what did you do anyway vigge? ;)

-Nay
User avatar
vigge89
Forum Regular
Posts: 875
Joined: Wed Jul 30, 2003 3:29 am
Location: Sweden

Post by vigge89 »

erm, if i answered that, everyone would be laughing at me :roll: :oops: :?
nah, but i forgot the name of the MySQL field, i thought it was name, when it was nick :P
Nay
Forum Regular
Posts: 951
Joined: Fri Jun 20, 2003 11:03 am
Location: Brisbane, Australia

Post by Nay »

rofl :lol:

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? 8O
Post Reply