_abl_ Posted March 29, 2007 Share Posted March 29, 2007 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 Link to comment https://forums.phpfreaks.com/topic/44747-new-to-php-text-includes-and-error-handling/ Share on other sites More sharing options...
Orio Posted March 29, 2007 Share Posted March 29, 2007 @ Orio. Link to comment https://forums.phpfreaks.com/topic/44747-new-to-php-text-includes-and-error-handling/#findComment-217283 Share on other sites More sharing options...
rcorlew Posted March 29, 2007 Share Posted March 29, 2007 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. Link to comment https://forums.phpfreaks.com/topic/44747-new-to-php-text-includes-and-error-handling/#findComment-217293 Share on other sites More sharing options...
_abl_ Posted March 29, 2007 Author Share Posted March 29, 2007 thanks Orio, I'm going to work on getting that to work for me. rcorlew, could you explain what you mean, and what the benefit is of using the error_reporting(0); function you described? thanks, abl Link to comment https://forums.phpfreaks.com/topic/44747-new-to-php-text-includes-and-error-handling/#findComment-217523 Share on other sites More sharing options...
rcorlew Posted March 29, 2007 Share Posted March 29, 2007 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. Link to comment https://forums.phpfreaks.com/topic/44747-new-to-php-text-includes-and-error-handling/#findComment-217589 Share on other sites More sharing options...
_abl_ Posted March 29, 2007 Author Share Posted March 29, 2007 gotcha that makes perfect sense- thanks. gotta deal with my regular job duties, but as soon as i get a second i will try this (these) out. Link to comment https://forums.phpfreaks.com/topic/44747-new-to-php-text-includes-and-error-handling/#findComment-217601 Share on other sites More sharing options...
_abl_ Posted March 30, 2007 Author Share Posted March 30, 2007 shutting off error reporting with error_reporting(0); worked perfectly. thanks Link to comment https://forums.phpfreaks.com/topic/44747-new-to-php-text-includes-and-error-handling/#findComment-217902 Share on other sites More sharing options...
Lytheum Posted March 30, 2007 Share Posted March 30, 2007 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"); } Link to comment https://forums.phpfreaks.com/topic/44747-new-to-php-text-includes-and-error-handling/#findComment-217911 Share on other sites More sharing options...
_abl_ Posted March 30, 2007 Author Share Posted March 30, 2007 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? Link to comment https://forums.phpfreaks.com/topic/44747-new-to-php-text-includes-and-error-handling/#findComment-217932 Share on other sites More sharing options...
joshi_v Posted March 30, 2007 Share Posted March 30, 2007 Yes. By passing remote server url u can check the whether file existed or not. Joshi. Link to comment https://forums.phpfreaks.com/topic/44747-new-to-php-text-includes-and-error-handling/#findComment-217943 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.