Current location: Hot Scripts Forums » Programming Languages » PHP » root url in php


root url in php

Reply
  #1 (permalink)  
Old 10-03-04, 04:28 AM
nassau nassau is offline
Wannabe Coder
 
Join Date: May 2004
Posts: 211
Thanks: 0
Thanked 0 Times in 0 Posts
root url in php

in my current code i use <? include("../_includes/rooturl.php");?> to make links. this way i can change to different servers and just change the rooturl.php file to make all links in my site correct.

there is one problem though, as you see above, the links to the rooturl.php page needs to be relative to where the file is located. this means i can't move files up or down in the folder hierarchy on the site or the links will fail.

is there a way to specify a URL with php and have it as a function that is not relative to a specific place on the server? for example: $rooturl; (<- i made this up, i don't know what the hell i'm doing here so don't take that example literary).


thanks
Reply With Quote
  #2 (permalink)  
Old 10-03-04, 05:28 AM
sufyan sufyan is offline
Newbie Coder
 
Join Date: Jun 2003
Location: Melbourne, AU
Posts: 53
Thanks: 0
Thanked 0 Times in 0 Posts
You could use an absolute path, example:

PHP Code:

<? include("/home/user/www/file.php");?>

This will work anywhere on the server as long as your 'file.php' is in /home/user/www/
__________________
suf.id.au
Reply With Quote
  #3 (permalink)  
Old 10-03-04, 08:35 AM
moronovich moronovich is offline
Junior Code Guru
 
Join Date: Oct 2004
Posts: 460
Thanks: 0
Thanked 0 Times in 0 Posts
This could be the hack for your problem. Its function algorithm uses variable reference method.
PHP Code:

function setRootPath(& $rootPath) {

     if(
preg_match("/{$_SERVER['DOCUMENT_ROOT']}/",$_SERVER['SCRIPT_FILENAME'])) {
         
//Set root path to document root
         
$rootPath $_SERVER['DOCUMENT_ROOT'];
     }
      if(!
preg_match('#/$#',trim($rootPath)) {
         
$rootPath trim($rootPath).'/';
      }
}

//Usage
//Directory tree
//Path: /home/www/
//                        ./_includes/
//                                       ./functions.php
//                        ./_content/
//                                       ./yourscript.php
//                        ./index.php

//yourscript.php
$rootDir '../'//always trail with single slash '/'
setRootPath($rootDir);
print 
$rootDir//output can be either '../' or '/home/www/'
include $rootDir.'_includes/functions.php'//include files with path relative to your index.php 
Reply With Quote
  #4 (permalink)  
Old 10-03-04, 11:26 AM
nassau nassau is offline
Wannabe Coder
 
Join Date: May 2004
Posts: 211
Thanks: 0
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by sufyan
You could use an absolute path, example:

PHP Code:

<? include("/home/user/www/file.php");?>

This will work anywhere on the server as long as your 'file.php' is in /home/user/www/
i'm guessing /home/user/www/ is the default for any server right? so if my URL is "www.website.com" the above will work? when i login into my server via ftp i'm one directory above the actual url, this folder is just called "/", no /home/user/... am i still on the right track?

the above is not working for me, that's why i'm asking...
Reply With Quote
  #5 (permalink)  
Old 10-03-04, 11:28 AM
nassau nassau is offline
Wannabe Coder
 
Join Date: May 2004
Posts: 211
Thanks: 0
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by moronovich
This could be the hack for your problem. Its function algorithm uses variable reference method.
PHP Code:

function setRootPath(& $rootPath) {

     if(
preg_match("/{$_SERVER['DOCUMENT_ROOT']}/",$_SERVER['SCRIPT_FILENAME'])) {
         
//Set root path to document root
         
$rootPath $_SERVER['DOCUMENT_ROOT'];
     }
      if(!
preg_match('#/$#',trim($rootPath)) {
         
$rootPath trim($rootPath).'/';
      }
}

//Usage
//Directory tree
//Path: /home/www/
//                        ./_includes/
//                                       ./functions.php
//                        ./_content/
//                                       ./yourscript.php
//                        ./index.php

//yourscript.php
$rootDir '../'//always trail with single slash '/'
setRootPath($rootDir);
print 
$rootDir//output can be either '../' or '/home/www/'
include $rootDir.'_includes/functions.php'//include files with path relative to your index.php 

thanks! would you mind giving a small explanation as to what each command does and how i can use this? how do i format my url etc?

i'm a newb so please be patient
Reply With Quote
  #6 (permalink)  
Old 10-03-04, 12:49 PM
moronovich moronovich is offline
Junior Code Guru
 
Join Date: Oct 2004
Posts: 460
Thanks: 0
Thanked 0 Times in 0 Posts
You can do this for the first step:
PHP Code:

print $_SERVER['DOCUMENT_ROOT'];

print 
'<br />';
print 
$_SERVER['SCRIPT_FILENAME'];
print 
'<br />';

//Function declaration
function setRootPath(& $rootPath) { 
     if(
preg_match("/{$_SERVER['DOCUMENT_ROOT']}/",$_SERVER['SCRIPT_FILENAME'])) { 
         
//Set root path to document root 
         
$rootPath $_SERVER['DOCUMENT_ROOT']; 
     } 
      if(!
preg_match('#/$#',trim($rootPath)) { 
         
$rootPath trim($rootPath).'/'
      } 

//Preg match is used to compare your current filename with your server default 
//document root. If there is a match, the function will set document root to your 
//server's default.
//The function parameter is passed by reference. It means that it doesn't make a 
//copy of the parameter passed but set an alias for it instead.

//Here
$rootDir '../'//always trail with single slash '/' 
setRootPath($rootDir);
//In the function, $rootDir will be bound with $rootPath in the function. when 
//$rootPath changes, $rootDir also changes.

print $rootDir//output can be either '../' or '/home/www/' 
include $rootDir.'_includes/functions.php'//include files with path relative to your index
//If your default root path is the same with your server's default, now the 
//string "$rootDir.'_includes/functions.php" should have 
//value "/home/www/_includes/functions.php" 
__________________
just an ignorant noob with moronic solution...
Reply With Quote
  #7 (permalink)  
Old 10-03-04, 02:35 PM
nassau nassau is offline
Wannabe Coder
 
Join Date: May 2004
Posts: 211
Thanks: 0
Thanked 0 Times in 0 Posts
ok.. so what do i do with this file? do include it in my index.php?
i'm sorry but i'm still confused. i'll try to figure it out..

thanks
Reply With Quote
  #8 (permalink)  
Old 10-03-04, 03:55 PM
nassau nassau is offline
Wannabe Coder
 
Join Date: May 2004
Posts: 211
Thanks: 0
Thanked 0 Times in 0 Posts
i found this on php.net:
PHP Code:

<? echo $_SERVER["SERVER_NAME"]; ?>

exactly what i was looking for!

but thanks for all the help! i appreciate it!
Reply With Quote
  #9 (permalink)  
Old 10-03-04, 06:21 PM
nekeno12 nekeno12 is offline
Wannabe Coder
 
Join Date: May 2004
Location: CO
Posts: 214
Thanks: 0
Thanked 0 Times in 0 Posts
Why doesn't anyone just use the default PHP include path? /usr/lib/php.

It's the best way to store your includes without having to worry about what directory level you're one.

in /usr/lib/php i make new folders for each project and include files as so:

<?php include("newproject/file.php"); ?>

Easier than writing code on every page

Last edited by nekeno12; 10-03-04 at 06:22 PM. Reason: Spelling
Reply With Quote
  #10 (permalink)  
Old 10-03-04, 06:43 PM
nassau nassau is offline
Wannabe Coder
 
Join Date: May 2004
Posts: 211
Thanks: 0
Thanked 0 Times in 0 Posts
nekeno, please explain more about that. where is /usr/lib/php? and how does <?php include("newproject/file.php"); ?> know where to find the include? would it not look for a folder, named newproject, within the current folder?
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

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 Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
PHP and MySQL ? rob2132 Hot Scripts Forum Questions, Suggestions and Feedback 4 08-29-08 02:22 AM
Mouseover with Single PHP page rjwebgraphix PHP 7 09-16-04 05:15 PM
php with Apache in windows eDevil PHP 3 08-08-04 12:03 AM
PHP Root Rapid Dr3am The Lounge 3 05-07-04 11:58 PM
100 Web Templates & 10 PHP Scripts for sale! HostersUK.co.uk General Advertisements 0 01-10-04 12:31 AM


All times are GMT -5. The time now is 04:34 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.