php cache function not working on jquery .load() - php

I'm using a standard php cache script on page.php
$cache = 'the_location/'.$id.'.html';
$expire = time() -3600 ;
if(file_exists($cache) && filemtime($cache) > $expire)
{
readfile($cache);
} else {
ob_start();
// some stuff
$pages = ob_get_contents();
ob_end_flush();
$fd = fopen("$cache", "w");
if ($fd) {
fwrite($fd,$pages);
fclose($fd);
}
echo $pages ; }
On main_page.php I'm loading page.php like so:
$('#div').load('page.php?id=' + id);
If I go straight to page.php?id=1234 the page is cached and file 1234.html appears in 'the_location'
Otherwise on main_page.php nothing happens ...
Help is much appreciated !
Edit : Everything works on main_page.php and page.php, page.php is correctly loaded into main_page.php but not cached, if I load page.php through browser it is cached.

I simulated this as basically as I could:
main_page.php:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"><head id="ctl00_Head1">
<script src="jquery-1.5.min.js"></script>
<script type="text/javascript">
var id = <?=$_GET['id']?>;
$(function(){
$('#div').load('page.php?id=' + id);
});
</script>
</head>
<body>
<div id="div">a</div>
</body>
</html>
page.php
<?php
$id = $_GET['id'];
$cache = 'the_location/'.$id.'.html';
$expire = time() -3600 ;
if(file_exists($cache) && filemtime($cache) > $expire)
{
readfile($cache);
} else {
ob_start();
echo 'This is a generated page';
$pages = ob_get_contents();
ob_end_clean();
$fd = fopen("$cache", "w");
if ($fd) {
fwrite($fd,$pages);
fclose($fd);
}
echo $pages;
}
This works for me. Things I noted while working through the code.
make sure you are setting id to php's $_GET['id'] in javascript in the main_page.php
make sure you are setting $id = $_GET['id']; on page.php
using ob_end_flush(); along with echo $pages is repetitive (content gets flushed, then shows up twice when the page is regenerated), use ob_end_clean() or $pages = ob_get_flush().

Have you looked at the request that is being sent using firebug? JQuery is probably appending a cache-busting GET variable to the URL hence busting your cache :D

Related

I'm not getting flush to work

I have the following script:
function follow($file)
{
$currentSize = filesize($file);
$size = $currentSize;
$index=0;
while ($index<$currentSize) {
//echo "ENTERING LOOP!!!!";
clearstatcache();
$currentSize = filesize($file);
if ($size == $currentSize) {
usleep(100);
continue;
}
$fh = fopen($file, "r");
fseek($fh, $size);
while ($d = fgets($fh)) {
ob_end_flush();
echo $d;
ob_flush();
flush();
ob_start();
}
fclose($fh);
$size = $currentSize;
$index=$index+1;
}
}
follow("/var/www/devicemanagement/testFile.txt");
This script echoes a log file in real time and it works well when run in command line.
The following html code is meant to display the echoed lines from the php script:
<!DOCTYPE html>
<html>
<head>
<script src="//cdnjs.cloudflare.com/ajax/libs/jquery/2.1.0/jquery.js"></script>
<script>
var sentData = {
'param1': 'value1',
'param2': 'value2'
};
function successCallback(returnedData) {
$('#myDiv').html(returnedData);
}
function doAjaxCall() {
$.get('/labtool/controllers/tailor.php', sentData, successCallback);
//$.get('testFile.php', sentData, successCallback);
}
$(document).ready(function () {
var id;
$('#doStuff').click(function () {
clearInterval(id);
//$.get('testFile.php', sentData, successCallback);
});
id = setInterval(doAjaxCall, 1000);
});
</script>
</head>
<body>
<div id="myDiv"><h2>Let AJAX change this text</h2></div>
<button type="button" id="doStuff">Change Content</button>
<div id="myDiv"></div>
</body>
</html>
I understand the key is using flush right, but despite my best efforts and a lot of experimenting I'm unable to get it to work.
Can anyone see what I'm doing wrong?
This works for me using info I gathered from probably many sources including stackoverflow, sorry about the formatting. Every time you have text to flush, simply call the function:
function flush_message($msg)
{
echo $msg;
// not a space, just '', I haven't tried removing it to see what happens
// cause I should really be working on something else right now!
echo str_pad('', 4096) . "\n";
ob_flush();
flush();
}
I also set
apache_setenv('no-gzip', 1);
ini_set('zlib.output_compression', 0);
at the beginning of script
Apparently there are lots of browser specific issues as well (regarding how big buffer until output is drawn) so you might want to test on different platforms to see how it performs.

