using relative/absolute URI's

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
slaterino
Forum Commoner
Posts: 46
Joined: Fri Jul 11, 2008 10:50 am

using relative/absolute URI's

Post by slaterino »

Hi,
I have been having some problems with my PHP gallery and am so looking at the possibility of using absolute or possibly relative URIs to see if this can be fixed. I'm basing this on this advice:
Note: HTTP/1.1 requires an absolute URI as argument to » Location: including the scheme, hostname and absolute path, but some clients accept relative URIs. You can usually use $_SERVER['HTTP_HOST'], $_SERVER['PHP_SELF'] and dirname() to make an absolute URI from a relative one yourself:

Code: Select all

<?php
/* Redirect to a different page in the current directory that was requested */
$host  = $_SERVER['HTTP_HOST'];
$uri   = rtrim(dirname($_SERVER['PHP_SELF']), '/\\');
$extra = 'mypage.php';
header("Location: http://$host$uri/$extra");
exit;
?>
A lot of the coding I am using has been taken from a tutorial so I'm not entirely if the use of relative URIs is completed being used in mine. I've posted the code from the page that deals with this area below. It uses the $_SERVER function at times but will it be using it on all pages. For example, at the bottom of each page I have this code or similar:

Code: Select all

<a href="?page=image-detail&album=' . $albumId . '&image=' . $row['im_id'] . '">
So basically my question is, based on the code I've posted are relative URIs being used for all my links.

Code: Select all

 
<?php
function getPagingLink($totalResults, $pageNumber, $itemPerPage = 10, $strGet = '')
{
    $pagingLink    = '';
    $totalPages    = ceil($totalResults / $itemPerPage);
    
    $numLinks      = 10;
 
    if ($totalPages > 1) {
        $self = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'] ;
 
        if ($pageNumber > 1) {
            $page = $pageNumber - 1;
            if ($page > 1) {
                $prev = " <a href=\"$self?pageNum=$page&$strGet\">[Prev]</a> ";
            } else {
                $prev = " <a href=\"$self?$strGet\">[Prev]</a> ";
            }   
                
            $first = " <a href=\"$self?$strGet\">[First]</a> ";
        } else {
            $prev  = ''; // we're on page one, don't show 'previous' link
            $first = ''; // nor 'first page' link
        }
    
        if ($pageNumber < $totalPages) {
            $page = $pageNumber + 1;
            $next = " <a href=\"$self?pageNum=$page&$strGet\">[Next]</a> ";
            $last = " <a href=\"$self?pageNum=$totalPages&$strGet\">[Last]</a> ";
        } else {
            $next = ''; // we're on the last page, don't show 'next' link
            $last = ''; // nor 'last page' link
        }
 
        $start = $pageNumber - ($pageNumber % $numLinks) + 1;
        $end   = $start + $numLinks - 1;        
        
        $end   = min($totalPages, $end);
        
        $pagingLink = array();
        for($page = $start; $page <= $end; $page++) {
            if ($page == $pageNumber) {
                $pagingLink[] = " $page ";   // no need to create a link to current page
            } else {
                if ($page == 1) {
                    $pagingLink[] = " <a href=\"$self?$strGet\">$page</a> ";
                } else {    
                    $pagingLink[] = " <a href=\"$self?pageNum=$page&$strGet\">$page</a> ";
                }   
            }
    
        }
        
        $pagingLink = implode(' | ', $pagingLink);
        
        $pagingLink = $first . $prev . $pagingLink . $next . $last;
    }
    
    return $pagingLink;
}
?>
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Re: using relative/absolute URI's

Post by RobertGonzalez »

Is there a question in here anywhere?
User avatar
Chalks
Forum Contributor
Posts: 447
Joined: Thu Jul 12, 2007 7:55 am
Location: Indiana

Re: using relative/absolute URI's

Post by Chalks »

Everah wrote:Is there a question in here anywhere?
I think he's just trying to figure out what the difference between a relative and absolute path is. I think?


An absolute path describes the location of a file from root. For insance:
"/public_html/admin/login.php"

A relative path describes the location of a file from the current directory. If I'm already in the root directory, saying:
"public_html/admin/login.php"
is exactly the same as using the absolute path. However, if I'm already in the admin folder the relative path to login.php is:
"login.php"
however, the absolute path to login.php is still the same as the first example given.

So, an absolute path will always be the same no matter what. A relative path will depend on where you are in the filesystem.
pkbruker
Forum Commoner
Posts: 32
Joined: Sun Aug 03, 2008 9:36 am
Location: Oslo, Norway

Re: using relative/absolute URI's

Post by pkbruker »

Absolute URIs

An absolute URI is the entire address of the page (or anything else on the web with and URI), i.e:

Code: Select all

http://www.mysite.com/something/index.php


Relative URIs
A relative URI is the address of the page relative to where you are at the moment. For instance: In http://www.mysite.com/index.php, you want to link to http://www.mysite.com/something/index.php. A relative URIwould look like this:

Code: Select all

something/index.php
It is highly recommended to use relative URIs within your site, as it can be easily moved around. For instance, if all your URIs are absolute, and your site is http://www.something.com, moving your site to http://www.anything.com would require you to change all URIs in your code. If they are relative, no URIs will have to be modified.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Re: using relative/absolute URI's

Post by RobertGonzalez »

If you write URIs in your output then it won't matter how your site is structured.
Post Reply