SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Enthusiast
    Join Date
    May 2007
    Posts
    33
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Identifying the server

    Hi,

    I have a development platform with a development server and a production server. I'll upload my projects to both servers, but I want exceptions to be handled differently on development servers. Because I want to be able to upload the exact same code to both servers, I want php to detect which server it is running on and act accordingly. I thought this would be an easy task but it isn't.

    So far I tried the following:
    Setting an environment variable in /etc/profile and reading it using php's getenv function.
    Didn't work because from php you can only read a couple of environment variables, and not those in etc/profile.

    Getting the server address and/or host using the $_SERVER superglobal.
    Didn't work for crons.

    Setting a custom non-standard setting (error_message_mode) in php.ini.
    Didn't work because only standard settings will be available in ini_get.

    Getting the ip address of the server using some linux function.
    I frankly don't know how to do this. Any ideas?

    How can I do this in a simple way?

    Regards,
    Bas

  2. #2
    SitePoint Enthusiast
    Join Date
    May 2007
    Posts
    33
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    By the way, I am now using display_startup_errors and ini_get in order to get the mode (default or live), because it is recommended to only turn on this flag in debug mode. However, I still like to know how to set custom server-specific flags in php.

  3. #3
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    9,077
    Mentioned
    153 Post(s)
    Tagged
    2 Thread(s)
    I use auto_prepend_file in php.ini for this.

    It includes a file that has (among others)
    PHP Code:
    defined('DEVELOPENT_SERVER') or define('DEVELOPMENT_SERVER'true); 
    and in the index/bootstrap file of projects I have

    PHP Code:
    defined('DEVELOPENT_SERVER') or define('DEVELOPMENT_SERVER'false); 
    Since you can also set this in the php.ini for CLI, it should also cover cron jobs.

    It's the easiest way I've been able to come up with so far.

    Edit:


    Forgot to mention: I have that auto_prepend_file on dev server(s) only, otherwise the approach doesn't make sense
    Rémon - Hosting Advisor

    SitePoint forums will switch to Discourse soon! Make sure you're ready for it!

    Minimal Bookmarks Tree
    My Google Chrome extension: browsing bookmarks made easy

  4. #4
    SitePoint Enthusiast
    Join Date
    May 2007
    Posts
    33
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    I thought of that approach. But I want to be able to upload identical code to both servers, and have the code itself figure out whether it is on a development server or live server. The define-approach is great normally, but it requires you to edit the project code every time you upload the file to a test or live server.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •