smcjones
smcjones

Reputation: 5600

Apache/PHP user folders

I am trying to create user profiles, similar to Facebook, which creates subdirectory (you can share your profile with http://facebook.com/CUSTOMURL.

Particularly, I am trying to figure out if there is a manageable way to simulate domain name links. The way I am currently running this is via a script like this:

function create_user_directory($userid,$dirname=null){
        if ($this->uzr_directory_exists($userid)) {                                         
            return FALSE;                                                                   
        } else {
            if (!isset($dirname)) { 
                $dirname = $userid; 
            }
            $directory = ROOT_PATH . $dirname . "/";
            mkdir($directory);
            $handle = fopen($directory . "index.php", "w") or die("can't open file");
            $template = "<?php\n\$userid=" . $userid . ";\ninclude(\"ROOT_PATH . "templates/userProfile.php\");";
            fwrite($handle, $template) or die("can't write file");
            fclose($handle);
        }

    }

When someone changes their directory, I would use rename() to change the directory and update the server.

I have to imagine there is a better way. Any insight?

Upvotes: 0

Views: 70

Answers (1)

Petah
Petah

Reputation: 46060

mod_rewrite is the best bet for things like this.

A basic mod_rewrite rule you can put in a .htaccess file to redirect all non existing files/directories to index.php

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . index.php
</IfModule>

You can the just pull the URL out of $_SERVER.

Upvotes: 4

Related Questions