Page 1 of 2

Script not working on PHP5 (but did in php4)

Posted: Fri Jun 02, 2006 7:11 pm
by wildcolour
Hi

I am very new to this php stuff (well programming an linux generally) I have this bit of code (that grabs and then displays part of a weather web site on my site -- I do have there permission to do this)

Since I have upgrade to php5 this does not work (nor do a few other scripts I have for rendering rss) I am guessing that this is a problem with the way I have set up my environment. (when I upgraded to php5 I also upgraded to FC4 and changed servers, so lots have changed)

so here is the code

Code: Select all

<link rel="stylesheet" type="text/css" href="http://www.bom.gov.au/standard/stylestd.css">
<link rel="stylesheet" type="text/css" href="http://www.bom.gov.au/standard/datatable.css">
<?
$url="www.bom.gov.au/products/IDN65188.shtml";  //replace with the full URL you wish to fetch the data from

preg_match("/^(https?:\/\/)?([^\/]*)(.*)/i", "$url", $matches);
$domain = "http://" . $matches[2];
$page = $matches[3];

$fd = fopen($domain.$page, "rb");
$code = "";
while(!feof($fd)){
	$code .= fread($fd, 4096);	
}
fclose($fd);

$start= strpos($code, "</div>");  // replace with your STARTING POINT source of HTML page
$finish= strpos($code, "<!--************* START OF STANDARD BUREAU FOOTER **************-->");  // replace with your END POINT source of HTML page 
$length= $finish-$start;
$code=substr($code, $start, $length);

$newcode = preg_replace("/(href=\"?)(\/[^\"\/]+)/", "\\1" . $domain . "\\2", $code);

echo $newcode;
?>
it is here http://www.wildwalks.com/nswobs.php

and php setting are here
http://www.wildwalks.com/test1.php

I have read through a lot of forums over the last two days trying to solve this, but I am stuck

any ideas will be greatly appreciated. -- thanks

Matt :)

Posted: Fri Jun 02, 2006 7:35 pm
by feyd
I'm going to guess short tags are off.. since your server appears to be not responding, I can't test that theory myself..
Run the following in a new file and tell us the results please.

Code: Select all

<?php

$neg = array(0, false, '', null, 'off');
$ve = phpversion();
$os = PHP_OS;
$er = intval(error_reporting());
$rg = (in_array(strtolower(ini_get('register_globals')), $neg) ? 'Off' : 'On');
$de = (in_array(strtolower(ini_get('display_errors')), $neg) ? 'Off' : 'On');
$so = (in_array(strtolower(ini_get('short_open_tag')), $neg) ? 'Off' : 'On');
$le = '';
$cli = (php_sapi_name() == 'cli');
$eol = ($cli ? "\n" : "<br />\n");

$gle = get_loaded_extensions();
$rows = array();
$wide = 4;
$j = count($gle);
$pad = $wide - $j % $wide;
$len = max(array_map('strlen', $gle));
$func = create_function('$a', 'return str_pad($a, ' . intval($len) . ');');
$gle = array_map($func, $gle);
for($i = 0; $i < $j; $i += $wide)
{
    $le .= '   ' . implode('   ', array_slice($gle, $i, $wide)) . "\n";
}
if ($cli)
{
     $le = $eol . $le;
}
else
{
 $le = '<pre>' . $le . '</pre>';
}

$ec = array(
   'E_STRICT' => 2048, 'E_ALL' => 2047, 'E_USER_NOTICE' => 1024,
   'E_USER_WARNING' => 512, 'E_USER_ERROR' => 256, 'E_COMPILE_WARNING' => 128,
   'E_COMPILE_ERROR' => 64, 'E_CORE_WARNING' => 32, 'E_CORE_ERROR' => 16,
   'E_NOTICE' => 8, 'E_PARSE' => 4, 'E_WARNING' => 2, 'E_ERROR' => 1,
);

$e = array();
$t = $er;
foreach ($ec as $n => $v)
{
   if (($t & $v) == $v)
   {
      $e[] = $n;
      $t ^= $v;
   }
}
$er = $er . ' (' . implode(' | ', $e) . ')';

if (!$cli)
{
  echo '<html><head><title>quick info</title></head><body>' . "\n";
}

echo 'PHP Version: ' . $ve . $eol;
echo 'PHP OS: ' . $os . $eol;
echo 'Error Reporting: ' . $er . $eol;
echo 'Register Globals: ' . $rg . $eol;
echo 'Short Tags: ' . $so . $eol;
echo 'Display Errors: ' . $de . $eol;
echo 'Loaded Extensions:' . $le . $eol;

if (!$cli)
{
  echo '</body></html>' . "\n";
}

?>

Posted: Sat Jun 03, 2006 6:49 am
by wildcolour
Hi Feyd

Thanks for your very quick response.

Looks like those short tags are on ??

I have restarted the server a couple of times - guessing that is why it was down when you tried, should be fine now

