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
https://forums.phpfreaks.com/topic/217898-auto-generate-a-new-php-page/
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.

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.

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

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");
}

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?

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.