Skip to main content
Tweeted twitter.com/StackCodeReview/status/1325634296460226560
Changed the title to be more in line with code review guidelines.
Link
pacmaninbw
  • 26.2k
  • 13
  • 47
  • 114

Is there any open vulnerabilities in this Website user registration script? in PHP

Source Link

Is there any open vulnerabilities in this registration script?

I have a registration system, but I really don't know if it is safe, because I don't understand security very well and I'm afraid of compromising my clients' data, I believe that the methods I created are good for security, but I still have doubts if it is safe

I created several variables using encapsulation, and then used them, is that right? Or should I create them within the methods

Is regular expression enough to protect against JavaScript attacks?

Is there a vulnerability in my code or an error?

Can we consider that I have created efficient methods for the security of my clients?

<?php
/**
 * Signup
 */

class SignUp {

    private $email;
    private $password;
    private $name;
    private $sql;
    private $result;
    private $conn;
    private $remote_addr;
    private $http_user_agent;
    private $http_client_ip;
    private $http_x_forwarded_for;
    private $check_name;
    private $line;

    public function __construct($host, $dbname, $user, $pass) {
        try {
            $this->conn = new PDO("mysql:host=$host;dbname=$dbname", $user, $pass);
            $this->conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
            return $this->conn;
        }catch(PDOException $e) {
            throw new Exception($e->getMessage());
        }
    }


    public function setEmail($e) {
        $this->email = $e;
    }

    public function getEmail() {
        return $this->email;
    }

    public function setName($n) {
        $this->name = $n;
    }

    public function getName() {
        return $this->name;
    }

    public function setPassword($password, $confirm) {
        if ($password === $confirm) {
            $this->password = password_hash($password, PASSWORD_BCRYPT);
        } else {
            // Handle input error here
            echo "Password does not match!";
        }
    }

    public function getPasswordHash() {
        return $this->password;
    }


    // public function CheckHashes() {
    //     echo $this->password;
    // }

    public function CheckName(){
        $this->check_name = $this->conn->prepare("SELECT name FROM users WHERE name = :name");
        $this->check_name->execute(array(
            ":name" => $this->getName()
        ));

        $this->line = $this->check_name->rowCount();

        if($this->line != 0){
            $_SESSION['msg'] = '<p style="color:red;">Name already exists!</p>';
            header("Location: signup.php");
            exit();
        }
    }

    public function CheckEmail(){
        $checkmail = $this->conn->prepare("SELECT email FROM users WHERE email = :email");
        $checkmail->execute(array(
            ":email" => $this->getEmail()
        ));

        $theline = $checkmail->rowCount();

        if($theline != 0){
            $_SESSION['msg'] = '<p style="color:red;">Email already exists!</p>';
            header("Location: signup.php");
            exit();
        }
    }

    public function CheckFields(){
        if(empty($this->password) || empty($this->name) || empty($this->email)){
            $_SESSION['msg'] = '<p style="color:red;">Please, fill out all the fields!</p>';
            header("Location: signup.php");
            exit();
        }
    }

    public function ValidateNameLength(){
        if(strlen($this->getName()) < 10){
            $_SESSION['msg'] = '<p style="color:red;">Can only use 10 or more characters as name!</p>';
            header("Location: signup.php");
            exit();
        }

        if(strlen($this->getName()) > 25){
            $_SESSION['msg'] = '<p style="color:red;">Can only use a maximum of 25 characters as name!</p>';
            header("Location: signup.php");
            exit();
        }
    }

    public function ValidateEmail(){
        if(!filter_var($this->getEmail(), FILTER_VALIDATE_EMAIL)){
            $_SESSION['msg'] = '<p style="color:red;">Incorrect email!</p>';
            header("Location: signup.php");
            exit();
        }
    }
    
    public function Insert() {
        if (preg_match("/^[A-Za-z0-9\s]*$/", $this->getName()) ) {
                $this->sql = "INSERT INTO users (name, email, pass, ip_address, http_client_ip, http_x_forwarded_for, http_user_agent) VALUES(:name, :email, :pass, :ip_address, :http_client_ip, :http_x_forwarded_for, :http_user_agent)";
                $this->result = $this->conn->prepare($this->sql);
                $this->remote_addr = $_SERVER['REMOTE_ADDR'];
                $this->http_client_ip = $_SERVER['HTTP_CLIENT_IP'];
                $this->http_x_forwarded_for = $_SERVER['HTTP_X_FORWARDED_FOR'];
                $this->http_user_agent = $_SERVER['HTTP_USER_AGENT'];
                $this->result->execute(array(
                    ":name" => $this->getName(),
                    ":email" => $this->getEmail(),
                    ":pass" => $this->getPasswordHash(),
                    ":ip_address" => $this->remote_addr,
                    ":http_client_ip" => $this->http_client_ip,
                    ":http_x_forwarded_for" => $this->http_x_forwarded_for,
                    ":http_user_agent" => $this->http_user_agent
                ));

                if($this->result == true){
                    $_SESSION['msg'] = '<p style="color:green;">Registered successfully!</p>';
                    header("Location: signin.php");
                    exit();
                }
        }

        else {
            $_SESSION['msg'] = '<p style="color:green;">You can only use letters and numbers as name!</p>';
            header("Location: signup.php");
            exit();
        }

    }
}

$obj = new SignUp('localhost', 'dbname', 'user', 'password');
$obj->setEmail('[email protected]');
$obj->setName('Anne');
$obj->setPassword('password', 'password');
$obj->CheckName();
$obj->CheckEmail();
$obj->CheckFields();
$obj->ValidateNameLength();
$obj->ValidateEmail();
$obj->Insert();
// $obj->getEmail();
// echo $obj->CheckHashes();
// echo "\n";