ProMike360 ProMike360 - 7 months ago 34
PHP Question

PHP fopen function only works one way

Ok let me explain what's goin one:

I have 2 hosts/websites, Host A and Host B. When i'm using the php function

code on Host A to Host B it works, i can read the title of the page. BUT when i go from Host B to Host A it won't work. And now the strange thing when i go from Host B to It does work??

I think that it's a wrong setting on Host A but, i can't change that much on the server. Does somebody know how to fix the problem? So i can go from Host B and read the title of a file on Host A?

Code that i use to open a file (and is hosted on Host B) and search the title:

$file = fopen ("", "r"); //This is HOST A
if ($file) {
echo "<p>Loadig remote file succesfull.</p>";
} else {
echo "<p>Unable to open remote file.</p>";
while (!feof ($file)) {
$line = fgets ($file, 1024);

/* This only works if the title and its tags are on one line */
if (preg_match ("@\<title\>(.*)\</title\>@i", $line, $out)) {
$title = $out[1];
echo $title;


I fixed it!

After a long time, i came with a solution for my problem.

But first, the problem

I have 2 hosts, Host A and Host B. I wanted to read a file on Host A by using a script (that is written in PHP and is using the fopen function) on Host B. This did not work. What did work, was reading a file that is hosted on Host B and read it by the script on Host A. (And no, the file-permissions was configured in the right way).
This is realy strange, and even at this moment i'm writing this. I still don't know how to solve it.

The solution

The solution to solve this problem, is using jQuery to request the file on Host A and add 1 row of php code to the file on Host A.

[PHP] The code on Host A what needs to be inside the php file.

header('Access-Control-Allow-Origin: *');

This says to your browser that you can go to this host. Otherwise the browser will block your request (This is what someone told me).

[jQuery/Javascript] The code that i used to read the file

$.get("", function( data ) {