If you're gonna use the header-location function to redirect, you'll need to finish the error checking prior to echoing any html, and instead store the values in the var $error for print AFTER the eventual header()-sentence.
<?php
## Functions ##
function Random_Password($length) {
srand(date("s"));
$possible_charactors = "ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
$string = "";
while(strlen($string)<$length) {
$string .= substr($possible_charactors,(rand()%(strlen($possible_charactors))),1);
}
return($string);
}
if ($submit) {
// Strip invalid Characters(spaces ect..)
$uname = str_replace(" ", "", $uname);
$address= str_replace(" ", "", $address);
$uname = trim ($uname);
$address = trim ($address);
// Make sure the username and email or not blank
if(strlen($uname) == 0) {
$error = $error . "<br><p><font color=red>Username cannot be blank!</font></p>";
}
if(strlen($address) == 0) {
$error = $error . "<br><p><font color=red>Email address cannot be blank!</font></p>";
}
// Connect to the database
$db = mysql_connect("localhost", "root");
mysql_select_db("basmentIM",$db);
// Check for Account existence
$sql = "SELECT Count(*) FROM Accounts WHERE Username='$uname' OR Email='$address'";
$result = mysql_query($sql);
if ($result != 0) {
$error = $error . "<br><p><font color=red>Account username/email already exists!</font></p>";
}
if(empty($error) {
// Generate Password
$newpass=Random_Password(8);
// Insert New Record
$sql = "INSERT INTO Accounts (Username, Password, Email, Active) VALUES ('$uname', '$newpass','$address',0)";
$result = mysql_query($sql);
// redirect
header("Location:success.php");
} else {
// Failed!
echo $error;
}
}
?>
NB! Untested code, must be checked before exec!
knutm 😉