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

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。