0

Possible Duplicate:
including php file from another server with php

i want to have my own error system instead of having the php errors so for example i require a file from another server and that server is not available right now

<?php
require 'http://example.com/file.php' or die ("that host is not available right now");
?>

but instead i get

Warning: require(1) [function.require]: failed to open stream: No such file or directory in C:\xampp\htdocs\index.php on line 5

Fatal error: require() [function.require]: Failed opening required '1' (include_path='.;C:\xampp\php\PEAR') in C:\xampp\htdocs\index.php on line 5

Community
  • 1
  • 1
Eli Y
  • 877
  • 16
  • 41

2 Answers2

6

It's because require 'foo' or bar() is interpreted as require ('foo' or bar()). 'foo' or bar() equals true, i.e. 1. If you want to write it like this, use different parentheses:

(require 'http://example.com/file.php') or die ("that host is not available right now");

But, you don't need die at all here, since require will already halt program execution if the required file can't be loaded. Just require 'http://example.com/file.php'; will do fine. Whether you should actually load foreign PHP files over the network is another story (hint: probably not).

deceze
  • 510,633
  • 85
  • 743
  • 889
  • (Responding to removed comment:) No, because `require` already stops program execution! There is nothing *after* a failed `require`. If you want custom error handling, override the error handler instead. – deceze Oct 09 '11 at 01:41
  • i'm sorry for the dumb question (my guess) how do i do this ? – Eli Y Oct 09 '11 at 01:45
  • Look at http://php.net/manual/en/function.set-error-handler.php – deceze Oct 09 '11 at 01:48
  • Use the include function instead (it doesn't throw a fatal error). And remember the precedence of operators. – Jaison Erick Oct 09 '11 at 01:51
  • @Jaison For one-off includes that's a good solution. For a global "custom error handler" he should override the error handler though. – deceze Oct 09 '11 at 01:53
  • @deceze I agree with you. But for a particular check, this gonna work. – Jaison Erick Oct 09 '11 at 01:56
3

The problem is the precedence of operators. The PHP are including the result of the "or" comparison (true). Try to remove this.

include('http://...') or die('error...');

It will work.

Jaison Erick
  • 655
  • 5
  • 11
  • if i remove the or i get other errors – Eli Y Oct 09 '11 at 01:51
  • If you want to custom check your errors, use the include function instead of the require statement. And if you are getting errors, read the other answers. Turn on allow_url_include directive. – Jaison Erick Oct 09 '11 at 01:55