Include CSS/JS only when necessary

My menu system all comes from the index file. A simplified example:
#index.php
...
<div id="container">
include($inc_file) //index.php?site=news will show news.php
</div>
...
My question is: how can I only include some js-files and css-files when they are needed. For example, I have a file with a slider. I would like only to include the js/css-files related to the slider when visiting that page.
I know that I can make if/else clauses, but I find that a bit ineffective. Is it possible to place a session containing an array with all the files that should be included on the frontpage?
Here is what I would use:
<?php
$path_parts = pathinfo(__FILE__);
$filename = $path_parts['filename'];
if ($filename === "somepage") {
?>
<script src=""></script>
<link ...>
<?php } ?>
I would just have a preset array for each page with an array of css/js files it would need to include.
eg.
settings.php
<?php
$pages = array();
$pages['index'] = array(
"css" => array("main.css","index.css"),
"js" => array("jquery.min.js","someotherjs.js")
);
$pages["about"] = array(
...
);
index.php
<?php
include('settings.php');
?>
...
<head>
<?php
foreach($pages['index']['css'] as $css)
echo "<link rel='stylesheet' type='text/css' href='$css'>";
?>
...
<?php
foreach($pages['index']['js'] as $js)
echo "<script src='$js'></script>";
</body>
</head>
This may not be the answer you were expecting but did you know that the browser does not download css or js files that have not changed since the last time they were downloaded. They are cached on the client PC and only refreshed if the copy on the server has changed.
Theerfore you may not need to be so selective about what you load on each page as it probably wont cause a fresh download of a css or js file.
Prepare an object where you can add style or js files as you need, here a little example.
class head {
private styles = array();
private scripts = array();
public function __construct() {}
// add script in page
public function add_script(filepath){
array_push($this->scripts, filepath);
}
// add style in page
public function add_style(filepath){
array_push($this->styles, filepath);
}
// get html <link> for styles
public function get_styles(){
$html = '';
$len = count($this->styles);
for($i=0; $i < $len; $i++){
$html .='<link rel="stylesheet" type="text/css" href="'.$this->styles[$i].'">';
}
return $html;
}
// get html <script> for scripts
public function get_scripts(){
$html = '';
$len = count($this->scripts);
for($i=0; $i < $len; $i++){
$html .='<script type="text/javascript" src="'.$this->scripts[$i].'"></script>';
}
return $html;
}
}
// destruct ...
in your controller :
require('/class/head.php');
$head = new head();
$head->add_style('/css/myStyle.css');
$head->add_script('/js/myScript.js');
in head
<?php echo $head->get_styles(); ?>
<?php echo $head->get_scripts(); ?>

reading Json from PHP with javaScript

