[PHP]Reading off a html file and.. appending it to a document..

SineCosine

I'm still looking for my Tangent
Reaction score
77
Yea, assuming the html file I wanna' read off and the php file are in the same domain..
How would I read off the html file and 'echo' it?

Because I just realized that this site I'm making has the same structure all over and felt that it would be tedious if I were to make a change..
Meaning, I would have to modify all <insert number here> of my web-pages to match the new change.

But If I could make all of the php files read off one single html file..
Life would be easier.

So..
How do I do it?
 

SineCosine

I'm still looking for my Tangent
Reaction score
77
What's this?
Note: If you're opening a URI with special characters, such as spaces, you need to encode the URI with urlencode().

Spaces?
So I need to do this?

urlencode(file_get_content('SomeHTML.html')) ?

[EDIT]
Wow, it worked.
I used file_get_content('SomeText.txt');
Then I echo'd it.

One problem.
Inside my txt file, I have some php inside..
And that led to garbled echoes only for the php part.
Everything else that was not php was fine.
Code:
		<table align="right">
			<tr>
				<?php
					include('Config.php');
					$SESSION = $_SESSION['Username'];

					if ($SESSION) {
						echo 'weird';
					} else {
						$ECHO = "<td class=&Links& onMouseOver=&this.style.backgroundColor = '#FAEBD7'; this.style.fontColor = '#00FF00';& onMouseOut=&this.style.backgroundColor='#FFCCCC'; this.style.fontColor='#3300FF';&>";
						$ECHO = str_replace('&', '"', $ECHO);
						echo  $ECHO;
						$ECHO2 = "_Log-In_";
						echo $ECHO2;
						$ECHO3 = "</td>";
						echo $ECHO3;
					}
				?>
				
			</tr>
			
			<tr>
				<?php
					if ($SESSION) {
						echo 'weird';
					} else {
						$ECHO4 = "<td class=&Links& onMouseOver=&this.style.backgroundColor = '#FAEBD7'; this.style.fontColor = '#00FF00';& onMouseOut=&this.style.backgroundColor='#FFCCCC'; this.style.fontColor='#3300FF';&>";
						$ECHO4 = str_replace('&', '"', $ECHO4);
						$ECHO5 = "_Register_";
						$ECHO6 = "</td>";

						echo $ECHO4 . $ECHO5 . $ECHO6;

					}

				?>
			</tr>
		</table>
That, turned into this:
Code:
"; $ECHO = str_replace('&', '"', $ECHO); echo $ECHO; $ECHO2 = " Log-In "; echo $ECHO2; $ECHO3 = ""; echo $ECHO3; } ?>  "; $ECHO4 = str_replace('&', '"', $ECHO4); $ECHO5 = " Register "; $ECHO6 = ""; echo $ECHO4 . $ECHO5 . $ECHO6; } ?>

Parts of the PHP got chopped off =/
 

Lyerae

I keep popping up on this site from time to time.
Reaction score
105
Aside from removing the PHP from the file, you could parse through it and remove any found PHP code.
 

SineCosine

I'm still looking for my Tangent
Reaction score
77
I tried something funny and it worked ._.
include('SomeWebDocument.txt');

Worked like a charm, flawless xD
Thanks for the help, anyway.
I may find a use for fwrite >.>
 

codemonkey

Code monkey not crazy, just proud.
Reaction score
66
I tried something funny and it worked ._.
include('SomeWebDocument.txt');

Worked like a charm, flawless xD
Thanks for the help, anyway.
I may find a use for fwrite >.>

Bad bad bad. include() grabs the files content then executes any PHP it may have. So if this document could be user controlled they could delete all the files on your server.

Use echo file_get_contents(). Not only is it safe but it's faster.
 

SineCosine

I'm still looking for my Tangent
Reaction score
77
How would I make it non-user controlled, then? o.0
And.. Umm..

I do want the php files to execute in the file that I am 'grabbing' =x

Oh, And..
What if I want this other site to be able to access a certain table to get a value.
How would I allow it?

Like, this site has a javascript that goes:
Code:
var SomeInteger = //This is where I need it to get the value from a table in my site =x
                               //But this javascript is not running on my site, it's running in another site, so... Yea, stuck

//Do stuff with Integer
 

codemonkey

Code monkey not crazy, just proud.
Reaction score
66
>How would I make it non-user controlled, then? o.0

I meant if users could change it :p

__

You can't change another sites JavaScript...
 

SineCosine

I'm still looking for my Tangent
Reaction score
77
Users can't change it >;(
Lol.

Really, they can't.

Nonono, I don't wanna' change another site's javascript.
Like, there's this site that wants to access data from my site.
I also want that site to access data from my site.
(We are in agreement, basically)