the out put of that file is
PHP Version: 5.0.4
PHP OS: Linux
Error Reporting: 2047 (E_ALL)
Register Globals: On
Short Tags: On
Display Errors: Off
Loaded Extensions:
yp xml wddx tokenizer
sysvshm sysvsem sysvmsg standard
SPL sockets SimpleXML shmop
session pspell posix pcre
mime_magic iconv gmp gettext
gd ftp exif dio
dbx curl ctype calendar
bz2 zlib openssl libxml
apache2handler bcmath dba dom
imap ldap mbstring mysql
mysqli ncurses odbc pgsql
snmp soap xmlrpc xsl
and can be found here
http://www.wildwalks.com/feyd.php

thanks again for looking at this for me.

Matt :)

Posted: Sat Jun 03, 2006 7:17 am
by Chris Corbyn
You need allow_url_fopen on in php.ini for that script to do anything.

Posted: Sat Jun 03, 2006 7:28 am
by wildcolour
thanks d11wtq

Good thinking but

allow_url_fopen is already on in my php.ini file

any other ideas??

Matt :)

Posted: Sat Jun 03, 2006 4:55 pm
by Hades
Turn Display Errors on and see what errors the script outputs with error reporting set to E_ALL.

That should give you a nudge in the right direction :)

Posted: Sat Jun 03, 2006 6:08 pm
by ok
Try open the remote file using "r" instead of "rb".

Posted: Sat Jun 03, 2006 6:50 pm
by wildcolour
Hades wrote:Turn Display Errors on and see what errors the script outputs with error reporting set to E_ALL.

That should give you a nudge in the right direction :)
Thanks done that - wow that is a lot of errors

this is what is said

Warning: fopen(http://www.bom.gov.au/products/IDN65188.shtml) [function.fopen]: failed to open stream: Permission denied in /var/www/html/wildwalks/nswobs.php on line 20

Warning: feof(): supplied argument is not a valid stream resource in /var/www/html/wildwalks/nswobs.php on line 22

Warning: fread(): supplied argument is not a valid stream resource in /var/www/html/wildwalks/nswobs.php on line 23

This does not mean much to me. (these last two warning got repeated many times on the page)

I tried using wget http://www.bom.gov.au/products/IDN65188.shtml on the server and that worked fine.

Looks like we must be getting close on this?? -any ideas

(BTW I turned of error reporting again - I obviously have some work to do on the rest of my site :) )

Posted: Sat Jun 03, 2006 6:52 pm
by wildcolour
ok wrote:Try open the remote file using "r" instead of "rb".
Thanks - did this

http://www.wildwalks.com/nswobs-b.php

(not sure why I called it -b should have been -r) - must be tired

- same error though

??

Posted: Sat Jun 03, 2006 8:53 pm
by Hades
It looks like it's simply failing to open the file. Do you still have a PHP4 version working?

Check that the file it is trying to open actually exists.

Other than that, all I can think of is it could only be some kind of restriction on the files end of things. Perhaps that server restricting access to the file in some way?

Posted: Sat Jun 03, 2006 9:09 pm
by wildcolour
Hum here is exactly the same script on another server

http://www.paraglide.com.au/fly/custom/nswobs.php

and its php info

http://www.paraglide.com.au/test1.php

I can wget http://www.bom.gov.au/products/IDN65188.shtml on the server fine and the file arrives fine - so the server seems to have access to the file - but are there any other permissions that may be restricing PHP access??

THanks again

Posted: Sat Jun 03, 2006 10:08 pm
by wildcolour
hum not sure what this means

Here i used wget command to get the file and copy it locally, then modified copied and modified the same script to look at the file locally and it works

see
http://www.wildwalks.com/localobs.php


My server is sitting in a data center - is it possible for them to set up a firewall or proxy server or something like that that would mean that wget can get the file but fopen can't ?????

or am I barking up the wrong tree

any ideas??

Posted: Sat Jun 03, 2006 10:26 pm
by Burrito
this should be your clue:
Warning: fopen(http://www.bom.gov.au/products/IDN65188.shtml) [function.fopen]: failed to open stream: Permission denied in /var/www/html/wildwalks/nswobs.php on line 20

Posted: Sat Jun 03, 2006 10:33 pm
by wildcolour
Burrito wrote:this should be your clue:
Warning: fopen(http://www.bom.gov.au/products/IDN65188.shtml) [function.fopen]: failed to open stream: Permission denied in /var/www/html/wildwalks/nswobs.php on line 20
Thanks Burrito

But I am new to this game and am not sure what this means

so the line is
$fd = fopen($domain.$page, "rb");

SO I am assuming that it is fopen() is not able to see the file - but I don't know why

any ideas??

Posted: Sun Jun 04, 2006 8:06 am
by Hades
I looked at your two phpinfo files and the only real difference I can see other than the obvious php version is that on the new server you have a temp dir defined. On the old php4 server you didn't.