Skip to content
View in the app

A better way to browse. Learn more.

Web Designer Forum

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

PHP/mysql Help

Featured Replies

Hi guys, getting the following

 

Parse error: syntax error, unexpected '}' in /home/mydomain/blah/reg.php on line 137

 

I'm using the sticky'd contact form on this site and am trying to add to a DB rather then generate an e-mail. I've commented out the bits that are giving me trouble elsewhere but not sure why I'm getting this error. Been coding various things all day so a bit code-blind. Anyone care to shed some light?

 

Cheers,

 

Chris

 

<?php
/*
------------------------------------------------------------------------
***FILE DESCRIPTION AND HISTORY***
------------------------------------------------------------------------
**DESCRIPTION**
*The purpose of register.php is to act as a controller between the
*database and the registration field. We first have to check if the
*username or e-mail address already exists in the database before
*validing our input to sure our data is valid as well as performing
*some crucial security procedures to protect our database.
*
**HISTORY**
*08 NOVEMBER 2010*
*First attempt at file. Included checks to ensure passwords and e-mails
*match and that no field is left blank as well as some bot checking and
*exploit blocking.
*09 NOVEMBER 2010*
*Revisited file to check user input sizes to not exceed column sizes 
*with the exception of password as our hash will ensure a size of 32.
*10 NOVEMBER 2010*
*Added queries to check database for existing usernames or e-mail 
*addresses to ensure to duplicates are not added to table
*/
error_reporting(E_ALL);
//Check if the form was submitted or not
if (!isset ($_POST['send'])) {
 header("Location:http://wwww.iamcg.co.uk/register/index.php");
}
//BOTS TO BLOCK
$bots = "/(Indy|Blaiz|Java|libwww-perl|Python|OutfoxBot|User-Agent|PycURL|AlphaServer|T8Abot|Syntryx|WinHttp|WebBandit|nicebot)/i";
//EXPLOITS TO BLOCK
$known_exploits = "/(content-type|bcc:|cc:|javascript|onclick|document.cookie|onload)/i";
//Check if known bot is visiting
if (preg_match($bots, $_SERVER["HTTP_USER_AGENT"])) {
 exit ("Sorry bots are not allowed here!");
}
/*
------------------------------------------------------------------------
CREATE INPUT FILTER FUNCTION
------------------------------------------------------------------------
*/
function mss($string) {
 return addslashes(trim(strip_tags(rawurldecode($string))));
}
//END OF INPUT FILTERING FUNCTION
//OUR MAIN PHP VARIABLES
$userName = (isset ($_POST['userName'])) ? mss($_POST['userName']) : FALSE;
$firstName = (isset ($_POST['firstName'])) ? mss($_POST['firstName']) : FALSE;
$lastName = (isset ($_POST['lastName'])) ? mss($_POST['lastName']) : FALSE;
$email1 = (isset ($_POST['email1'])) ? mss($_POST['email1']) : FALSE;
$email2 = (isset ($_POST['email2'])) ? mss($_POST['email2']) : FALSE;
$passWord1 = (isset ($_POST['passWord1'])) ? mss($_POST['passWord1']) : FALSE;
$passWord2 = (isset ($_POST['passWord2'])) ? mss($_POST['passWord2']) : FALSE;
$email_check = "/^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,6}$/i";
$ip = (isset ($_SERVER['REMOTE_ADDR'])) ? mss($_SERVER['REMOTE_ADDR']) : FALSE;
//Error array
$errors = array();
//END OF VARIABLE CREATION
/*
CHECK EXISTING DATA
*/
/*$con = mysql_connect("******************", "************", "****************") or die(mysql_error());
 mysql_select_db("mydb", $con) or die(mysql_error());
$result1 = mysql_num_rows (mysql_query ("SELECT * FROM users WHERE username = '$userName'"));
$result2 = mysql_num_rows (mysql_query ("SELECT * FROM users WHERE email = '$email1'"));
$result3 = mysql_num_rows (mysql_query ("SELECT * FROM users WHERE email = '$email2'"));    
mysql_close($con)*/
/*
INPUT VALIDATION STARTS
*/
if (!$userName) {
 $errors[] = "Please enter your username";
}
if ($result1 > 0) {
$errors[] = "Username already in use, please choose another one.";
}
if (!$firstName) {
 $errors[] = "Please enter your first name";
}
if (!$lastName) {
 $errors[] = "Please enter your last name";
}
if (!$passWord1) {
 $errors[] = "Please enter your password";
}
if (!$passWord2) {
 $errors[] = "Please confirm your password";
}
if(strlen($passWord1) < 8 || strlen($passWord2) < {
$errors[] = "Your passwords must be at least 8 characters";
}	
if ($passWord1 != $passWord2) {
 $errors[] = "Your passwords must match";
} 
if (!$email1) {
 $errors[] = "Please enter your email address";
}
if (!$email2) {
 $errors[] = "Please confirm your email address";
}
if ($result2 > 0 || $result3 > 0) {
$errors[] = "E-mail address already in use, please choose another one.";
}
if ($email1 != $email2) {
 $errors[] = "Your e-mail addresses must match";
} 
else if ($email1) {
   if (!preg_match($email_check, $email1)) {
     $errors[] = "You must enter a valid email address";
   }
 }
else if ($email2) {
   if (!preg_match($email_check, $email2)) {
     $errors[] = "You must enter a valid email address";
   }
 }
if ($email1 != $email2) {
 $errors[] = "Your e-mail addresses must match";
}  

//Check if any errors exist if they do display the errors
 if (count($errors) > 0) {
   foreach ($errors as $error) {
     echo "• $error<br />";
   }
 }
 else {
//Add stuff to database
 $passWordFinal = md5($_POST["passWord1"]);
 $con1 = mysql_connect("***********", "*****", "*******") or die(mysql_error());
 mysql_select_db("mydb", $con1) or die(mysql_error());
$sql = mysql_query("INSERT INTO users (username, password, firstname, surname, email, ip) VALUES ('$userName','$passWordFinal','$firstName','$lastName','$email','$ip')") or die(mysql_error());
echo $sql;
mysql_close($con1)
}
/*if (!mysql_query($sql,$con1))
 {
 die('Error: ' . mysql_error());
 }*/
// mysql_close($con1)
/*
-----------------------------------------------------------------
Loop through post array
-------------------------------------
*/
//   foreach ($_POST as $key => $value) {

/*
-----------------------------------------------------------------
Exploit checking & blocking
-------------------------------------
*/
 //    if (preg_match($known_exploits, $value)) {
   //    $value = preg_replace($known_exploits, "", $value);
  //   }



//header("Location:http://wwww.mydomain......");

?>

Hi guys, getting the following

 

Parse error: syntax error, unexpected '}' in /home/mydomain/blah/reg.php on line 137

 

I'm using the sticky'd contact form on this site and am trying to add to a DB rather then generate an e-mail. I've commented out the bits that are giving me trouble elsewhere but not sure why I'm getting this error. Been coding various things all day so a bit code-blind. Anyone care to shed some light?

 

Cheers,

 

Chris

 

<?php
/*
------------------------------------------------------------------------
***FILE DESCRIPTION AND HISTORY***
------------------------------------------------------------------------
**DESCRIPTION**
*The purpose of register.php is to act as a controller between the
*database and the registration field. We first have to check if the
*username or e-mail address already exists in the database before
*validing our input to sure our data is valid as well as performing
*some crucial security procedures to protect our database.
*
**HISTORY**
*08 NOVEMBER 2010*
*First attempt at file. Included checks to ensure passwords and e-mails
*match and that no field is left blank as well as some bot checking and
*exploit blocking.
*09 NOVEMBER 2010*
*Revisited file to check user input sizes to not exceed column sizes 
*with the exception of password as our hash will ensure a size of 32.
*10 NOVEMBER 2010*
*Added queries to check database for existing usernames or e-mail 
*addresses to ensure to duplicates are not added to table
*/
error_reporting(E_ALL);
//Check if the form was submitted or not
if (!isset ($_POST['send'])) {
 header("Location:http://wwww.iamcg.co.uk/register/index.php");
}
//BOTS TO BLOCK
$bots = "/(Indy|Blaiz|Java|libwww-perl|Python|OutfoxBot|User-Agent|PycURL|AlphaServer|T8Abot|Syntryx|WinHttp|WebBandit|nicebot)/i";
//EXPLOITS TO BLOCK
$known_exploits = "/(content-type|bcc:|cc:|javascript|onclick|document.cookie|onload)/i";
//Check if known bot is visiting
if (preg_match($bots, $_SERVER["HTTP_USER_AGENT"])) {
 exit ("Sorry bots are not allowed here!");
}
/*
------------------------------------------------------------------------
CREATE INPUT FILTER FUNCTION
------------------------------------------------------------------------
*/
function mss($string) {
 return addslashes(trim(strip_tags(rawurldecode($string))));
}
//END OF INPUT FILTERING FUNCTION
//OUR MAIN PHP VARIABLES
$userName = (isset ($_POST['userName'])) ? mss($_POST['userName']) : FALSE;
$firstName = (isset ($_POST['firstName'])) ? mss($_POST['firstName']) : FALSE;
$lastName = (isset ($_POST['lastName'])) ? mss($_POST['lastName']) : FALSE;
$email1 = (isset ($_POST['email1'])) ? mss($_POST['email1']) : FALSE;
$email2 = (isset ($_POST['email2'])) ? mss($_POST['email2']) : FALSE;
$passWord1 = (isset ($_POST['passWord1'])) ? mss($_POST['passWord1']) : FALSE;
$passWord2 = (isset ($_POST['passWord2'])) ? mss($_POST['passWord2']) : FALSE;
$email_check = "/^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,6}$/i";
$ip = (isset ($_SERVER['REMOTE_ADDR'])) ? mss($_SERVER['REMOTE_ADDR']) : FALSE;
//Error array
$errors = array();
//END OF VARIABLE CREATION
/*
CHECK EXISTING DATA
*/
/*$con = mysql_connect("******************", "************", "****************") or die(mysql_error());
 mysql_select_db("mydb", $con) or die(mysql_error());
$result1 = mysql_num_rows (mysql_query ("SELECT * FROM users WHERE username = '$userName'"));
$result2 = mysql_num_rows (mysql_query ("SELECT * FROM users WHERE email = '$email1'"));
$result3 = mysql_num_rows (mysql_query ("SELECT * FROM users WHERE email = '$email2'"));    
mysql_close($con)*/
/*
INPUT VALIDATION STARTS
*/
if (!$userName) {
 $errors[] = "Please enter your username";
}
if ($result1 > 0) {
$errors[] = "Username already in use, please choose another one.";
}
if (!$firstName) {
 $errors[] = "Please enter your first name";
}
if (!$lastName) {
 $errors[] = "Please enter your last name";
}
if (!$passWord1) {
 $errors[] = "Please enter your password";
}
if (!$passWord2) {
 $errors[] = "Please confirm your password";
}
if(strlen($passWord1) < 8 || strlen($passWord2) < {
$errors[] = "Your passwords must be at least 8 characters";
}	
if ($passWord1 != $passWord2) {
 $errors[] = "Your passwords must match";
} 
if (!$email1) {
 $errors[] = "Please enter your email address";
}
if (!$email2) {
 $errors[] = "Please confirm your email address";
}
if ($result2 > 0 || $result3 > 0) {
$errors[] = "E-mail address already in use, please choose another one.";
}
if ($email1 != $email2) {
 $errors[] = "Your e-mail addresses must match";
} 
else if ($email1) {
   if (!preg_match($email_check, $email1)) {
     $errors[] = "You must enter a valid email address";
   }
 }
else if ($email2) {
   if (!preg_match($email_check, $email2)) {
     $errors[] = "You must enter a valid email address";
   }
 }
if ($email1 != $email2) {
 $errors[] = "Your e-mail addresses must match";
}  

//Check if any errors exist if they do display the errors
 if (count($errors) > 0) {
   foreach ($errors as $error) {
     echo "• $error<br />";
   }
 }
 else {
//Add stuff to database
 $passWordFinal = md5($_POST["passWord1"]);
 $con1 = mysql_connect("***********", "*****", "*******") or die(mysql_error());
 mysql_select_db("mydb", $con1) or die(mysql_error());
$sql = mysql_query("INSERT INTO users (username, password, firstname, surname, email, ip) VALUES ('$userName','$passWordFinal','$firstName','$lastName','$email','$ip')") or die(mysql_error());
echo $sql;
mysql_close($con1)
}
/*if (!mysql_query($sql,$con1))
 {
 die('Error: ' . mysql_error());
 }*/
// mysql_close($con1)
/*
-----------------------------------------------------------------
Loop through post array
-------------------------------------
*/
//   foreach ($_POST as $key => $value) {

/*
-----------------------------------------------------------------
Exploit checking & blocking
-------------------------------------
*/
 //    if (preg_match($known_exploits, $value)) {
   //    $value = preg_replace($known_exploits, "", $value);
  //   }



//header("Location:http://wwww.mydomain......");

?>

 

This line

mysql_close($con1)

should be

mysql_close($con1);

  • Author

This line

mysql_close($con1)

should be

mysql_close($con1);

 

 

Thanks for the response. What is the difference?

 

EDIT: See it, code blind indeed!

 

EDIT AGAIN: How rude of me, I've +1'd you now!

Edited by chrissyg

  • Author

Also, any idea how I can return the error messages to the original form?

 

I've done it ages ago but it's snaring me at the moment!

Also, any idea how I can return the error messages to the original form?

 

I've done it ages ago but it's snaring me at the moment!

 

Yes

add 2 variables to the top of ur page

$error_tri = FALSE;
$success = FALSE;

 

add $success = TRUE; inside the else statement after the error check like this

 

 else {
//SET SUCCESS TO TRUE
$success = TRUE;
//Add stuff to database
 $passWordFinal = md5($_POST["passWord1"]);
 $con1 = mysql_connect("***********", "*****", "*******") or die(mysql_error());
 mysql_select_db("mydb", $con1) or die(mysql_error());
$sql = mysql_query("INSERT INTO users (username, password, firstname, surname, email, ip) VALUES ('$userName','$passWordFinal','$firstName','$lastName','$email','$ip')") or die(mysql_error());
echo $sql;
mysql_close($con1)

 

then change this bit

  
if (count($errors) > 0) {
   foreach ($errors as $error) {
     echo "• $error<br />";
   }
 }

 

To

 

 if (count($errors) > 0) {
  $error_tri = TRUE;
 }

 

Then where ever u want ur errors to appear do this

 

if($error_tri === TRUE && $success === FALSE;){
foreach ($errors as $error) {
     echo "• $error<br />";
   }
}

  • Author

Thanks a lot.

 

Looks like the way you mentioned is handling the input on the same page as the form.

 

Is there a way of doing this if we're using an external handler? Session variables for instance?

Create an account or sign in to comment

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.