Question regarding PHP possibilites (users, admin/

2019-09-05 03:39发布

问题:

First of all, I'm kinda new to web programming and programming in general. HTML/CSS + Javascript are as far as I've gone over the last few years.

Now, for the purpose of my work, I need to learn PHP.

What I'm most interested about is user registration + users' privileges.

My task is to create a web application that is, let's say, a simpler version of a blog. I'd like to know is it possible to build such an application from scratch and add users with different privileges. And, if so, what is the best way to do it? I doubt I'm gonna be using some framework as I intend to learn PHP coding on my own. If you could point me to some useful resources I would be more than grateful :)

Cheers

EDIT: I'll try to be more specific. My goal is not to create a content management system from scratch. I'm interested to know if there is a way to build a blog-like application with user registration and different privileges that are going to be applied on every page.

回答1:

In order to solve that problem you need some idea about how you are going to construct your website. Most of the times header and footer should be in different files which are later included on every page. In header there should be some user validation code. Which depending on user privileges will create some global variable which will later serve as a flag.

For example: let's say you have privileges from 5 to 1. Five being full access and 1 being most restricted access. So later in the code you should create test for the privileges status which will change the output of the page depending on the number.



回答2:

Okay. The way I do this (and it seems to work well) is to have individual classes in which you define all of the core functions. Ex,

//core.class.php 
class Core {
    public function someStuffForCore() {
    //Stuff
    }
}

//admin.class.php
class Admin {
    public function userManagement() {
    //User Management
    }
}

Then you would include them in your index.php file, and create new instances of each class, and then global them for use later, like

require_once("/include/scripts/core.class.php");
require_once("/include/scripts/admin.class.php");
$core = new Core;
$admin = new Admin;
global $core, $admin;

Like what timik said, I would keep your layout in another file somewhere else. I like to keep all of my layouts in an array like

//layout.php
$layout = array (
    'header'=>'<html><head>..</head><body><div class="body">',
    'footer'=>'</div></body></html>'
    );

Then, if you need to, you can store the format for your posts, and use sprintf This is how I would do it, and you may do it differently.