0

I'm developing PHP scripts on localhost and it is working without any issues, but when I upload it to share host I found different configs on the host as simple things, the names of files or folder if they are not the same, that throws errors and other things relate to databases.


My question is: Is there any way or resources to be sure that my PHP script will not face these types of issues because I don't have control on me

HanSal
  • 1
  • 2
  • 1
    Use a configuration file for things that could change from host to host, as most open source projects do. Try to avoid absolute paths (that include usernames) where possible (or use a constant if you feel like it) – Jesse May 10 '21 at 01:52
  • could you give me examples for that that could change from host to other – HanSal May 10 '21 at 14:04

1 Answers1

1

Short answer: no.

Putting your application to the production environment (the server) can be complicated process (usually called "deployment"). Shared hosts are limiting your control indeed, but most of them provide you enough flexibility to run a "common" PHP application. Things you will probably need:

  • Find out where is the php.ini on the host
  • Compare it with your local php.ini
  • Use the __DIR__ magic constant (that always gives you the directory of the file where you wrote it). Use it for includes and file copy/delete path.

If you use a general include file at the top of all your scripts, then you can write this in there: define('MY_ANCHOR', dirname(__DIR__));. (well, they usually call it BASE_PATH or ROOT_PATH, but you get the idea). After that you can use it for every other include and file/directory path like this: include( MY_ANCHOR.'/CoolThings/GimmeThis.php' );

If you are using an index.php and route all requests through that (single entry point), then put that MY_ANCHOR thingy on top of that file.

Skacc
  • 1,726
  • 2
  • 12
  • 16