Reply to thread

I know this may be more of a learning experience project type of thing, but I would highly recommend using a pre-existing templating library. I quite like Twig.


Some reasoning and other tips:

  • Twig is pretty fast, flexible in the sense it can be extended, and most importantly it is secure (provides you with methods to escape output for different contexts, in-fact it actually default escapes output for the HTML context, so in 90% of cases you do not have to worry about XSS).
  • Highly consider using something like composer. It will make your life a whole lot easier in the long run, and your code much cleaner. For example, rather than manually including each of your required files at the top, you could use an autoloader.
  • Also look into the concept of dependency injection. This allows you to essentially inject all of your services (User) into a container which you can use anywhere, rather than having to instantiate a new object of it everywhere you need to use it. That also makes unit testing much easier, should you choose to learn more about that (recommended, essentially confirms your code is behaving the way you expect it to).
  • I noticed you called $user->newUser() without validating any of the user submitted info, perhaps you should add another method under the User class to validate each of the inputs are valid (type, length, and semantically correct - e.g. validate e-mail's using filter_var($email, FILTER_VALIDATE_EMAIL).
  • After adding a new user ($user->newUser()), you sender a Location header to redirect. You should call exit/die() after redirections to make sure the rest of the script does not execute.
  • Not required, but you might also want to research type hinting, here's a link: PHP type hinting.
  • Again, not required, but I'd recommend following what's sometimes referred to as short circuiting for cleaner code. e.g. your code

[CODE]

if(isset($_POST["submit"])) {

    if(!empty($_POST["email"]) && !empty($_POST["username"]) && !empty($_POST["password"]) && !empty($_POST["verify"])) {

//assign user input to variables

$email = $_POST["email"];

$username = $_POST["username"];

$password = $_POST["password"];

$verify = $_POST["verify"];

//creates new user with username, password and email

if($user->newUser($username, $password, $email)) {

header("location: index.php");

}

} else {

$message = "Whoops, something went wrong! Please check all of the fields, and try again.";

}

}

[/CODE]

could be rewritten as:

[CODE]

if (isset($_POST["submit"])) {

    if (empty($_POST["email"]) || empty($_POST["username"]) || empty($_POST["password"]) || empty($_POST["verify"])) {

        $message = "Whoops, something went wrong! Please check all of the fields, and try again.";

        // return a response early here.

    }

    //assign user input to variables

    $email = $_POST["email"];

    $username = $_POST["username"];

    $password = $_POST["password"];

    $verify = $_POST["verify"];

    //creates new user with username, password and email

    if ($user->newUser($username, $password, $email)) {

        header("location: index.php");

    }

}

[/CODE]


Hopefully some of this helped, and good luck :)


Top