...

View Full Version : When a header("Location: $url") doesn't work



jcma
01-20-2003, 12:25 PM
In some point of my code i have a redirection to a diferent page.


The problem is that sometimes that page doesn't exist.
I want to detect that error, log it and fix it.

In the manual the function definition is


int header ( string string [, bool replace [, int http_reponse_code]])

but when i call the function it never returns anything.

i'm running php 4.2.2


How can i detect if the page doesn´t exist? i've thougt of fopen but in that case it has to connect twice to page.
Can someone help... thanks

firepages
01-20-2003, 12:53 PM
well apache should redirect you to your 404 error page?

but I suppose you can check.....


<?
if(file_exists($location)){ //or is_file//
header("location:$location");
}
?>

jcma
01-20-2003, 01:01 PM
the problem is that $location is a remote file /address and those file function do not work remotely

i think it will only work with fopen....

and what does header() return?

Dylan Leblanc
01-20-2003, 10:07 PM
The curl() function might help with this, http://www.php.net/manual/en/ref.curl.php .

mr_ego
01-23-2003, 12:06 PM
the header tag MUST be executed before anything else



<?php

// Will Work:
header("Location: $location");

// Wont Work:
echo "<html>";
header("Location: $location");

?>

mordred
01-23-2003, 01:09 PM
Originally posted by jcma
and what does header() return?

Good question acutally, and I couldn't find anything related to that in the manual (haven't looked closely enough probably). Anyway, I doubt that it returns the status code of a page the client fetches after receiving a header("location: "). Because this doesn't make sense to me - how should the PHP function immediately know of the result? The page requested by the browser could go down just in this microsecond and the client (browser) does not communicate synchronously with the server(PHP). It seems to me that the int returned denotes whether the sending of the header, i.e. the correct execution of the header() function, was successful.

Your only options are to check if the file exists prior to redirecting the browser to it. You can do that with fopen(), the cURL functions, or use a socket (if you really want to do that very low-level), as others have told you already.

jcma
01-23-2003, 01:25 PM
i haven't used cUrl because i havent installed it.
but i tried fopen and fsockopen(or something like that) and they can't open remote files. but do open localhost files.

maybe because i'm behind a firewall, but don't they use port 80? when i used fsockopen i told it to use :80.

is this just my problem??

this open a file and reads its title


$file = fopen ("http://$url", "r");
echo "Done<br>";
if (!$file)
{
echo "<p>Unable to open remote file.\n";
exit;
}
while (!feof ($file))
{
$line = fgets ($file, 1024);
// /* This only works if the title and its tags are on one line
if (eregi ("<title>(.*)</title>", $line, $out))
{
$title = $out[1];
break;
}
}
echo $title;
fclose($file);



and this one just tries to open a file(i think)




$up = fsockopen("www.google.com", 80, &$errno, &$errstr, 30);
if($up)
{
echo "ok";
}
else
echo "$errsts";

and outputs Bad file descriptor


none of this code was made by me, so i think it's my problem....
:confused: :confused: :confused:

jcma
01-23-2003, 02:27 PM
i've just tested the fopen code above on my firewall /webserver and the code works perfectly..... :) :(
it's good because it works and it's bad because i wanted it to work on my pc (behind the firewall/webserver).

is there a way to solve this problem?

i've seen a piece of code with fsockopen to connet to a site throuh a proxy, but my server doesn't have a proxy...



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum