0

I have been trying to follow a guide for making a PHP server app and I am getting a few syntax errors.

Error message:

Parse error: syntax error, unexpected '?' in 
D:\inetpub\wwwroot\cmpswoo1\CMPPROJ_Web\ServerApp\api2\db_functions2.php on line 134 

So I know were the error is, I just don't know how to fix it

<?php
class db_functions2 {
private $db;
//put your code here
// constructor
function __construct() {
    require_once 'db_connect2.php';
    // connecting to database
    $this->db = new db_connect2();
    $this->db->connect();
}
// destructor
function __destruct() {
}
/**
 * Random string which is sent by mail to reset password
 */
public function random_string()
{
$character_set_array = array();
$character_set_array[] = array('count' => 7, 'characters' => 'abcdefghijklmnopqrstuvwxyz');
$character_set_array[] = array('count' => 1, 'characters' => '0123456789');
$temp_array = array();
foreach ($character_set_array as $character_set) {
    for ($i = 0; $i < $character_set['count']; $i++) {
        $temp_array[] = $character_set['characters'][rand(0,  strlen($character_set['characters']) - 1)];
    }
 }
 shuffle($temp_array);
 return implode('', $temp_array);
 }
 public function forgotPassword($forgotpassword, $newpassword, $salt){
 $result = mysql_query("UPDATE `Users` SET `encryptedPassword` = '$newpassword',`salt` = '$salt'
          WHERE `email` = '$forgotpassword'");
 if ($result) {
 return true;
 }
 else
 {
 return false;
 }
 }
 /**
 * Adding new user to mysql database
 * returns user details
 */
 public function storeUser($FirstName, $LastName, $DOB, $email, $Username, $Password) {
    $uuid = uniqid('', true);
    $hash = $this->hashSSHA($Password);
    $encrypted_password = $hash["encrypted"]; // encrypted password
    $salt = $hash["salt"]; // salt
    $result = mysql_query("INSERT INTO Users(unique_id, FirstName, LastName, email, DOB, Username, encryptedPassword, salt, created_at) VALUES('$uuid', '$FirstName', '$LastName', '$email', '$DOB', '$Username', '$encryptedPassword', '$salt', NOW())");
    // check for successful store
    if ($result) {
        // get user details
        $uid = mysql_insert_id(); // last inserted id
        $result = mysql_query("SELECT * FROM Users WHERE uid = $id");
        // return user details
        return mysql_fetch_array($result);
    } else {
        return false;
    }
}
 /**
 * Verifies user by username and password
 */
 public function getUserByUsernameAndPassword($Username, $Password) {
    $result = mysql_query("SELECT * FROM Users WHERE usernameE = '$Username'") or die(mysql_error());
    // check for result
    $no_of_rows = mysql_num_rows($result);
    if ($no_of_rows > 0) {
        $result = mysql_fetch_array($result);
        $salt = $result['salt'];
        $encrypted_password = $result['encryptedPassword'];
        $hash = $this->checkhashSSHA($salt, $Password);
        // check for password equality
        if ($encryptedPassword == $hash) {
            // user authentication details are correct
            return $result;
        }
    } else {
        // user not found
        return false;
    }
 }
 /**
 * Checks whether the username is valid or fake
 */
 public function validUsername($Username)
 {
 $isValid = true;
 $atIndex = strrpos($Username, "@");
 if (is_bool($atIndex) && !$atIndex)
 {
  $isValid = false;
 }
 else
 {
  $domain = substr($Username, $atIndex+1);
  $local = substr($Username, 0, $atIndex);
  $localLen = strlen($local);
  $domainLen = strlen($domain);
  if ($localLen < 1 || $localLen > 64)
  {
     // local part length exceeded
     $isValid = false;
  }
  else if ($domainLen < 1 || $domainLen > 255)
  {
     // domain part length exceeded
     $isValid = false;
  }
  else if ($local[0] == '.' || $local[$localLen-1] == '.')
  {
     // local part starts or ends with '.'
     $isValid = false;
  }
  else if (preg_match('/\.\./', $local))
  {
     // local part has two consecutive dots
     $isValid = false;
  }
  else if (!preg_match('/^[A-Za-z0-9\-\.]+$/', $domain))
  {
     // character not valid in domain part
     $isValid = false;
  }
  else if (preg_match('/\.\./', $domain))
  {
     // domain part has two consecutive dots
     $isValid = false;
  }
   else if
     (!preg_match('/^(\\.|[A-Za-z0-9!#%&`_=\/$'*+?^{}|~.-])+$/',str_replace("\\","",$local)))
  {
     // character not valid in local part unless
     // local part is quoted
     if (!preg_match('/^"(\\"|[^"])+"$/',
         str_replace("\\","",$local)))
     {
        $isValid = false;
     }
  }
  if ($isValid && !(checkdnsrr($domain,"MX") ||checkdnsrr($domain,"A")))
  {
     // domain not found in DNS
     $isValid = false;
  }
 }
 return $isValid;
 }
 /**
 * Check user is existed or not
 */
 public function isUserExisted($Username) {
    $result = mysql_query("SELECT Username from Users WHERE Username = '$Username'");
    $no_of_rows = mysql_num_rows($result);
    if ($no_of_rows > 0) {
        // user existed
        return true;
    } else {
        // user not existed
        return false;
    }
 }
 /**
 * Encrypting password
 * returns salt and encrypted password
 */
 public function hashSSHA($Password) {
    $salt = sha1(rand());
    $salt = substr($salt, 0, 10);
    $encrypted = base64_encode(sha1($Password . $salt, true) . $salt);
    $hash = array("salt" => $salt, "encrypted" => $encrypted);
    return $hash;
 }
 /**
 * Decrypting password
 * returns hash string
 */
 public function checkhashSSHA($salt, $Password) {
    $hash = base64_encode(sha1($Password . $salt, true) . $salt);
    return $hash;
 }
 }

 ?>
2

2 Answers 2

1

Here single quote is causing problem:

So, change

(!preg_match('/^(\\.|[A-Za-z0-9!#%&`_=\/$'*+?^{}|~.-])+$/',str_replace("\\","",$local)))

To:

(!preg_match('/^(\\.|[A-Za-z0-9!#%&`_=\/$\'*+?^{}|~.-])+$/',str_replace("\\","",$local)))
Sign up to request clarification or add additional context in comments.

Comments

1

The regular expression string is opened with ', but also contains a '. Escape it with \. The error should be gone.

 (!preg_match('/^(\\.|[A-Za-z0-9!#%&`_=\/$'*+?^{}|~.-])+$/',str_replace("\\","",$local)))

Should be

 (!preg_match('/^(\\.|[A-Za-z0-9!#%&`_=\/$\'*+?^{}|~.-])+$/',str_replace("\\","",$local)))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.