简体   繁体   中英

Create a copy/ session of a site for demo purposes

I coded a PHP ecommerce site I will show to potential clients/buyers. They would be able to create and delete categories, products, upload images and play as they wish with the site and database, even deleting everything as some no doubt will.

It is a full demo of the product, that the user will access with a demo admin account, as a portfolio or to convince small business to buy it.

Basically I want them to be free to use the site as they will but I don't want to restore it manually everytime someone wrecks it.

So my idea is to create a copy/session of the site and MySQL database everytime a visitor clicks the url and delete it when they close the browser. A new user would get a new, pristine copy of the site.

Concomitant visitors would each have a copy of their own to use.

Googling/Binging an implementation of such a thing failed me. Maybe I can't word it right but I found nothing that is close to what I need.

So I ask for help about pointers to how I could code it. Thanks in advance.

Option 1:

You can make a php file with this codes :

$command = "mysql --user={$vals['db_user']} --password='{$vals['db_pass']}' "
 . "-h {$vals['db_host']} -D {$vals['db_name']} < {$script_path}";

$output = shell_exec($command . '/backup.sql');

to restore database & cleanup your database (In SQL file need DROP & CREATE all tables again).

For remove uploaded files do this :

//The name of the folder.
$folder = 'temporary_files';

//Get a list of all of the file names in the folder.
$files = glob($folder . '/*');

//Loop through the file list.
foreach($files as $file){
    //Make sure that this is a file and not a directory.
    if(is_file($file)){
        //Use the unlink function to delete the file.
        unlink($file);
    }
}

Use both of them in one file like cron.php & then add a button on your website to manually clear by your users, also you can add this to your server cronjob for hourly/daily/weekly/monthly clear.

Option 2:

If use CPanel - you need read that cPanel API 2 Functions . create a subdomain for a user with PHP like this code (API1 Example):

$xmlapi->api1_query($cpanelusr, 'SubDomain', 'addsubdomain', array('user123','bodoamat.com',0,0, '/public_html/NewUserDirectory')

Then copy your package.zip & then extract that inside /public_html/NewUserDirectory

then create a database & account with cPanel API 2 Functions .

And with this code add database username/password/name in user config file:

$file_path= 'public_html/NewUserDirectory/config.php';
// Open the file to get existing content
$current = file_get_contents($file_path);

$data_to_write = str_replace("DATABASE_USERNAME",$newdb_user,$current);
$data_to_write = str_replace("DATABASE_PASSWORD",$newdb_pass,$data_to_write);
$data_to_write = str_replace("DATABASE_NAME",$newdb_name,$data_to_write);

file_put_contents($file_path, $data_to_write);

every user have a subdomain for test/use.

Option 3:

You can add this subdomain ( *.domain.ir to your /public_html directory) in DNS server for domain.

in your PHP codes you must read domain ( $_SERVER['REQUEST_URI'] ) & then use a prefix tables or another database for every user. (for example if they open user1.domain.ir use user_TABLENAME if exists.) and make a upload directory for every user.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM