Jump to content

* Auto generate a new PHP page


unistake

Recommended Posts

Hi all,

 

I am using php to allow users to create a new directory e.g. www.website.com/theirname/ and would like to auto generate an index.php (or index.html) page if possible within this directory. Any tips on how to do it would be appreciated.

 

The code I have for generating the directory is:

 

<?php 

function handleError() { 
    trigger_error('MY ERROR'); 

    /** usage sample 
        @handleError(); 
        echo $php_errormsg; 
    */ 
} 

// detect slash/backslash nomenclature dirname 
$path = dirname( __FILE__ ); 
$slash = '/'; 

(stristr( $path, $slash )) ? '' : $slash = '\\'; 
define( 'BASE_DIR', $path . $slash ); 

$folder  = $_POST['reg'];               // folder name 
$dirPath = BASE_DIR . $folder;   // folder path 

// print results 
echo $slash; 
echo '<hr>'; 

$rs = @mkdir( $dirPath, 0777 ); 
@handleError(); 
if( $rs ) 
{ 

    // print success information 
    echo 'was done!'; 
    echo '<br>folder: <a href="' . $folder . '">' . $folder . '</a>'; 
    echo '<br>dirPath: ' . $dirPath; 

}else{ 
   
    // print error information 
    echo "This webpage <b>www.website.com/".$folder."</b> appears to have been already been created. Please try another web name. If the problem persists please report it to us."; 
} 

?> 

Link to comment
Share on other sites

Hi Thorpe,

 

Thanks for the advice, I was thinking the same but for my website I want each user to have their own directory such as www.website.com/mine so that they have a memorable web name to pass on to their friends.

 

I am fully aware that one php can do all but I just wanted this directory for the above use.

 

In question to your comment, is there a way then of just adding an index.php page to the new directory then, and displaying all the information relating to that new directory name?

 

e.g.

www.website.com/mine

$directoryname = 'mine' from domain above.

$sql = "SELECT * FROM table WHERE id='$directoryname'";

 

How would I get the directory name in to a variable?

 

Thanks.

Link to comment
Share on other sites

As Thorpe said, you don't need to create a directory. You can use the Apache module mod_rewrite to rewrite the request URL from "example.com/mine" to something like "example.com/profile.php?username=mine".

 

Create a file called ".htaccess" in your web root directory, and add:

 

<IfModule mod_rewrite.c>
    RewriteEngine on

    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^([a-zA-Z0-9_\.-]+)$ profile.php?username=$1
</IfModule>

 

Create a file called profile.php, and just add the following for now:

 

<?php

print_r($_GET);

?>

 

Then go to 'yourserver.com/mine'.. You should see the array printed out containing the username parameter.

Link to comment
Share on other sites

this is a great post, question ontop, dont see the need for a new thread,

 

what if in your site you have the neccessary directories /Contact /About etc...

 

is this going to rewrite them to profile.php?user=contact etc etc? or will current directories and future addon directories remain directores

Link to comment
Share on other sites

but the .hta... will automatically create the impression that /johnsmith exists but then profile.php will say "no johnsmith found" for example?

 

and the 404 error? everything request that doesnt match a folder / file will be treaten as a username?

Link to comment
Share on other sites

Only requests matching the rewrite rule (a basic regex) will be rewritten:

 

RewriteRule ^([a-zA-Z0-9_\.-]+)$ profile.php?username=$1

 

That means requests containing only the following characters will be treated as usernames "a-z A-Z _ . -" .. Otherwise the normal 404 error will be thrown. You can customize that to match your own needs; add a prefix like "user/", reduce the characters allowed, or add other rewrite conditions.

 

For usernames that do match the pattern but don't exist, you want to throw a 404 header response code to tell the bwoser/search engines that page isn't found:

 

if (/* username not found */)
{
    header("HTTP/1.0 404 Not Found");
}

Link to comment
Share on other sites

so :)

<IfModule mod_rewrite.c>
    RewriteEngine on

    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^([a-zA-Z0-9_\.-]+)$ ClientArea/profile.php?username=$1
</IfModule>

 

would ONLY rewrite request that look like "www.domain.com/ClientArea/BP" not, "www.domain.com/BP" that would get ignored?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.