So, that site wants to display a value retrieved from a MySQL table from my site.
Using javascript.

How would that be done? o.0

[EDIT]
I don't wanna' read.. =/
Wait-
You mean if I read a php..
And it echos something, I'll only get the echo'd stuff and not the actual php codes?

[EDIT=2]
Umm..
Code:
var Stuff = <?php 

echo readfile('http://SomeWbsite/Some.php');

?>;

Did not work for me =/
I even tried
Code:
var Stuff = <?php

readfile('http://SomeWebsite/Some.php');

?>;
and:
Code:
var Stuff = <?php

$Something = readfile('http://SomeWebsite/Some.php');
echo $Something;

?>;

I'm doing something wrong, aren't I? =x
 

SineCosine

I'm still looking for my Tangent
Reaction score
77
Change the value of a javascript variable to the value stored in a MySQL database.

[EDIT]
I tried making the PHP output a javascript.
Code:
echo '<script type="text/javascript">';
echo 'var Test = 9001';
echo '</script>';

And I tried to do this:
Code:
<?php
$Test = readfile('http://Something.com/Test.php');
echo $Test;
?>

Failed.

So I tried:
Code:
echo '%script type="text/javascript"^';
echo 'var Test = 9001';
echo '%/script^';

Code:
<?php
$Test = readfile('http://Something.com/Test.php');
$Test = str_replace('%', '<', $Test);
$Test = str_replace('^', '>', $Test);
echo $Test;
?>

Still failed =/

[EDIT=2]
Weird..
I tried to create a new php file and placed it on my desktop (For testing).
And inside, I wrote:
Code:
<?php
    echo 'Testing, Test';
?>

When I opened it in Mozilla, no output was found ._.
Weird..

[EDIT=3]
But placing it in my web-server then opening it up generated the expected behaviour.
Does that mean I can't run PHP pages if they're on my desktop? o.0
 

Lyerae

I keep popping up on this site from time to time.
Reaction score
105
To get the file contents:
PHP:
<?php

$url = ' '; // Enter URL here.
$str = file_get_contents($url);

echo $str;

?>

Load the file contents into the page:
Code:
var scriptLocation = 'index.php'; // Change to the location of 'index.php'

if (window.XMLHttpRequest) {
	httpObj = new XMLHttpRequest();
} else
	httpObj = new ActiveXObject('Microsoft.XMLHTTP');
}

httpObj.onreadystatechange = function() {
	if (httpObj.readyState == 4 && httpObj == 200) {
		document.getElementById('toChange').innerHTML = httpObj.responseText;
	}
}

httpObj.open('GET', scriptLocation, true);
httpObj.send();

No promises on whether this actually works. LAMPP isn't working right, and it takes way to long to upload to my server with my current internet connection.
 

SineCosine

I'm still looking for my Tangent
Reaction score
77
Code:
var scriptLocation = 'index.php'; // Change to the location of 'index.php'

if (window.XMLHttpRequest) {
	httpObj = new XMLHttpRequest();
} else
	httpObj = new ActiveXObject('Microsoft.XMLHTTP');
}

httpObj.onreadystatechange = function() {
	if (httpObj.readyState == 4 && httpObj == 200) {
		document.getElementById('toChange').innerHTML = httpObj.responseText;
	}
}

httpObj.open('GET', scriptLocation, true);
httpObj.send();

I actually tried this, but it never worked.
The readystate was never 0, 1, 2, 3 or 4 and the status never was 200 or 404
Meaning, no output responseText was ever created

Really confusing me =x

I guess it's got to do with cross domain thingy =x
 

Lyerae

I keep popping up on this site from time to time.
Reaction score
105
Yeah, JavaScript (and AJAX) can't access data remotely, hence the need for the PHP script.
 

SineCosine

I'm still looking for my Tangent
Reaction score
77
Wait--
You mean create 2 PHP files?
One for the other server..
One for my server?

The JS calls the other php, that php reads from my php..
And returns a value?

[EDIT]
So..
Do you know why PHP files won't run on my desktop? o.0
Even a simple:

<?php echo 'Hello, World!'; ?>

Fails and does not return an output =/
 

Lyerae

I keep popping up on this site from time to time.
Reaction score
105
Yeah, you can't run PHP like that.
You need to run it on a server. If you don't have a server, you can always use XAMMP.

And you only need one PHP page.
PHP can fetch data from a remote server. JavaScript can't.

You use the PHP to get the contents of the file, and you use an AJAX request to get the data from the PHP page and display it on the page.
 
General chit-chat
Help Users
  • No one is chatting at the moment.

      The Helper Discord

      Staff online

      Members online

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top