tags:

views:

70

answers:

3

In my web application, users can download a .tar.gz archive containing the app files. However, because the MySQL database won't have been configured then, the user needs to run the install script located in ./install.

I "catch" the user upon first run of the app by checking to see if the ./install directory exists. If so, the index.php page redirects the user to the install script.

However, I was wondering if there were a more elegant way to "catch" a user upon first-run of a program.

Someone on IRC suggested the web-server create a file .installed upon completion, but because the web server might not have write privileges to the web root directory, I can't rely on that.

How would you go about solving this problem, or is my solution workable?

A: 

Other than the solution you provided, I could think that you do the opposite: Add a .not-installed file to your tar and delete after installation finishes.

soulmerge
But I would still need write privileges to delete the file, yes?I can't always depend on the end-user to keep the application owned by www-data, can I? I tend not do, but I could be doing things "the wrong way"...
Julian H. Lam
@Julian H. Lam: Well you can specify that your application needs write privileges in order to work. I don't think that e.g. wordpress works differently. It also needs write privileges.
Felix Kling
Yes, any CMS I know requires you to either manually delete a file (or folder) or write access to the installation folder.
soulmerge
+3  A: 

When MySQL queries fail with the error code 1146, it means the tables don't exist. You could be on the lookout for such errors, since they either mean the software wasn't installed, or that the installation is broken.

As Julian H. Lam said in the comments, the mysql_errno() function returns that error number. In case of MySQLi, it's either the property $connection->errno or the function call mysqli_errno($link).

zneak
This is a great solution, as it'll also handle the inevitable case where the user deletes the DB for some reason.
fatcat1111
I like this... it works better than having to use php exec(), which I am afraid of doing anyway.
Julian H. Lam
For those wondering:mysql_errno() returns the error number.
Julian H. Lam
A: 

Most web applications out there (wordpress, drupal, etc) assume it is a first-run condition when there is a lack of settings in a file, or the presence of a file. By removing or forcing the user to remove a file upon first-run of an application you also guarantee security because the application can't be re-configured by deleting or changing the name of a file... which might be easier to do than create a new file and simulate its contents.

More advanced frameworks will read and modify a configuration file to indicate first-run. In a Zend Framework application you would read, then modify one of the installations configuration files.

Because a web application executes code when it receives a request, code you do not want to be executed again should be deleted or outside of the 'public' directory. Projects that are extremely concerned with a setup being re-ran delete the setup file altogether.

In your installation you can check for write permissions to the URL root, if you lack them tell the user to delete setup.php (for example)

  $delete = False;
  $dir = dirname(__FILE__);
  if(is_writeable(__FILE__)) {
     if(unlink(__FILE__)) {
       $delete = True;
     }
  }

  if(!$delete) {
    $file = __FILE__
    print "IMPORTANT please remove/rename the file {$file} to complete installation"
  }
Ben DeMott