I've started learning PHP for university-related projects today. I tried implementing a basic blog/forum. Here's the full source code.
It has the following features/design-decisions:
- There are posts, which have the following fields: title, author, date and contents.
- Posts have comments, which have the following fields: author, date, contents.
- There is a basic login system: users can register/login with an username and password.
- JSON files are used for data storage instead of a MySQL database.
- There is a JSON file for posts,
posts.json
, which contains posts and comments. - There is a JSON file for users, which contains usernames and password hashes.
- There is a JSON file for posts,
I'm looking for a full code review: I've only read the PHP manual today and while I understand most of its features I have no idea what the best practices are and how things should be implemented.
I will list below some of my main concerns with the code/design.
I have a core.php
file which is included with require
in every other PHP file.
It links the stylesheet at the beginning (and it could also import JS files):
<head>
<link rel="stylesheet" type="text/css" href="./CSS/style.css">
</head>
Is this the correct way of adding the same <head>
to every file?
Every time I want to access the user database or the posts database, I call these functions:
function getUsersDB() { return new UsersDB(); }
function getPostsDB() { return new PostsDB(); }
However, creating a new UsersDB
or PostsDB
instance forces a read from the .json
file. I need to access these databases in multiple PHP files.
Is there a way to "cache" these UsersDB
and PostsDB
instances? Something similar to C++'s static
, that can be shared between all PHP scripts?
When I have to generate something between HTML tags, I just call print
like this:
print("<strong>");
print($mPost["author"]);
print("</strong>");
Is there an easier/better way of generating open/close tags? Should I create a function that takes a lambda like:
function doBetweenStrong($lambda)
{
print("<strong>");
// execute $lambda here
print("</strong>");
}
Every page of my website has the same menu and "login status" bar. I simply call Builder::printCommon();
at the beginning of every page. Is this the best way to do it?
I keep track of the current logged-in user in $_SESSION
.
class Credentials
{
static public function isLoggedIn()
{
if(!isset($_SESSION["currentUser"])) return false;
if($_SESSION["currentUser"] == "") return false;
return true;
}
static public function login($mUser)
{
$_SESSION["currentUser"] = $mUser;
}
static public function logout()
{
unset($_SESSION["currentUser"]);
}
static public function getCurrentUserName()
{
return $_SESSION["currentUser"];
}
}
Is this safe? What is the correct way to keep track of the currently logged-in user?
I find it hard to show the user error/success messages (for example, after registration, or after login, or after posting something). The current solution is creating a new page like registerSuccess.php
or registerFailure.php
.
Should I create a general message.php
and pass the desired message to it using $_SESSION
?
Paths. Is there a reliable way to get the server's root folder?
I'm currently using ./
, ../
and ../../
to get the files I want from scripts. What if I want to move a script in the future? I would have to change all paths manually.
What if I include a script into another? Wouldn't the paths be resolved incorrectly?