dongrou2920 2013-05-07 07:15
浏览 55
已采纳

动态创建php文件和表

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()?

  • 写回答

1条回答 默认 最新

  • dongpo1216 2013-05-07 07:32
    关注

    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
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥35 平滑拟合曲线该如何生成
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 自己瞎改改,结果现在又运行不了了
  • ¥15 链式存储应该如何解决
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站