Reading a php file from another php file - php

I currently have a php file that I'm using as a template but I need it to read in data from another php file that I'm using for the page content. I'm doing it this way to save on code and time, however it doesn't appear to be working. I have done a test with shorter amounts of code but it still isn't working. they are both .php files.
Code -
<html>
<head>
<title></title>
</head>
<body>
<?php
$temp = 'test-2.php';
$file = fopen($temp, 'r');
$cont = fread($file, filesize($temp));
print $cont;
fclose($file);
?>
</body>
test-2.php
<?php
echo 'hello world';
?>

just use <?php include('test-2.php'); ?> surely that'll do what you want?

Related

How to convert php code to html source code?

I need to display html source code form other php file.
I have two file
code.php
index.php (I hope I can convert the code.php to html source code.)
code.php:
<!DOCTYPE html> <html> <body> <?php $color = "red"; echo $color; ?> </body> </html>
index.php (I hope I can convert the code.php to html source code.)
$php_to_html = file_get_contents("code.php");
$html_encoded = htmlentities($php_to_html);
echo $html_encoded;
but when i run the index.php file, the result is
<!DOCTYPE html> <html> <body> <?php $color = "red"; echo $color; ?> </body> </html>
but I hope I can see the result is
<!DOCTYPE html> <html> <body> red </body> </html>
any idea how can i do this ,thanks!!!
You want to execute the PHP, so include it and capture the output:
ob_start();
include("code.php");
$php_to_html = ob_get_clean();
$html_encoded = htmlentities($php_to_html);
echo $html_encoded;
If you want the HTML to be rendered as HTML then don't use htmlentities().
Optionally (not the best way) but you can execute it by retrieving from the URL:
$php_to_html = file_get_contents("http://www.example.com/code.php");
$html_encoded = htmlentities($php_to_html);
echo $html_encoded;
Buffer output and include it:
ob_start();
include_once('code.php');
$html = ob_get_clean();
By using output buffering, any output is not sent to the browser, but instead kept in memory. This allows you to run the code, and get the output as a variable. ob_get_clean() flushes the buffer (in this case into our $html variable), and then stops buffering, allowing you to continue as normal. :-)

php not working properly in mail()

I am trying to send the information as a mail to the admin
Below is the mail code and I am reading this using file_get_contents() in another file.
<!doctype html>
<?php
require_once("mysqlconnect.php");
session_start();
?>
<html>
<head>
<link rel="stylesheet" href="css/mail.css">
</head>
<body>
<table border="2">
<?php
$q="select * from profile where mobile=2147483647";
$result=mysqli_query($conn,$q);
while($row = mysqli_fetch_assoc($result))
{
foreach($row as $key=>$value)
{
if($key=="Email")
echo"<tr><th>$key</th><td><a href='mailto:$value; target='_blank'>$value</td></tr>";
else
echo"<tr><th>".str_replace('_',' ',$key)."</th> <td>$value</td></tr>";
}
}
?>
</table>
</body>
</html>
<?php
session_destroy();
?>
The result of this code in the mail is the PHP code itself..
$value) { if($key=="Email") echo"
$key $value
"; else echo"
".str_replace('_',' ',$key)." $value
"; } } ?>
file_get_contents returns the content of a file as string. It does not executes the code inside it.
To use the HTML from a php file you can use php's buffer.
Like this:
ob_start();
include('file_path_here');
$HTML = ob_get_clean();
This will store the output of the file in the $HTML variable.
file_get_contents literally reads in a file as a string. If you're reading in a PHP file using that method, it'll literally return the code, which is what you're getting.
If you want to send HTML as an e-mail, use something like a template system (sitecrafting.com/blog/top-5-php-template-engines) or build a string and pass it onto the mail() function.
PHP documentation: file_get_contents

How to refresh a webpage in php when file contents has changed?

I Was wondering if it was possible to refresh a php webpage when a files content has changed?
I have some code but it doesn't refresh on a file change it refreshes every second. How can I do it when the file changes?
Here is the code:
<html>
<head>
<title>PHP Page</title>
<meta http-equiv="refresh" content="1">
</head>
<body>
<?php
$filename = "data.txt";
$handle = fopen($filename, "r");
$contents = fread($handle, filesize($filename));
echo "<pre>$contents</pre>";
fclose($handle);
?>
</body>
you can try this
header( "Refresh:5; url=http://www.example.com/page2.php", true, 303);
You can fire an ajax request on every 5 seconds which will check either file is changed or not.
If file is changed the reload the page with javascript using
location.reload();

Load a html file

Probably i didnt explain very well, i updated the code to turn more easy to see what i want.
I need to load a external html file in that "div external_page....", i thing that the best way is to load a file in the php file(External PHP content), tell me if im wrong :)
This is the demo: http://vasplus.info/demos/load_and_refresh_div_every_10_seconds/index.php
(UPDATE)
<script language="javascript" src="js/jquery_1.5.2.js"></script>
<div id="external_page_content_displayer">External page contents will be here</div>
<!-- EXTERNAL PHP CONTENTS-->
<?php
srand((float) microtime() * 10005224);
$lines = file('ajax/rssatom/rss-atom.html');
$This_Page_Content = array($lines,
"teste","teste","teste");
$This_Page_Content_Rand_Keys = array_rand($This_Page_Content, 2);
$This_Page_Content_Displayer = $This_Page_Content[$This_Page_Content_Rand_Keys[0]] . "\n";
echo strip_tags($This_Page_Content_Displayer);
?>
<!-- LOAD FUNCTION-->
<script>
function Load_external_content()
{
$('#external_page_content_displayer').load('external_content.php').hide().fadeIn(3000);
}
setInterval('Load_external_content()', 10000);
</script>
Regards
You can try with this:
$lines = file('ajax/rssatom/rss-atom.html');

get a text file using php and send the string to javascript

Ok, what I am trying to do is make a javascript loop of images, but first I have to get a list of the images. In javascript there is no way to directly grab this text file... http://www.ssd.noaa.gov/goes/east/tatl/txtfiles/ft_names.txt but it can be done eaisly in php, I am currently gettung the txt file using php, but the javascript cannot read the variable. How can I make javascript be able to read this variable. Here is what I have...
<?php
$file = "http://www.ssd.noaa.gov/goes/east/tatl/txtfiles/ft_names.txt"; //Path to your *.txt file
$contents = file($file);
$string = implode($contents);
echo $string;
?>
<script type="text/javascript">
function prnt() {
var whatever = "<?= $string ?>";
alert(whatever);
}
</script>
You can use echo or print to write to the page in PHP.
var whatever = "<?php echo $string; ?>";
Although, if the file has line breaks in it, you will need to remove those.
Make it a bit more interesting: go ahead and split the fields and use JSON encoding. It should read directly in javascript without needing to call JSON.parse() on the client.
<?php
$lines = file_get_contents('http://...');
$lines = explode("\n",trim($lines));
foreach ($lines as &$line) {
$line = preg_split('/,? /',$line);
}
$js = json_encode($lines);
?>
<!DOCTYPE HTML>
<html lang="en-US">
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<script type="text/javascript">
var dar = <?php echo $js; ?>;
</script>
</body>
</html>
You should also consider using a local proxy to cache the results of that file if you plan to run this frequently and especially if you are going to serve it up on a public web server somewhere. Store the file locally as "noaa_data.txt" and have a second script on a cron job (12 hours or something):
<?php
file_put_contents("/var/www/noaa_data.txt",file_get_contents("http://www.ssd.noaa.gov/goes/east/tatl/txtfiles/ft_names.txt"));
?>

Categories