Jump to content

using relative/absolute URI's


slaterino

Recommended Posts

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:

<?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:

 

 <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.

 

 
<?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;
}
?>

Link to comment
Share on other sites

The advice you have there is for 3xx redirects using a Location: header, not for links.  All browsers can handle relative links, but there may be some which cannot handle relative Location: headers (though I have never come across one that doesn't).

 

In other words, there's no need to use absolute URIs in your code.  You only need it when you call header("Location: ..."), and even then you don't need it for the major browsers.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.