absolute links with $_SERVER['SERVER_NAME'] issue

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Rippie
Forum Commoner
Posts: 76
Joined: Sun Jan 10, 2010 11:32 am
Location: Nottingham

absolute links with $_SERVER['SERVER_NAME'] issue

Post by Rippie »

Hi all.

Having some issues with $_SERVER['SERVER_NAME'] making absolute links. $_SERVER['SERVER_NAME'] it self echo to "intranet" and when i do http://".$_SERVER['SERVER_NAME']."/index.php it make the link to http://intranet/intranet/index.php..... does anyone know why???

Rippie
User avatar
mecha_godzilla
Forum Contributor
Posts: 375
Joined: Wed Apr 14, 2010 4:45 pm
Location: UK

Re: absolute links with $_SERVER['SERVER_NAME'] issue

Post by mecha_godzilla »

Have you tried using $_SERVER['SERVER_ADDR'] or $_SERVER['HTTP_HOST'] instead to see what happens?

This article explains where the $_SERVER['SERVER_NAME'] is derived from, which might explain the results you're getting:

http://shiflett.org/blog/2006/mar/serve ... -http-host

HTH,

Mecha Godzilla
Rippie
Forum Commoner
Posts: 76
Joined: Sun Jan 10, 2010 11:32 am
Location: Nottingham

Re: absolute links with $_SERVER['SERVER_NAME'] issue

Post by Rippie »

HTTP_HOST worked like a treat.. Thank you very much for your reply.
Rippie
Forum Commoner
Posts: 76
Joined: Sun Jan 10, 2010 11:32 am
Location: Nottingham

Re: absolute links with $_SERVER['SERVER_NAME'] issue

Post by Rippie »

mecha_godzilla,

Thinking of including a systemvariables.php on each of my pages where i could have $siteaddress = "http://mydomain.com"; and another one for the include paths so when i need to link to something i can do echo "$siteaddress/path/file.xxx"; that way i only need to change 1 place if my domain name changes. what is your thoughts on this ?

Rippie
Rippie
Forum Commoner
Posts: 76
Joined: Sun Jan 10, 2010 11:32 am
Location: Nottingham

Re: absolute links with $_SERVER['SERVER_NAME'] issue

Post by Rippie »

Merca

Just found an application that uses a big like what i want to use.... look at this

Code: Select all

global $CONF;
unset($CONF);

/**
 * PERSONALIZATION
 * You can alter these entries to personalize your TeamCal Pro installation.
 * You might also want to look at the indludes/header.application.inc.php
 * file if you want to display your own header image.
 * Sample:
 * $CONF['app_root'] = $_SERVER['DOCUMENT_ROOT']."/tcpro/";
 * $CONF['app_url'] = "http://www.myserver.com/tcpro";
 */
$CONF['app_root'] = $_SERVER['DOCUMENT_ROOT']."/teamcal/";
$CONF['app_url'] = "http://www.rippie.dk/teamcal";

$CONF['app_avatar_dir'] = 'img/avatar/';
$CONF['app_icon_dir'] = 'img/icons/';
but must say i am not sure if it is any smart to do global $CONF and not sure why unset $CONF. can you cast some light on this ?
User avatar
mecha_godzilla
Forum Contributor
Posts: 375
Joined: Wed Apr 14, 2010 4:45 pm
Location: UK

Re: absolute links with $_SERVER['SERVER_NAME'] issue

Post by mecha_godzilla »

Hmm...I think I need to see the full script to answer that one :)

The point of having the unset() in there is to destroy any local instances of $CONF but I'm not sure why that would be useful in this particular script. From the looks of it, this code must be from part of a function because global is being used to access $CONF and $CONF must therefore have already been defined in the main body of the script somewhere. I'll be honest, I've always found the way PHP does local and global variables a bit strange (at least, it doesn't seem like the "normal" way to do it) but I would guess that maybe this bit of code is included as a way to make sure that the values aren't accidentally added to a 'local' $CONF array instead of the 'global' $CONF array.

Does that make any sense? If you want a proper answer, post the full code! :)

M_G
Rippie
Forum Commoner
Posts: 76
Joined: Sun Jan 10, 2010 11:32 am
Location: Nottingham

Re: absolute links with $_SERVER['SERVER_NAME'] issue

Post by Rippie »

Code: Select all

<?php
/**
 * config.default.php
 * 
 * Contains the default config parameters. Is being read by the installation
 * script and used to create config.tcpro.php.
 * 
 * @package TeamCalPro
 * @version 3.2 
 * @author George Lewe
 * @copyright Copyright (c) 2004-2009 by George Lewe
 * @link http://www.lewe.com
 * @license http://www.lewe.com/tcpro/doc/license.txt Extended GNU Public License
 */

global $CONF;
unset($CONF);

/**
 * PERSONALIZATION
 * You can alter these entries to personalize your TeamCal Pro installation.
 * You might also want to look at the indludes/header.application.inc.php
 * file if you want to display your own header image.
 * Sample:
 * $CONF['app_root'] = $_SERVER['DOCUMENT_ROOT']."/tcpro/";
 * $CONF['app_url'] = "http://www.myserver.com/tcpro";
 */
$CONF['app_root'] = $_SERVER['DOCUMENT_ROOT']."/tcpro/";
$CONF['app_url'] = "http://localhost/tcpro";

$CONF['app_avatar_dir'] = 'img/avatar/';
$CONF['app_icon_dir'] = 'img/icons/';

/**
 * DATABASE
 * Currently only db_type=1 (MySQL) is supported.
 */
$CONF['db_type']   = 1;
$CONF['db_persistent'] = false;

$CONF['db_server'] ="localhost";
$CONF['db_name']   ="test";
$CONF['db_user']   ="root";
$CONF['db_pass']   ="";
?>
As you can see i looked at TeamCal pro code to see how he did his apps variables so thought i would copy it using "systemvars" but i am not really sure if after setting these variables can use $CONF['app_root'] as a normal var or not.
User avatar
mecha_godzilla
Forum Contributor
Posts: 375
Joined: Wed Apr 14, 2010 4:45 pm
Location: UK

Re: absolute links with $_SERVER['SERVER_NAME'] issue

Post by mecha_godzilla »

I don't think $CONF is any kind of special/reserved value, but you might want to check if the TeamCal scripts use any other libraries that rely on it. If you're writing a completely new script from scratch then you can use whatever names you want - some of these values (such as the root folder of the application) could be defined as a constant instead of as a key/value pair in an array, by using:

Code: Select all

DEFINE("APPLICATION_ROOT", "/path/to/my/webfolder/");
echo 'The application root folder is: ' . APPLICATION_ROOT;
or

Code: Select all

DEFINE("APPLICATION_ROOT", $_SERVER['HTTP_HOST']);
echo 'The application root folder is: ' . APPLICATION_ROOT;
The good thing about using constants is that they're available to all parts of the script so you don't have to keep declaring global $app_root or whatever in all your functions. You do need to be careful about not having any spaces in the name though, or if you do then you need to reference them as constant("APPLICATION ROOT") instead.

Does that answer your question?

M_G
Post Reply