[SOLVED] Getting the current domain

This is a discussion on "[SOLVED] Getting the current domain" within the PHP Forum section. This forum, and the thread "[SOLVED] Getting the current domain are both part of the Program Your Website category.



Go Back   Webforumz.com > Main Forums > Program Your Website > PHP Forum

Notices


Reply
 
LinkBack Thread Tools
  #1 (permalink)  
Old Nov 22nd, 2007, 16:54
SuperMember

SuperMember
Join Date: May 2007
Location: UK
Age: 27
Posts: 1,111
Thanks: 0
Thanked 0 Times in 0 Posts
[SOLVED] Getting the current domain

I want to assign a variable to the current domain of the web page. When I'm developing the site, the domain is "http://localhost/", but when I upload it the domain is "http://www.badmintonbible.com/".

So I'm using this variable to make links work both locally and on the site itself.

I've tried using $_SERVER['DOCUMENT_ROOT'], but this goes too deep: it returns all the server's file structure.

There must be a pre-assigned variable for this, but I haven't found it. Any ideas?
Reply With Quote

  #2 (permalink)  
Old Nov 22nd, 2007, 17:00
c010depunkk's Avatar
SuperMember

SuperMember
Join Date: Apr 2007
Location: Willich, Germany
Age: 20
Posts: 593
Blog Entries: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to c010depunkk
Re: Getting the current domain

I had the same problem and here's my solution. This function returns the current domain plus the folder structure to where the file is located plus a trailing slash:
PHP: Select all

function getLinkPrefix() {
    
preg_match('#^/([^/]+)/[.]*#',$_SERVER['PHP_SELF'],$regs);
    return 
'http://'.$_SERVER['HTTP_HOST'].'/'.($regs[1]==''?'':$regs[1].'/');

Hope this helps, you should be able to modify it to work. I think the $_SERVER['HTTP_HOST'] is interesting 4 u.

Last edited by c010depunkk; Nov 22nd, 2007 at 17:05.
Reply With Quote
  #3 (permalink)  
Old Nov 22nd, 2007, 17:02
karinne's Avatar
SuperMember

SuperMember
Join Date: Jan 2007
Location: You know where
Age: 31
Posts: 4,617
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Getting the current domain

Hmmmm ... I can't test any of this as I don't have access but... have you tried

'HTTP_HOST'
Reply With Quote
  #4 (permalink)  
Old Nov 22nd, 2007, 17:22
Junior Member
Join Date: Jan 2006
Location: Newport, UK
Age: 18
Posts: 35
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to @lun
Re: Getting the current domain

what karinne said:

this has always worked for me

PHP: Select all

$url "http://".$_SERVER['HTTP_HOST']."/"
Reply With Quote
  #5 (permalink)  
Old Nov 22nd, 2007, 17:40
SuperMember

SuperMember
Join Date: May 2007
Location: UK
Age: 27
Posts: 1,111
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Getting the current domain

Quote:
Originally Posted by @lun View Post
what karinne said:

this has always worked for me

PHP: Select all

$url "http://".$_SERVER['HTTP_HOST']."/"
Fantastic, that's solved it -- and quickly, too.

Thanks everyone!
Reply With Quote
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
highlight current page unitedcraig Web Page Design 53 Feb 13th, 2008 22:20
[SOLVED] close current window in php script Posie PHP Forum 7 Dec 14th, 2007 05:38
[SOLVED] Domain Fowarding? Good Idea? TheSealPortalTeam Web Page Design 9 Dec 2nd, 2007 13:31
Getting current URL in java QuikFrozen Other Programming Languages 3 Mar 27th, 2007 23:38


All times are GMT. The time now is 07:40.


Powered by vBulletin®
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC8
© 2003-2008 Webforumz.com : All Rights Reserved

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43