Background php file processing without AJAX - php

Working with files compiler that puts a bit of a strain on FIRST homepage hit until the css files are compiled thus fooling the visitor that site is slow,
I have successfully routed the function to ajax witch works very well but I cant let it go since it is bound to js and would love to do this with php
the ajax mootools
new Request({
url: linktofile... and basic responses
does nothing else but is accessing php file which says
// sitename.com/?view=custom
file_get_contents('OF_THE_website_but_different_view');
so how would I do this from php was trying exec() , cant get to work (WAMP local)
CURL , would be same as if I would do file_get_contents() within homepage and slows everything down again was trying to see how proc_open() works but I cant figure it out
so if anyone could rout me to use any php function as a fallback for ajax and call my file_get_contents() file in backend without slowing the homepage down.
please do not suggest SHELL , CRON , or any other lang but PHP
Thank you!

Fund awesome reference
PHP Background Process Still Affecting Page Load
http://php.net/features.connection-handling#71172
header("Connection: close");
ob_start();
phpinfo();// or my page buffer previously saved in variable
$size=ob_get_length();
header("Content-Length: $size");
ob_end_flush();
flush();
file_get_contents('OF_THE_website_but_different_view');
works like charm

Related

A download script and statcounter working together without opening a new page

I have this download script running that allows people to click on a button and download a file without any new html pages opening. It all works well. Here is the script.
<?php
header('Content-disposition: attachment; filename=brochure-company-details.pdf');
header('Content-type: pdf');
readfile('brochure-company-details.pdf');
?>
However, I really want to be able to run the statcounter script at the same time, so as to document who is downloading the file. The Statcounter script works well enough when an html page opens and runs through its function. But I down't want a new page to open. I just want the download to start. The user experience will be that they only have the file download and without them knowing the statcounter will record the event.
Can you help?
What you want is not possible. (executing javascript on pdf download) Statcounter is an script and is executed by the browser by being included on the html. If you download a pdf file, you are not executing any js.
However, statcounter can see what links are pressed and therefore you can find which files were downloaded; as long your files are downloaded by using regular links on an html that has statcounter included. You don't need to do anything at all, they would be counted by default.
The idea Dachi gave, is to add an sql insertion to that php code of yours.
<?php
$dbh = new PDO("sqlite:/path/to/database.sdb");
//Put the insertion code here.
//Insert things like the IP, the login name, the time, etc.
header('Content-disposition: attachment; filename=brochure-company-details.pdf');
header('Content-type: pdf');
readfile('brochure-company-details.pdf');
That works too, and is a good choice if you are allowing downloads by not using regular links or not from an html that has statcounter.

download a file with php and hash

So this is simple to understand what i want to achieve. So i get links like theese:
http://rockdizfile.com/atfmzkm7236t
http://rockdizfile.com/xuj5oincoqmy
http://rockdizfile.com/pg8wg9ej3pou
So theese links are from one cloud storage site I want to make a php script that automates their downloading.
So I can't find which is the script or the thing these links download button starts and how can I start that so i can download it with php on my server?
Basically my idea is to download a lot of files but don't wanna do it manually so need automatic way of doing it. As far as I know I make a request which is the following 2 urls:
http://rockdizfile.com/pg8wg9ej3pou
http://wi32.rockdizfile.com/d/wsli6rbhfp4r2ge4t7cqeeztijrprelfiw4afvqg5iwspmvqabpkmgiz/Desislava%20feat.%20Mandi%20&%20Ustata%20-%20Pusni%20go%20pak%20(CDRIP).mp3
So the first url is executing the next one but here comes the tricky part as far as I tested that last string Desislava%20feat.%20Mandi%20&%20Ustata%20-%20Pusni%20go%20pak%20(CDRIP).mp3 is the file name we get when downloading so if you change it with for example somefile.mp3 it will download somefile.mp3 but with the same file content as http://wi32.rockdizfile.com/d/wsli6rbhfp4r2ge4t7cqeeztijrprelfiw4afvqg5iwspmvqabpkmgiz/Desislava%20feat.%20Mandi%20&%20Ustata%20-%20Pusni%20go%20pak%20(CDRIP).mp3 so the data is hidden in this hash wsli6rbhfp4r2ge4t7cqeeztijrprelfiw4afvqg5iwspmvqabpkmgiz or i think so. And now is the tricky part how to get this hash? we have almost everything we have the code for the url atfmzkm7236t the hash wsli6rbhfp4r2ge4t7cqeeztijrprelfiw4afvqg5iwspmvqabpkmgiz and the filename Desislava%20feat.%20Mandi%20&%20Ustata%20-%20Pusni%20go%20pak%20(CDRIP).mp3 There must be a way to download from this site without clicking so please help me kinda a hack this :)
you can use PHP's header function to force a file to download
header('Content-disposition: attachment; filename=index.php');
readfile('Link');
You should know that this will not give you the ability to download PHP files from external websites.
You can only use this if you got the direct link to a file
It's impossibly to tell you without the source code
e.g. sha1("Test Message") gives you 35ee8386410d41d14b3f779fc95f4695f4851682 but sha256("Vote this up") gives you 65e03c456bcc3d71dde6b28d441f5a933f6f0eaf6222e578612f2982759378ed
totally different... unless you're hidden function add's "65e03c456bcc3d71dde6b28dxxxxxxxxxxxxxxxxxxxxxxxxxx" (where xxxxxxxxxxxxxxxxxxxxxxxxxx is a bunch of numbers I can't be arsed to work out) to each hash...
then sha1("Test Message") gives you 65e03c456bcc3d71dde6b28d441f5a933f6f0eaf6222e578612f2982759378ed
The file is embedded into the swf player.
alert(jwplayer('mp3player').config.file);
Something like:
<?PHP echo file_get_contents($_GET["url"]); ?>
<script>
document.location=jwplayer('mp3player').config.file;
</script>
Though I've actually just noticed they change 5 digits of the URL on each page request, and the script above uses 2 page requests. One to get the URL and HTML source and another to try and download the file, meaning the URL has changed before the second request has started.

How can I pass a very long string from one page to another?

I'm making a data visualisation tool that works using SVG, d3.js and JQuery. I am currently making a feature to export (and download) as an SVG file:
// Code on main page
var svg = $("#svg-wrap").html();
var win = window.open("export.php?svg=" + svg, '_blank'); // _blank means export.php opens in a new tab
win.focus;
// Code in export.php
<?php
ob_start();
header("Content-Type: application/octet-stream");
header("Content-disposition: attachment; filename=data.svg");
$svg = $_GET["svg"];
echo stripslashes($svg);
?>
This doesn't work though, because although some of the SVG is passed through, the full code is too long for a query string (or so it seems).
Is there some way that I can fix this? I could use compression, but that would only shrink it up to a limit and I think it would probably still be too long - the SVG code could be hundreds of lines :( .
Most UAs put limits on GET requests. Use POST instead.
You can store it in session variable and access it on other page... but not the best practice though...
You may want to use window.postMessage to handle the communication between your main app window and your popup, this way you could just stay on the client side only.
It is hard to tell the limitation,
a browser can have a limit, for post vs get
a proxy can have a limit, for post vs get
a web server can have a limit, for post vs get
the application itself can set a limit...
etc
As suggested, you could go with writing a svg file and send the url instead.
How big is the svg file?

AJAX: Detect connection closed, PHP continues

Situation: User uploads a number of photos via AJAX, then continues interacting with the website whilst a PHP script continues running in the background and generates a variety of thumbnails based on the uploaded photos.
Site config:
jQuery AJAX (v1.9.1)
PHP 5.4.7, FastCGI mode
IIS 7.5, with gzip
Previous posts that I've referred to and tried to implement (but to no avail):
Closing a connection early with PHP
PHP: continue after output is complete
Send AJAX results but continue processing in PHP
close a connection early
Disable Gzip compression for single php file with IIS
http://www.php.net/manual/en/features.connection-handling.php#71172
I have tried a huge number of script options based on the previous posts, however none seem to tell the AJAX script to let the user continue, whilst the PHP continues to process...
Example PHP code:
<?php
// Save images to db, etc
// Now tell AJAX to let the user continue, before generating thumbnails
if(ini_get('zlib.output_compression')) {
ini_set('zlib.output_compression', 'Off'); // turn IIS gzip for this file
}
ob_end_clean();
header("Connection: close");
header("Content-Encoding: none"); //ensures gzip is not sent through
ob_start();
echo '123'; // 3 digit number will be sent back to AJAX
$size = ob_get_length(); // should mean Content-Length = 3
header("Content-Length: $size");
ob_end_flush();
flush();
ob_end_clean();
// Generate thumbnails, etc
?>
Example jQuery AJAX code:
$.ajax({
type: 'POST',
url: ajax_url,
data: { foo: bar },
beforeSend:function(){
//
},
success:function(data){
alert(data); // Only seems to be firing once the thumbnails have been generated.
}
});
The response headers seem okay...
Question: How do I get AJAX to allow the user to continue once it has received the code from the middle of the PHP script, whilst the PHP script continues to generate the thumbnails?
If you run request, it will always wait until PHP Script finish executing, or there will be a timeout. So you cannot stop AJAX in middle, and keep PHP running. If you want to upload files, and then create thumbnails, but have info that files are uploaded, do it in two steps:
upload files with AJAX -> return success
run another AJAX request on success to get uploaded images (or thumbs in fact).
Thanks to that, thumbs can be also rendered later, when they are first time requested (even without ajax). If you don't want requesting and waiting for thumbs, use cron job on server, which will create thumbs for awaiting images.

PHP Output complete notification

I'm trying find a way to have PHP to indicate to the browser that all page output is complete. After the page is done we're running some statistics code that usually doesn't take to long but in case it does I don't want to have the users browser waiting for more data. This can't be done via JavaScript because it needs to work with mobile phones.
I'm already starting output buffering using
mb_http_output("UTF8");
ob_start("mb_output_handler");
to insure I don't have issues with my sites MB text (Japanese). I was hoping that ob_end_flush() would do the trick but if I place sleep(10); after the ob_end_flush() the browser waits an additional 10 seconds. Does anyone have any ideas about this?
UPDATE:
Using jitters approach below I "ob_gzhandler" to get it working with gzip any one see any possible issues here?
//may be also add headers for cache-control and expires (IE)
header("Connection: close"); //tells browser that connection will be closed
ob_start();
ob_start("ob_gzhandler");
//page content
ob_end_flush();
$size = ob_get_length();
header("Content-Length: $size");
ob_end_flush();
flush();
UPDATE AGAIN:
Please take another look at the code above. You need to do an ob_start(); before the ob_start("ob_gzhandler"); and then call ob_end_flush(); prior to calling ob_get_length() so that you get the correct gzip compressed size.
Use something along these lines
//may be also add headers for cache-control and expires (IE)
header("Connection: close"); //tells browser that connection will be closed
ob_start();
//generate your output
$size = ob_get_length();
header("Content-Length: $size");
ob_end_flush();
flush();
//continue statistic processing
I don't think there is a way to notify the browser that the output is complete, at least from the script that sends the output. If you use some other script that will monitor the output of your first script and use an iframe maybe then you might be able to do it.
The browser knows when the output is complete when the page is considered loaded. That is what the browser knows.
You could fork a new php process in the background and let that take care of the stats. Something like:
shell_exec('php stats.php &');
The & at the end makes sure that it's run in the background, so even if the stats.php takes 20 seconds, the visitor won't notice it.
You would probably need to pass data to the stats script, which you can do by passing in parameters, like this:
shell_exec('php stats.php -b '. escapeshellarg($_SERVER['HTTP_USER_AGENT']) .' &');
In stats.php, you'd use the $argv variable to get that data.
But I wouldn't do this if the statistics code doesn't take that long to run, since forking a new process for every page load like this has some overhead. I don't know what it is that makes the stats code take a long time to process, but another solution might be to insert the raw data into a database, and let a background job work on that data to create usable statistics. That could be done either by a cron job, or having a screen run in an infinte loop that processes the queue.
Try to move your statistics code to a seperate function and call this function with an ajax call in the dom.ready or onload event in javascript code on your rendered page like in this meta code:
<html>
<script type="text/javascript">
dom.onready = Ajax.call(location.href + '?do_stats');
</script>
<body>...
</html>
The dom.ready event can be provided by jQuery or Prototype libraries. Downside is it will only work with js enabled.
Alternatively you could just record all needed information for the stats to a database and dispatch a script collecting the queued data from there and working on it in the background - eg by using cron.

Categories