My PHP code is:
<?php
class Sample{
public $name = "N3mo";
public $answer = "";
}
if( isset( $_GET['request'] ) ){
echo "Starting to read ";
$req = $_GET[ 'request' ];
$result = json_decode($req);
if( $result->request == "Sample" ){
$ans = new Sample();
$ans->answer = " It Is Working !!! ";
echo json_encode($ans);
}else{
echo "Not Supported";
}
}
?>
Is there anything wrong
I want to send a JSON to this php and read the JSON that it returns using java script , I can't figure out how to use JavaScript in this , because php creates an html file how Can I use $_getJson and functions like that to make this happen ?!
I tried using
$.getJSON('server.php',request={'request': 'Sample'}) )
but php can't read this input or it's wrong somehow
thank you
try this out. It uses jQuery to load contents output from a server URL
<!DOCTYPE html>
<html>
<head>
<title>AJAX Load Test</title>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>
<script>
$(document).ready(function() {
$("#button").click(function(event) {
$('#responce').load('php_code.php?request={"request":"Sample"}');
});
});
</script>
</head>
<body>
<p>Click on the button to load results from php_code.php:</p>
<div id="responce" style="background-color:yellow;padding:5px 15px">
Waiting...
</div>
<input type="button" id="button" value="Load Data" />
</body>
</html>
Code below is an amended version of your code. Store in a file called php_code.php, store in the same directory as the above and test away.
<?php
class Sample
{
public $name = "N3mo";
public $answer = "";
}
if( isset( $_GET['request'] ) )
{
echo "Starting to read ";
$req = $_GET['request'];
$result = json_decode($req);
if( isset($result->request) && $result->request == "Sample" )
{
$ans = new Sample();
$ans->answer = " It Is Working !!! ";
echo json_encode($ans);
}
else
{
echo "Not Supported";
}
}
Let me know how you get on
It would be as simple as:
$.getJSON('/path/to/php/server.php',
{request: JSON.stringify({request: 'Sample'})}).done(function (data) {
console.log(data);
});
You can either include this in <script> tags or in an included JavaScript file to use whenever you need it.
You're on the right path; PHP outputs a result and you use AJAX to get that result. When you view it in a browser, it'll naturally show you an HTML result due to your browser's interpretation of the JSON data.
To get that data into JavaScript, use jQuery.get():
$.get('output.html', function(data) {
var importedData = data;
console.log('Shiny daya: ' + importedData);
});

how to make a php function wait to execute until the end of the script

I'm trying to create a queue function for javascript files. Basically, this is the way I want it to work:
I want to create a function that will take all of the javascripts sent to it and put them in the appropriate place on the page (i.e. header or footer, and above dependent scripts).
I want to be able to say:
Here's a script. Add it to the queue in the order that it should be in. Then, after all the scripts have been queued up, run the function to write them to the page.
So far my code looks like this:
$scripts = array();
function enqueue_script($src="", $script_name="", $script_data="",
$script_dependencies=array(), $force_header=false, $additional_atts=array()){
global $scripts;
//run check for duplicates
//run check for dependencies already in the variable
//run checks to see if this script depends on other scripts
//$scripts array is saved in increments of 10 to allow for up to
//9 dependants
$i = count($scripts);
$i = ($i*10)+10;
$scripts[$i]['src'] = $src;
$scripts[$i]['script_name'] = $script_name;
$scripts[$i]['script_data'] = $script_data;
$scripts[$i]['dependencies'] = $script_dependencies;
$scripts[$i]['force_header'] = $force_header;
$scripts[$i]['atts'] = $additional_atts;
}
function write_scripts_header() {
global $scripts;
$echo = "";
$atts = "";
//create script tag for insertion in header
foreach($scripts as $s){
if($s['force_header']){
foreach($s['atts'] as $a => $v){
$atts .= " {$a}='{$v}'";
}
if($s['src']!=""){
$echo .= "<script src='{$s['src']}'{$atts}></script>\n";
} else {
$echo .= "<script{$atts}>{$s['script_data']}</script>\n";
}
}
}
echo $echo;
}
function write_scripts_footer() {
global $scripts;
$echo = "";
$atts = "";
//create script tag for insertion in footer
foreach($scripts as $s){
if(!$s['force_header']){
foreach($s['atts'] as $a => $v){
$atts .= " {$a}='{$v}'";
}
if($s['src']!=""){
$echo .= "<script src='{$s['src']}'{$atts}></script>\n";
} else {
$echo .= "<script{$atts}>{$s['script_data']}</script>\n";
}
}
}
echo $echo;
}
Then, in the HTML file part:
<html>
<head>
<?php write_scripts_header();?>
</head>
<body>
<?php write_scripts_footer();?>
</body>
</html>
This works fine if the HTML section is loaded last. However if I have an include that happens in the middle of the body tag, and that include needs to enqueue_script() in the header, then the $scripts variable isn't ready yet when the write_scripts_header() function runs.
How can I make write_scripts_header() and write_scripts_footer() wait until all the scripts have been queued up before running? Or....is there a better way to allow for a scripts queue so that I can write all the scripts to the document at the end?
If it's all running in the same file, Can you make the body content be a variable, $body, and load it first, then echo it into the <body> section?
change
<html>
<head>
<?php write_scripts_header();?>
</head>
<body>
<?php write_scripts_footer();?>
</body>
</html>
to
print "<html><head>".write_scripts_header()."</head><body>".write_scripts_footer()."</body></html>";
Try using a template engine, such as Twig, and you'll be able to output your html after you're done preparing your scripts-array.

