Search code examples
phpcontent-management-systemcreateprocessmulti-user

dynamically creating php files and tables


I'm new to PHP and after writing a couple CMS' I'd like to try something more complex.

What I want to do is create a script whereby I can create a whole user area with functions, classes and forms etc. Something like a personal admin panel for the user but within their own directory. The aim would be to then create their own webpage within the webpage. I assume this is a common task as I guess it is what all blogs, portfolio sites, facebook, youtube do.

I have been experimenting with mkdir fopen fwrite and similar commands but I don't know how I could use these - without LOTS of code - to create fully functioning user areas like an admin panel.

After writing this I'm seeing problems in scaling this. If users are always creating files then this will eventually take up a lot of server space. Would a better way to do this be via MVC in a framework like Codeigniter?

Or would the best way to do this be by using php copy()?


Solution

  • So you want to make facebook without lots of code. Good luck :)

    But to create a seperate userarea you dont physically create files for those users. You have a database that stores the data per user and you have a php file that selects the right data for the right user.

    The example below is a very simplistic proof of concept, and should not be used directly.

    you have a table users

    id | Name
    1  | Elisa
    2  | Hugo
    

    And a table content

    id | UserId | MyBlogEntry
    1  | 1      | Hello, this is my blog
    2  | 1      | Hello, this is my 2nd blog
    3  | 2      | I dont like blogging
    

    Now you have a page called usercontent.php which accepts a username as variable. You call it like usercontent.php?user=Elise or usercontent.php?user=Hugo

    The content: Keep in mind I left out all validation checks for simplicity. You need to make sure there are valid users and no SQL Injection

    <?
    $user = isset($_GET['user']) ? $_GET['user'] : ''
    
    if (empty($user)) {
      echo 'I dont know this user';
      exit;
    }
    
    echo $user.'\'s Blog<hr>';
    
    $query = "SELECT * FROM content WHERE UserId=(SELECT Id FROM users WHERE name='".$user."')";
    
    //execute mysqli query
    
    while($row = $result->fetch_array(MYSQLI_ASSOC))
    {
      echo '<p>' . $row['MyBlogEntry'] . '<p>';
    }
    ?>
    

    The ouput would be be something like this for Elisa

    Elisa's blog
    ---------------------
    Hello, this is my blog
    
    Hello, this is my 2nd blog
    

    And for Hugo

    Hugo's blog
    ---------------------
    I dont like blogging