#1
  1. No Profile Picture
    Contributing User
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Dec 2004
    Posts
    3,008
    Rep Power
    376

    Testing sites on local/test/live sites.. question about linking


    at the moment I have a script that gets run on every page load something to the effect of

    PHP Code:
    if ( strpos $_SERVER['SCRIPT_FILENAME'], "C:/Documents/" ) !== false ) {

        
    defined 'HOME_PATH' )   ? null define 'HOME_PATH',  'C:/Documents/wamp/www/test/'  ); //
        
    defined 'SITE_PATH' )   ? null define 'SITE_PATH',  'C:/Documents/wamp/www/test/websites'  );
        
    defined 'SERVER' )      ? null define 'SERVER',     'local'     );
        
    defined 'SITE_URL' )    ? null define 'SITE_URL',   'http://localhost/test/websites/sitename/'     );

    } elseif ( 
    strpos $_SERVER['SCRIPT_FILENAME'], "test_teradatamedia" ) !== false ) {
        
        
    defined 'HOME_PATH' )   ? null define 'HOME_PATH',  '/home/www/test/'  ); //
        
    defined 'SITE_PATH' )   ? null define 'SITE_PATH',  HOME_PATH 'websites/sitename/'  );
        
    defined 'SERVER' )      ? null define 'SERVER',     'test'     );
        
    defined 'SITE_URL' )    ? null define 'SITE_URL',   'http://site_url/'     );
        
    } else {
        
        
    defined 'HOME_PATH' )   ? null define 'HOME_PATH',  '/home/www/live'  ); //
        
    defined 'SITE_PATH' )   ? null define 'SITE_PATH',  HOME_PATH 'websites/sitename/'  );
        
    defined 'SERVER' )      ? null define 'SERVER',     'live'     );
        
    defined 'SITE_URL' )    ? null define 'SITE_URL',   'http://site/'     );
        

    Now i am thinking if there is a better way because I want to create a GENERIC one that doesnt have a sitename because i want to use this script for other sites as well. But this means that inside the links i would then have to do

    <?= include_once 'SITE_PATH."/".sitename."/".... ?>

    SO i was thinking what you guys do. The main idea is of course to make sure sites work throughout my local, test & live server without me needing to do anything
  2. #2
  3. Mad Scientist
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Oct 2007
    Location
    North Yorkshire, UK
    Posts
    3,661
    Rep Power
    4124
    I use a combination of

    $_SERVER['DOCUMENT_ROOT']
    $_SERVER['SERVER_NAME']
    dirname()

    and occasionally

    PHP Code:
    $os PHP_OS;
    switch(
    $os)
    {
        case 
    "Linux"define("SEPARATOR""/"); break;
        case 
    "Windows"define("SEPARATOR""\\"); break;
        default: 
    define("SEPARATOR""/"); break;

    and then define

    WEB_BASE_PATH (for http requests, eg "/")
    ADMIN_BASE_PATH (for http requests to admin area, eg "/admin/")
    APP_BASE_PATH (for app and file system, eg "/var/www/html/")

    When developing locally I create a new vhost in my httpd.conf file, so I access

    http://local.sitename/

    and edit my etc/hosts file to set this to 127.0.0.1

    Then I use a directory scanner with relection [PHPNET="token_get_all"]token_get_all()[/PHPNET] to automatically maintain a class/file-list hashmap for my autoloader.
    Last edited by Northie; October 25th, 2013 at 04:51 AM.
    I said I didn't like ORM!!! <?php $this->model->update($this->request->resources[0])->set($this->request->getData())->getData('count'); ?>

    PDO vs mysql_* functions: Find a Migration Guide Here

    [ Xeneco - T'interweb Development ] - [ Are you a Help Vampire? ] - [ Read The manual! ] - [ W3 methods - GET, POST, etc ] - [ Web Design Hell ]
  4. #3
  5. Wiser? Not exactly.
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    May 2001
    Location
    Bonita Springs, FL
    Posts
    5,953
    Rep Power
    4033
    Originally Posted by Northie
    and occasionally

    PHP Code:
    $os PHP_OS;
    switch(
    $os)
    {
        case 
    "Linux"define("SEPARATOR""/"); break;
        case 
    "Windows"define("SEPARATOR""\\"); break;
        default: 
    define("SEPARATOR""/"); break;

    That is unnecessary, PHP already defines a constant for this called DIRECTORY_SEPARATOR. If you wanted to make it shorter you could just do define('DS', DIRECTORY_SEPARATOR);

    @paulh1983 if you have a common include file then you can work out the path to it using __FILE__ or __DIR__. From there, you can use dirname() to go up in the tree and concatenate other paths. For example, I typically have a layout like:

    Code:
    /
       /includes
          common.inc.php
          classes/
             Somefile.class.php
       /template
          main.tpl
       index.php
    Within common.inc.php I have code like this:
    Code:
    define('DS', DIRECTORY_SEPARATOR);
    define('DOCUMENT_ROOT', dirname(__DIR__));
    define('CLASSES_ROOT', DOCUMENT_ROOT.DS.'includes'.DS.'classes');
    define('TEMPLATES_ROOT', DOCUMENT_ROOT.DS.'templates');
    That sets up all the useful path constants I'd need. Then in my main PHP files the first thing I do is include common.inc.php using a relative path. After that I can use the constants for further includes or file manipulation.
    Recycle your old CD's, don't just trash them



    If I helped you out, show some love with some reputation, or tip with Bitcoins to 1N645HfYf63UbcvxajLKiSKpYHAq2Zxud
  6. #4
  7. Mad Scientist
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Oct 2007
    Location
    North Yorkshire, UK
    Posts
    3,661
    Rep Power
    4124
    Originally Posted by kicken
    That is unnecessary, PHP already defines a constant for this called DIRECTORY_SEPARATOR. If you wanted to make it shorter you could just do define('DS', DIRECTORY_SEPARATOR);
    Very true and I do use it on occasion, but I found it was always "/" on both linux (obviously) and on my local windows machine (oddly) which, on occasion, broke things...hence,
    Originally Posted by me
    and occasionally
    and
    PHP Code:
    dirname(__DIR__
    is always a nice bit of magic
    I said I didn't like ORM!!! <?php $this->model->update($this->request->resources[0])->set($this->request->getData())->getData('count'); ?>

    PDO vs mysql_* functions: Find a Migration Guide Here

    [ Xeneco - T'interweb Development ] - [ Are you a Help Vampire? ] - [ Read The manual! ] - [ W3 methods - GET, POST, etc ] - [ Web Design Hell ]
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Dec 2004
    Posts
    3,008
    Rep Power
    376
    Originally Posted by kicken
    That is unnecessary, PHP already defines a constant for this called DIRECTORY_SEPARATOR. If you wanted to make it shorter you could just do define('DS', DIRECTORY_SEPARATOR);

    @paulh1983 if you have a common include file then you can work out the path to it using __FILE__ or __DIR__. From there, you can use dirname() to go up in the tree and concatenate other paths. For example, I typically have a layout like:

    Code:
    /
       /includes
          common.inc.php
          classes/
             Somefile.class.php
       /template
          main.tpl
       index.php
    Within common.inc.php I have code like this:
    Code:
    define('DS', DIRECTORY_SEPARATOR);
    define('DOCUMENT_ROOT', dirname(__DIR__));
    define('CLASSES_ROOT', DOCUMENT_ROOT.DS.'includes'.DS.'classes');
    define('TEMPLATES_ROOT', DOCUMENT_ROOT.DS.'templates');
    That sets up all the useful path constants I'd need. Then in my main PHP files the first thing I do is include common.inc.php using a relative path. After that I can use the constants for further includes or file manipulation.
    I was thinking of creating such code and using auto prepend feature

IMN logo majestic logo threadwatch logo seochat tools logo