Jump to content

new to php... text includes and error handling?


_abl_

Recommended Posts

I'm trying to dynamically pull text from a remote server, into an

existing local web page. I have a local text file as a default, just in case.

 

However, when I use this code:

 

 

<?php

 

$str = file_get_contents("http://www.remote.com/text/index.txt", false);

if ($str) {

  echo $str;

} else {

  require_once("default.txt");

}

 

?>

 

…and the remote file is missing, I still get an ugly long string of warning/error code in the web page- and then it includes the default.txt after the error code. What can i do to make sure the warning/error does not print in the browser at all? (I am brand spanking new to php, so please, go slow)

 

thanks for any help–

 

abl

 

also, to make your site more secure and to prevent easy hacks, the first line of code should be

 

error_reporting(0);

 

When testing pages just comment it out and find the errors, but you don't want people to know the root/dir tree on your server, very bad things can happen.

Turning off error reporting prevents the php core from describing the error in the docroot of the page. Once somebody knows the directory tree of your site, they could sniff for things like include directories and get passwords and stuff. Error reporting is good for developement but if your site is live and running and an error is reporting something like:

 

mysql syntax is wrong home/username/includes/config.php on line 12 near '1'

 

You would not want people to know where private files are and how you write your scripts, makes you vulnerable to attacks by hackers.

You're not completely getting rid of the problem, just hiding it. Here's how to fix it:

 

$file = 'http://www.remote.com/text/index.txt';
if (file_exists($file)) {
str = file_get_contents("$file", false);
echo "$str";
}else{
require_once("default.txt");
}

 

  i was told that file_exists() won't work for files on a remote server, that it was designed for local server files only.

 

which explains why when i attempted to use file_exists() it automatically used my default.txt, every time.

 

or are you saying that by assigning the remote server url as the value of the variable, that it will allow me to use the file_exists() anyway?

Archived

This topic is now archived and is closed to further replies.

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