I want to create several static html pages from my PHP content using a wwwcopy script

Edited on 7/16/2012
I need to revisit this problem. I have been using dynamic pages for some time with cached PHP on it for some time. Most of my page is cached, but the meta information is not. As i have explained in my post below, i need to reduce/remove requests going to my local MySQL database. I am getting 500 internal server errors because of the requests that are coming. I have been exploring Jquery/Ajax to pull data from the database into a xml page and have the site request data from that, which is working, but i am still running into the problem to move the content from the body, into the meta so facebook, and search engines can see the dynamic content. Here is what i need........
I need a solution that will either create about 1500 static pages by looking at
http://chennaichristianradio.com/PHP/web/songinfo.php?songID= and start with 0 and count up to 1500 (http://chennaichristianradio.com/PHP/web/songinfo.php?songID=0 to http://chennaichristianradio.com/PHP/web/songinfo.php?songID=1500). That would work for me but not preferred.
The second option would be to have a PHP cache option that will cache the entire page including the meta data that is created from PHP.
The third option (my preferred option) is to be able to create meta data from either ajax/Jquery created data in the body, or from the xml page itself.
Thank you for revisiting this request. Please read my original posts from earlier this year. here is some of my current code that i am using and links for examples.....
Current PHP used to create the dynamic pages...
<title>Chennai Christian Radio</title>
<meta property="og:title" content="<?php echo $song->title; ?> by <?php echo $song->artist; ?> - Found on Chennai Christian Radio"/>
<meta property="og:type" content="song"/>
<meta property="og:url" content="http://chennaichristianradio.com/PHP/web/songinfo.php?songID=<?php echo $song->ID; ?>"/>
<meta property="og:image" content="<?php echo $song->picture; ?>"/>
<meta property="og:site_name" content="Chennai Christian Radio"/>
<meta property="fb:admins" content="1013572426"/>
<?php
$cachefile = "cache/".$reqfilename.$cache_folder.md5($_SERVER['REQUEST_URI']);
$cachetime = 11000 * 60; // 110000 minutes
// Serve from the cache if it is younger than $cachetime
if (file_exists($cachefile) && (time() - $cachetime
< filemtime($cachefile)))
{
include($cachefile);
echo "<!-- Cached ".date('jS F Y H:i', filemtime($cachefile))."
-->n";
exit;
}
ob_start(); // start the output buffer
?>
<div id="fbbutton">
<div id="fb-root"></div>
<script>(function(d, s, id) {
var js, fjs = d.getElementsByTagName(s)[0];
if (d.getElementById(id)) return;
js = d.createElement(s); js.id = id;
js.src = "//connect.facebook.net/en_US/all.js#xfbml=1&appId=170819122971839";
fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));</script>
<div class="fb-like" data-send="false" data-layout="box_count" data-width="50" data-show-faces="false"></div>
</div>
<div id="songinfo">
<!-- BEGIN:AlbumArt -->
<?php if(SHOW_PICTURES && !empty($song->picture)) : ?> <img class="picture" id="picture" onload="showPicture(this, <?php echo SHOW_PICTURES; ?>)" src="<?php echo $song->picture; ?>" alt="" border=0 width="142" height="142" /></a><?php endif; ?>
</div>
<!-- Song Info -->
<div id="wrapper">
<div id="title"><?php echo $song->title; ?></div>
<div id="artist"><?php echo $song->artist; ?></div>
<div id="album"><?php echo $song->album; ?></div>
<div id="duration"><?php echo $song->durationDisplay; ?></div>
<div id="year"><?php echo $song->albumyear; ?></div>
</div>
<div id="lyrics"><pre><?php if (!empty($song->lyrics)) : ?><dd class="broad"><pre><?php echo $song->lyrics; ?><?php endif; ?></pre></div>
<?php
// open the cache file for writing
$fp = fopen($cachefile, 'w');
// save the contents of output buffer to the file
fwrite($fp, ob_get_contents());
// close the file
fclose($fp);
// Send the output to the browser
ob_end_flush();
?>
Here is my new scripts to create the XML.......
<?php
// Change to your database user name
$username="*********";
//Change to your database password
$password="*********";
// Change to your database name
$database="********";
// Connect to the database
mysql_connect('*********',$username,$password);
// Handle an error
#mysql_select_db($database) or die( "Unable to select database");
// Build Sql that returns the data needed - change this as required.
$sql = "SELECT songlist.*, historylist.listeners as listeners, historylist.requestID as requestID, historylist.date_played as starttime FROM historylist,songlist WHERE (historylist.songID = songlist.ID) AND (songlist.songtype='S' OR songlist.songtype='C' OR songlist.songtype='N') ORDER BY historylist.date_played DESC LIMIT 1";
// Store results in result object
$result = mysql_query($sql);
//store values in vars for calculation for array creation
$curPlay = mysql_query("SELECT * FROM historylist ORDER BY date_played DESC LIMIT 1");
$curPlayRow = mysql_fetch_assoc($curPlay);
if (!$curPlay) { echo( mysql_error()); }
$dt_duration = mysql_result($result,$i,'duration');
$title= mysql_result($result,$i,'title');
$artist= mysql_result($result,$i,'artist');
$album= mysql_result($result,$i,'album');
$picture= rawurlencode(mysql_result($result,$i,'picture'));
$lyrics= mysql_result($result,$i,'lyrics');
$ID= mysql_result($result,$i,'ID');
$curtime = time();
$timeleft = $starttime+round($dt_duration/1000)-$curtime;
$secsRemain = (round($dt_duration / 1000)-($curtime-$starttime));
//build array to return via getXMLHTTPRequest object - you can include more vars but remeber to handle them
//correcty in the useHttpResponse function
$Aj_array = $dt_duration . '|' . $secsRemain . '|' . $title . '|' . $artist .'|' . $album .'|' . $picture .'|' . $lyrics .'|' . $ID;
//we are using the text response object - which i think is easier for small data return
//just echo the array - not so much AJAX rather AJA ??
echo $Aj_array
?>
And the script in my html to read the extracted content....
<script language="JavaScript">
var http = getXMLHTTPRequest();
var counter;
function getXMLHTTPRequest() {
try {
req = new XMLHttpRequest();
} catch(err1) {
try {
req = new ActiveXObject("Msxml2.XMLHTTP");
} catch (err2) {
try {
req = new ActiveXObject("Microsoft.XMLHTTP");
} catch (err3) {
req = false;
}
}
}
return req;
}
function getServerText() {
var myurl = 'http://bangalorechristianradio.com/PHP/web/aj.php';
myRand = parseInt(Math.random()*999999999999999);
var modurl = myurl+"?rand="+myRand;
http.open("GET", modurl, true);
http.onreadystatechange = useHttpResponse;
http.send(null);
}
function useHttpResponse() {
if (http.readyState == 4) {
if(http.status == 200) {
var aj_results = http.responseText.split("|");
var aj_duration = aj_results[0];
var aj_secsremaining = aj_results[1];
var aj_title = aj_results[2];
var aj_artist = aj_results[3];
var aj_album = aj_results[4];
var aj_picture = aj_results[5];
var aj_lyrics = aj_results[6];
var aj_ID = aj_results[7];
// update title and artist divs
document.getElementById('title').innerHTML = aj_title;
document.getElementById('artist').innerHTML = aj_artist;
document.getElementById('album').innerHTML = aj_album;
document.getElementById('picture').innerHTML = "<img src=http://chennaichristianradio.com/images/album_art/" + aj_results[5] +" width='142' height='142'>"
document.getElementById('lyrics').innerHTML = aj_lyrics;
countDownInterval = aj_secsremaining - 0;
countDownTime = countDownInterval + 1;
countDown()
}
} else {
//document. getElementById('actual').innerHTML = "";
}
}
function countDown() {
countDownTime--;
if (countDownTime == 0) {
countDownTime = countDownInterval;
getServerText()
}
else if (countDownTime < 0)
countDownTime = 30;
if (document.all)
document.all.countDownText.innerText = secsToMins(countDownTime);
else if (document.getElementById)
document.getElementById("countDownText").innerHTML = secsToMins(countDownTime);
stopCountDown();
counter = setTimeout("countDown()", 1000);
}
function secsToMins(theValue) {
var theMin = Math.floor(theValue / 60);
var theSec = (theValue % 60);
if (theSec < 10)
theSec = "0" + theSec;
return(theMin + ":" + theSec);
}
function stopCountDown()
{
clearTimeout(counter)
}
</script>
Some links
Dynamically created Page using PHP: http://chennaichristianradio.com/PHP/web/songinfo.php?songID=5351
XML Page that shows the data that i need in my pages: http://bangalorechristianradio.com/PHP/web/aj.php
Page that has been created using JQuery and Ajax: http://bangalorechristianradio.com/PHP/web/player.html
Thanks everyone for your help!!
-Bob Swaggerty
Original post Below
I am wanting to create several hundred static html pages using the following script:
<?php
function wwwcopy($link,$file)
{
$fp = #fopen($link,"r");
while(!feof($fp))
{
$cont.= fread($fp,1024);
}
fclose($fp);
$fp2 = #fopen($file,"w");
fwrite($fp2,$cont);
fclose($fp2);
}
wwwcopy("http://www.domain.com/list.php?member=sample", "sample.html");
I create my php page called create.php, which creates 1 file. How do I use this script and create 1200 pages?
My whole purpose to do this is to minimize requests going to my MySQL database, and make my pages come up faster. After creating these pages, I will use another script to point to these files.
(update on 3/24/2012)
Thank you for your responses. Here is what I am trying to accomplish in doing this. Maybe there is a better solution......
look at my page http://chennaichristianradio.com/PHP/web/songinfo.php?songID=5351
My software is dynamically creating this page from the ID 5351 (the song in my database)
I am using php to go grab the song info. To make this process a bit more efficient, I hace set up PHP caching. I am caching this now using....
<?php
$cachefile = "cache/".$reqfilename.$cache_folder.md5($_SERVER['REQUEST_URI']);
$cachetime = 11000 * 60; // 110000 minutes
// Serve from the cache if it is younger than $cachetime
if (file_exists($cachefile) && (time() - $cachetime
< filemtime($cachefile)))
{
include($cachefile);
echo "<!-- Cached ".date('jS F Y H:i', filemtime($cachefile))."
-->n";
exit;
}
ob_start(); // start the output buffer
?>
The PHP Cache is being accomplished using this, but my problem is it only caches the PHP info from this code and below. The reason this is a problem, is because i use PHP in the meta info aslo for my Open Graphic tags. The OG is so people can "like" my music in Facebook. Here is what my OG tags look like.
<title>Chennai Christian Radio</title>
<meta property="og:title" content="<?php echo $song->title; ?> by <?php echo $song->artist; ?> - Found on Chennai Christian Radio"/>
<meta property="og:type" content="song"/>
<meta property="og:url" content="http://chennaichristianradio.com/PHP/web/songinfo.php?songID=<?php echo $song->ID; ?>"/>
<meta property="og:image" content="<?php echo $song->picture; ?>"/>
<meta property="og:site_name" content="Chennai Christian Radio"/>
<meta property="fb:admins" content="1013572426"/>
<meta property="og:description"
content="Chennai Christian Radio is your last stop for today's best Christian Music. http://chennaichristianradio.com"/>
So...... What is the solution for caching my dynamic page AND include the meta info. That is defiantly the best option, but with my current code, it still queries my MYSql server for the Meta info and the song info. I thought by creating static pages for this and telling my software to point to these pages instead of querying my database it would be more efficient, as well as help in reducing my PHP traffic back to my server. Hope this clarifies my need, and I thank everyone for their response and help.
I'm not sure what you are trying to achieve but to do this 1200 times simply use a for loop and change the filename and urls.
for ($i = 1; $i <= 1200; $i++)
{
wwwcopy("url", "file");
}
I think that you must go for cache option. If you are using any frame work there are default settings for enabling cache. This will really improve your speed.

Categories