Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8

Thread: register script

  1. #1
    Senior Coder rafiki's Avatar
    Join Date
    Aug 2006
    Location
    Floating around somewhere...
    Posts
    2,042
    Thanks
    19
    Thanked 42 Times in 42 Posts

    register script

    PHP Code:
    <?php
    $submit 
    $_GET['submit'];
    $name $_POST['name'];
    $sname $_POST['sname'];
    $email $_POST['email'];
    $username $_POST['username'];
    $password $_POST['password'];
    if (isset(
    $submit))
    {
    if (!isset(
    $username))
    {
    echo 
    "Please enter a username.";
    }
    if (!isset(
    $password))
    {
    echo 
    "Please enter a password.";
    }
    else {
    $count count($password);
    }
    if (!isset(
    $email))
    {
    echo 
    "Please enter your VALID email address.";
    }
    if (!isset(
    $name))
    {
    echo 
    "Please enter your name.";
    }
    if (!isset(
    $sname))
    {
    echo 
    "Please enter your last name.";

    if (
    $count 6)
    {
    echo 
    "Password too short.";
    }
    }
    $datahost localhost;
    $dataname databasename;
    $datapase databasepass;
    $datauser databaseuser;
    mysql_connect($datahost$datauser$datapass) or 
    die(
    mysql_error());
    mysql_select_db($dataname) or die(mysql_error());
    $check mysql_query("SELECT username FROM users WHERE 
    username='$username'"
    );  
    $user_exist mysql_num_rows($check); 
    if(
    $user_exist 0){ 
        echo 
    "I'm sorry but the username you specified has already 
    been taken.  Please pick another one."

        unset(
    $user);   
        exit;   
    }
    $query "INSERT INTO users (name, sname, email, username, 
    password, ) 
    VALUES('$name', '$sname', '$email', '$username', 
    '$password')"

    mysql_query($query) or die(mysql_error()); 
    mysql_close();
    echo 
    "You have successfully registered please click <a href 
    =\"/login.php\" >here</a> to log in"
    ;
    ?>
    <?php 
    if (!isset(submit))
    {
    include 
    'register.htm';
    }
    ?>
    would this script work i have no host to test
    p.s if someone has a little space for me to test i would appreciate
    for some reason lycos wont work, and cant find anouther one with instant sign up
    Last edited by rafiki; 03-05-2007 at 10:28 PM.

  • #2
    Regular Coder
    Join Date
    May 2004
    Posts
    144
    Thanks
    0
    Thanked 0 Times in 0 Posts
    instead of:
    PHP Code:
    $submit $_GET['submit']; 
    try
    PHP Code:
    $submit $_POST['submit'

  • #3
    Senior Coder Nightfire's Avatar
    Join Date
    Jun 2002
    Posts
    4,265
    Thanks
    6
    Thanked 48 Times in 48 Posts
    It'll work, but if the form fields are empty, you're not stopping the script from querying the db. It'll do the query if they're empty or filled and will result in an error. You need to set up a flag (I think that's the right term), so if an error is shown, it increases a variables value. Then before the query, check the variable is what it started off as and not been incremented by the errors

    Also, just by scanning it quickly, at the bottom you have missed off a $

    if (!isset($submit))

  • #4
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,642
    Thanks
    2
    Thanked 405 Times in 397 Posts
    Use empty() instead of isset(). isset() will always be true since you specifically the variables earlier in the script. The manual has some examples and comparisons between the two.

  • #5
    Senior Coder rafiki's Avatar
    Join Date
    Aug 2006
    Location
    Floating around somewhere...
    Posts
    2,042
    Thanks
    19
    Thanked 42 Times in 42 Posts
    so somehting like
    if (empty($email))
    {
    die(Please Enter Email Address.);
    }
    ?

  • #6
    Senior Coder Nightfire's Avatar
    Join Date
    Jun 2002
    Posts
    4,265
    Thanks
    6
    Thanked 48 Times in 48 Posts
    If you use die() then your script won't continue loading and will invalidate your html page as it'll not finish loading.

    I'd do something a bit like this
    PHP Code:
    $errormessages = array("No username entered","No password entered","etc");
    $error '';
    if(empty(
    $username)){
      
    $error .= $errormessages[0];
    }
    if(empty(
    $password)){
      
    $error .= $errormessages[1];
    }

    // done all checking
    if($error == ''){
      
    // do db query etc here
    }else{
      echo 
    $error;

    Untested and will need tweaking. I've only just woke up so just wanted to knock something quick up as an example

  • #7
    Senior Coder rafiki's Avatar
    Join Date
    Aug 2006
    Location
    Floating around somewhere...
    Posts
    2,042
    Thanks
    19
    Thanked 42 Times in 42 Posts
    nice night, thanks
    could i use 2 error checks on same line for the password field

    PHP Code:
    $errormessages = array("No username entered","No password entered or password to short","etc");
    if (empty(
    $password) || $count 6){
    $error .= $errormessages[1]

    Last edited by rafiki; 03-06-2007 at 01:01 PM.

  • #8
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,642
    Thanks
    2
    Thanked 405 Times in 397 Posts
    Yes, but you need a colon after your $error line.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •