RE: [PHP] Header
| From: | Cynic | Date: | Wed, 20 Dec 2000 22:29:20 +0000 |
| Subject: | RE: [PHP] Header | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-31331@lists.php.net to get a copy of this message | ||
Well, standards are a Good Thing(tm).
BTW, if anybody's concerned, I've written a function that
can take any form of relative or absolute, full or partial
path to redirect to, and redirects to a correct URL. It
relies on another useful function I'm including as well.
Here it is, quite commented (comments refer to the code
BELOW them).
Supposing I'd use it at http://www.wad.cz/foo/bar/script.php,
redirect( '../otherscript.php' ) will result in
header( 'Location: http://www.wad.cz/foo/otherscript.php'
),
redirect( './otherscript.php' ) and redirect( 'otherscript.php' )
in header( 'Location: http://www.wad.cz/foo/bar/otherscript.php'
)
... Etc.
function redirect( $input )
{
$buffer = '' ;
# won't work with CGI (I guess force-cgi-redirect might help, not sure
$self = $GLOBALS['HTTP_SERVER_VARS']['PHP_SELF'] ;
# ready to go, currently no additional checking is done
if( "http://" == substr( $input , 0 , 7 ) ) {
header( 'Location: ' . $input ) ;
# absolute path without a hostname (i. e. just prepend the hostname)
} elseif( '/' == $input[ 0 ] ) {
$buffer = $input ;
# parent.
} elseif( '../' == substr( $input , 0 , 3 ) ) {
# get the length of an initial segment composed solely of '.'s and
'/'s
# this, divided by 3, is the number of directory levels we'll climb up
# this isn't the most robust code I can think of, but will do for now
$up = strspn( $input , './' ) ;
# get the current directory level
$cur = substr_count( $self , '/' ) ;
# echo "\$cur: $cur<BR>\n" ;
# how deep from the documentroot will it end up?
# is the value legal? if not, trigger an error
$go = $cur - $up / 3 ;
# echo "\$go: $go<BR>\n" ;
if( $go <= 0 ) {
trigger_error( "Cannot go above the document root, clipping\n\t" ,
E_USER_WARNING ) ;
$go = 1 ;
} ;
# find the position of $go-th occurence of '/' in $self. that is: find
# the length of the initial segment of $self we'll use and fetch the segment
$pre = substr( $self , 0 , str_npos( $self , '/' , $go ) ) ;
# substr( $input , $up ) returns everything BUT the initial '../'s
$buffer = $pre . '/' . substr( $input , $up ) ;
# current directory ( './otherscript.php' )
# faster than substr( $input , 0 , 2 ) ;
} elseif( './' == $input[ 0 ] . $input[ 1 ] ) {
$buffer = dirname( $self ) . substr( $input , 1 ) ;
# current directory ( 'otherscript.php' )
} else {
$buffer = dirname( $self ) . '/' . $input ;
}
header( 'Location: ' . 'http://' .
$GLOBALS['HTTP_SERVER_VARS']['SERVER_NAME'] . $buffer ) ;
}
# find position of n-th occurence of $needle in $haystack
function str_npos( $haystack , $needle , $occurrence )
{
if( $occurrence > substr_count( $haystack , $needle ) ) {
return false ;
}
for( $i = 0 , $offset = -1 ; $i < $occurrence ; $i++ ) {
$offset = strpos( $haystack , $needle , $offset + 1 ) ;
}
return $offset ;
}
At 21:55 20.12. 2000, Dave VanAuken wrote the following:
--------------------------------------------------------------
>I agree, and this is the primary reason why Javascript is barely a
>tool in my opinion - the fact that half of its implementations don't
>work on half the browsers...
>
>We also need to remember though that all these standards are simply
>RFC's (Request For Comments) meaning that they are driven by feedback
>and general concensus... The reason that we are still not limited to
>HTML 0 is because of people pushing standards through the RFC
>process(good) and browser monoliths from pushing THEIR own standards
>(maybe not so good) due to market pull.
>
>Thus my comment, not judging the right or wrong or correctness of
>nature, as its not my place to determine if someone should code their
>site with code that only works on MSIE 7.0a codename "never work" or
>elects to be HTML 1, 1.1 and so forth complient.
>
>Dave
>
>-----Original Message-----
>From: Cynic [mailto:cynic@mail.cz]
>Sent: Wednesday, December 20, 2000 2:44 PM
>To: Dave VanAuken; php-general@lists.php.net
>Subject: RE: [PHP] Header
>
>
>Ever heard of WaSP? It's a group of people pushing HTML UA
>vendors towards standards compliance. Because noncompliance
>is what created the web hell where HTML people must have
>a different version of a HTML page for every browser their
>audience uses.
>Noncompliance with HTTP 1.1 is what brought us coders problems
>with proxies.
>
>header( "Location: ../blah/index.html") ;
>might work with all current browsers, because they're not
>compliant with the HTTP protocol. Next time you scream in
>anger because of lack of support for standard X in browser Y,
>rest assured it's not that the programmers played Minesweeper.
>They spent the time they should've been implementing proper
>web standards support coding around your laziness instead.
>
>No offense intended, I'm just...
>
>
>At 20:05 20.12. 2000, Dave VanAuken wrote the following:
>--------------------------------------------------------------
>>No, but it isn't "required" for the function to perform as intended.
>>
>>Stating what is proper to be compliant and stating what is required
>>are two different things.
>>
>>Dave
>>
>>-----Original Message-----
>>From: Cynic [mailto:cynic@mail.cz]
>>Sent: Wednesday, December 20, 2000 12:49 PM
>>To: mathias rockel; Dan Lowe; php-general@lists.php.net
>>Subject: Re: [PHP] Header
>>
>>
>>Is being standards-compliant such a PITA?
>>
>>
>>At 18:41 20.12. 2000, mathias rockel wrote the following:
>>--------------------------------------------------------------
>>>Hi!
>>>
>>>Although it is it seemed to me every browser I tried could use
>>relative urls
>>>as a location header. anyone made different experiences ?
>>>
>>>mats
>>>----- Original Message -----
>>>From: "Dan Lowe" <dan@tangledhelix.com>
>>>To: <php-general@lists.php.net>
>>>Cc: <php-general@lists.php.net>
>>>Sent: Wednesday, December 20, 2000 1:08 AM
>>>Subject: Re: [PHP] Header
>>>
>>>
>>>> Previously, Diogo Zulli said:
>>>> > On [15 December 2000 18:32] (Cynic), wrote:
>>>> >
>>>> > } According to the HTTP protocol, the URL must be absolute.
>>>> >
>>>> > Does this means that
>>>> > "http://hostname/" should be on "Location:
>"
>>>> > or just the /absolute/pathname must be there?
>>>>
>>>> Must be fully qualified with protocol, host, and anything extra,
>so
>>>> yes, http://hostname/extra/ is needed.
>>>>
>>>> See section 14.30 of RFC 2616 (HTTP 1.1) or section 10.11 of RFC
>>1945
>>>> (HTTP 1.0).
>>>>
>>>> The Location response-header field defines the exact location
>>of the
>>>> resource that was identified by the Request-URI. For 3xx
>>responses,
>>>> the location must indicate the server's preferred URL for
>>automatic
>>>> redirection to the resource. Only one absolute URL is allowed.
>>>>
>>>> Location = "Location" ":" absoluteURI
>>>>
>>>> An example is
>>>>
>>>> Location:
>>>> http://www.w3.org/hypertext/WWW/NewLocation.html
>>>>
>>>> -dan
>>>>
>>>> --
>>>> Dan Lowe <dan@tangledhelix.com>
>>>> http://tangledhelix.com/