MyBB Community Forums

Full Version: fsockopen() problem
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Okey, so I've got this script running on createmybb.com in order to use the MyBB function postify().

<?php
$fp = @fsockopen("www.createmybb.com", 80, $errno, $errstr, 30);
if (!$fp) {
	echo "News is unavailable at this time. Refreshing may fix the problem<br />";
  //echo "$errstr ($errno)<br />\n";
} else {
   $out = "GET /support/news.php HTTP/1.0\r\n";
   $out .= "Host: www.createmybb.com\r\n";
   $out .= "Connection: Close\r\n\r\n";

   fwrite($fp, $out);
   while (!feof($fp)) {
       echo fgets($fp, 128);
   }
   fclose($fp);
}
?> 

First it reads news.php in the forums directory. In news.php it fetches the threads from a specificed forum and postify()'s them in order to allow MyCode and such, then it is echo'ed to the site: www.createmybb.com.

Now theirs no errors, but You will see something like: "HTTP/1.1 200 OK Date: Wed, 08 Feb 2006 02:24:29 GMT Server: Apache X-Powered-By: PHP/4.4.1 Set-Cookie: mybb[lastvisit]=1139365469; expires=Thu, 08 Feb 2007 02:24:29 GMT; path=/ Set-Cookie: mybb[lastactive]=1139365469; expires=Thu, 08 Feb 2007 02:24:29 GMT; path=/ Set-Cookie: sid=*******************; path=/ Connection: close Content-Type: text/html"


which is the result of headers. How can I remove that?
Nevermind I just used this code instead:

$handle = fopen("http://www.createmybb.com/support/news.php", "rb"); 
$contents = '';
while (!feof($handle)) {
  $contents .= fread($handle, 8192);
}
fclose($handle);
echo $contents;

Thanks to LitraSoft for the fix Wink
Why don't you just include it? Confused
Its in a different directory, and news.php also has includes.

Because I cant do include "./support/global.php" because it will wont look for it in the /support/ directory. It will only look for it in the main directory, of which it isnt their!
I think Dennis was asking why you didn't
include("http://www.createmybb.com/support/news.php");
because news.php includes other includes!

So what it would do is something like this:

####INDEX.PHP CODE######

include("http://www.createmybb.com/support/news.php");

####INDEX.PHP CODE END####


now, when it compiles it on run time it would look something like this

###INDEX.PHP CODE#####
###NEWS.PHP CODE####
require "./global.php";

ect....
####NEWS.PHP CODE END####
####INDEX.PHP CODE END####


Now because news.php is in the ./support/ directory, and index.php is in the MAIN directory, is will look for "./global.php" in the MAIN directory, NOT the /support/ directory.

And if you change the code to "require './support/global.php';" there are other includes in GLOBAL.PHP that dont define the directory './support/' so it will think those files are in the main directory still! Therefore making you changing EVERY INCLUDE or REQUIRE in all of MYBB!

This way just gets html (the already parsed php), NOT the SOURCE therefore avoiding all of those errors and changes needed to be done.

Perhaps the developers could take a look at this issue and try and find a work-around. such as doing require $mybb->settings['bburl'].'/filehere.php' - instead of just require "./filehere.php";

Get my flow?
The best way to do it is like phpBB. Have a define called ROOT_DIR. This would be defined before anything else. Then when you need to include a file you use "ROOT_DIR . 'news.php'". In your file (say forums.php) you'd specify ROOT_DIR to be "./", and if in admin dir ROOT_DIR would be "../". Very easy and elegant way to do it.

The problem with using $mybb->setings['bburl'] is that the link will have "http://" at the start. On some hosts, using "http://" to link to files is disabled for security. So people can't try including files on other servers. Now I'm not sure if the server will work out that you are pointing to a file on your server but using a URL instead of a path, but it's not worth risking it.

And fyi, it might be a bad idea to use absolute URLs in your files. You change the location of one file and you might have to edit it in lots of files. Not a biggy but might cause some problems if you forget.

Still think phpbb's method is quite nice Smile.
You need to have the absolute path, or else it will fetch the source rather than the output