Page 1 of 1

everything is fine until everything is right...

Posted: Thu Sep 24, 2009 10:23 am
by ceorlskep
pickle | Please use [ code=php ], [ code=text ], etc tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: :arrow: Posting Code in the Forums to learn how to do it too.


Im trying to create a register page for my web site, coded it to the best of my ability and below is what I got. If info is missing, I get the missing info error, if the user name is taken, I get the user name taken error, and if the passwords do not match, I get that error. Unfortunately, if everything is right, I get a black page! Any help would be greatly appreciated!

Code: Select all

<?php
    
    session_start();
    require("config.php");
    require("db.php");
    
    if($_POST['submit'])
    {
        if(empty($_POST['username']) ||
            empty($_POST['username']) ||
            empty($_POST['username'])){
            header("Location: " . $config_basedir . "register.php?error=1");
            exit;
        }
        
        $_SESSION['username'] = $_POST['username'];
        
        if($_POST['password1'] == $_POST['password2'])
        {
            $checksql = "SELECT * FROM logins WHERE username = '" . $_POST['username'] . "';";
            $checkresult = mysql_query($checksql) or die(mysql_error());
            $checknumrows = mysql_num_rows($checkresult) or die(mysql_error());
            
            if($checknumrows != 1)
            {
                header("Location: " . $config_basedir . "cust-address.php");
                exit;
            }else{
                header("Location: " . $config_basedir . "register.php?error=taken");
                exit;
            }
        }else{
            header("Location: " . $config_basedir . "register.php?error=2");
            exit;
        }
    }
    
    require("header.php");
    
    if(($_GET['error']) == 1){
        echo "<strong>Please fill in the missing information from the form</strong>";
    }elseif(($_GET['error']) == 'taken'){
        echo "<strong>User name already taken</strong>";
    }elseif(($_GET['error']) == 2){
        echo "<strong>Passwords do not match</strong>";
    }
?>
    <h2>Register</h2>
    <form action="<?php echo $SCRIPT_NAME ?>" method="post">
    <table border="1" cellpadding="5">
        <tr>
            <td><strong>User Name</strong></td>
            <td><input type="text" name="username" value="<?php echo $_SESSION['username'] ?>"></td>
            <td><strong>Password</strong></td>
            <td><input type="password" name="password1"></td>
        </tr>
        <tr>
                        <td colspan = "2"></td>
            <td><strong>confirm password</strong></td>
            <td><input type="password" name="password2"></td>
        </tr>
                <tr>
                <td colspan="4"><div align="center"><input type="submit" name="submit" value="Register"></div></td>
                </tr>
    </table>
    </form>
<?php
    require("footer.php");
?>

pickle | Please use [ code=php ], [ code=text ], etc tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: :arrow: Posting Code in the Forums to learn how to do it too.

Re: everything is fine until everything is right...

Posted: Thu Sep 24, 2009 11:38 am
by MichaelR
On line 36 you are closing the "if ($_POST['submit'])". This comes before you output the HTML. So, there is a blank page. Based on how you've done it, you should replace line 36 with ... header('Location: register.php?success=true'); } ... and then replace line 46 with ... } elseif ($_GET['success']) { echo "Success!"; }

Code: Select all

<?php
 
session_start();
require("config.php");
require("db.php");
 
if($_POST['submit'])
{
if(empty($_POST['username']) ||
empty($_POST['username']) ||
empty($_POST['username'])){
header("Location: " . $config_basedir . "register.php?error=1");
exit;
}
 
$_SESSION['username'] = $_POST['username'];
 
if($_POST['password1'] == $_POST['password2'])
{
$checksql = "SELECT * FROM logins WHERE username = '" . $_POST['username'] . "';";
$checkresult = mysql_query($checksql) or die(mysql_error());
$checknumrows = mysql_num_rows($checkresult) or die(mysql_error());
 
if($checknumrows != 1)
{
header("Location: " . $config_basedir . "cust-address.php");
exit;
}else{
header("Location: " . $config_basedir . "register.php?error=taken");
exit;
}
}else{
header("Location: " . $config_basedir . "register.php?error=2");
exit;
}
}
 
require("header.php");
 
if(($_GET['error']) == 1){
echo "<strong>Please fill in the missing information from the form</strong>";
}elseif(($_GET['error']) == 'taken'){
echo "<strong>User name already taken</strong>";
}elseif(($_GET['error']) == 2){
echo "<strong>Passwords do not match</strong>";
}
?>
<h2>Register</h2>
<form action="<?php echo $SCRIPT_NAME ?>" method="post">
<table border="1" cellpadding="5">
<tr>
<td><strong>User Name</strong></td>
<td><input type="text" name="username" value="<?php echo $_SESSION['username'] ?>"></td>
<td><strong>Password</strong></td>
<td><input type="password" name="password1"></td>
</tr>
<tr>
<td colspan = "2"></td>
<td><strong>confirm password</strong></td>
<td><input type="password" name="password2"></td>
</tr>
<tr>
<td colspan="4"><div align="center"><input type="submit" name="submit" value="Register"></div></td>
</tr>
</table>
</form>
<?php
require("footer.php");
?>

Re: everything is fine until everything is right...

Posted: Thu Sep 24, 2009 11:47 am
by pickle
The Coding Critique forum is intended for finished, working code that you want others to critique in the interest of improving it.

If you need help getting your code to work, the appropriate forum is PHP - Code. I've moved this thread there.

Re: everything is fine until everything is right...

Posted: Thu Sep 24, 2009 9:11 pm
by ceorlskep
First off - to pickle - sorry, and thank you for replacing it to the correct forum.

Secondly, tried the suggested changes with no luck, I still get:
pass mismatch= password error, retry
username taken= user name taken, retry
missing info= missing info error, retry
everything correct= black page (previous error code if any in address bar)