//set host, username and password for MySQL 
$dbhost = "localhost"; 
$dbuser = "root"; 
$dbpass = "root"; 
//connect to MySQL or return an error 
$conn = mysql_connect("$dbhost", "$dbuser", "$dbpass") 
or die('Could not connect: ' . mysql_error()); 
//set database name 
$dbname = "database"; 
//select database or return an error 
$dbselect = mysql_select_db("$dbname")  
or die ('Could not select database');

  $privatekey = "PRIVATE KEY";
  $resp = recaptcha_check_answer ($privatekey,

  if (!$resp->is_valid) {
    // What happens when the CAPTCHA was entered incorrectly
    die ("The reCAPTCHA wasn't entered correctly. Go back and try it again." .
         "(reCAPTCHA said: " . $resp->error . ")");
  } else {
    // Your code here to handle a successful verification
//get username and password info from the form, protecting against SQL injection 
$pass = mysql_real_escape_string($_POST["pass"]); 
$confirm = mysql_real_escape_string($_POST["confirm"]); 
$user = mysql_real_escape_string($_POST["name"]); 
//validate user input 
if(!preg_match('/^[a-zA-Z0-9]{5,20}$/',$user)) { 
die ('Error: Usernames can only contain alphanumeric characters and must be between 5 and 20 characters in length.'); 
if(!preg_match('/^[a-zA-Z0-9]{5,20}$/',$pass)) { 
die ('Error: Passwords can only contain alphanumeric characters and must be between 5 and 20 characters in length.'); 
if($pass != $confirm) { 
die ('Error: Passwords do not match.'); 
//make sure user doesn't already exist and if it doesn't, add new record to the database 
$result = mysql_query("SELECT login FROM accounts WHERE login='$user'"); 
if(mysql_num_rows($result)>0) { 
die ('Error: Username already exists.');
mysql_query("INSERT INTO accounts (login, password, accesslevel) VALUES ('".$_POST['name']."', '".base64_encode(pack('H*', sha1($_POST['pass'])))."', 0)") 
or die ('Error: ' . mysql_error()); 
//report successful registration 
echo "Account created successfully."; 
//close MySQL connection 

Hello i have 1 problem!
I use this script to register members in my database!
But all Error messages and other messages are displayed on acc.php where this script is stored not on the page where registartion form is!

It looks kinda bad!

So again step by step!

1)I input all data in form which is located in reg.php
2)I click on Submit
3)I am redirected to acc.php where i have this messages from script : echo "Account created successfully.";

So how can i make my acc.php to show all messages on reg.php where my form is?

Member Avatar


redirect back to the reg.php page and then show the message.

A simple way of doing this would be with the querystring:


Use the $_GET global to create custom output depending on the value of msg.

Or, to hide the gubbins, you could create a $_SESSION in your app.php page, so that the var is available as soon as you return to reg.php. Check to see that there is a value in the session var, access it and then unset it immediately:

  $regmsg =  "<p>" . $_SESSION['regmsg'] . "</p>";

Use session or pass variable for message like 'http://url.com/msg=1', and fetch the message with $_GET and determine what the message refers. And show this message near your form. Example below:

$message = array("Invalid username","Invalid password","Invalid email","Account created successfully");

<title>Registration Form</title.
<form name="registrationform" method="post" action="register.php">
    <label for="name">Name:</label><input type="text" name="name" id="name" /><br />
    <label for="email">Email:</label><input type="text" name="email" id="email" /><br />
    <label for="password">Password:</label><input type="password" name="password" id="password" /><br />
    <input type="submit" value="Submit" name="register" />
if(isset($_GET['msg'])) {
        $msg = $message[$msg];
        echo "<p>$msg</p>";

    $name = $_POST['name'];
    $email = $_POST['email'];
    $password = $_POST['password'];

//your codes goes here.

//if something wrong
header("Location: index.php?msg=x"); //where x is variable for the error message that you want to return

Hope this help.

@ardav, you were a little earlier posted. :)

Member Avatar


Hah, I'm usually on the receiving end of simultaneous posts. :)