October 14th, 2009

PHP – Simple caching

What should be on the main page?? Correctly, there should be ponemnozhku from each unit of a site. And certainly your page is under construction automatically, proceeding from contents of forums, news lines and other sections of a site.

What should be on the main page?? Correctly, there should be ponemnozhku from each unit of a site. And certainly your page is under construction automatically, proceeding from contents of forums, news lines and other sections of a site.

How much SQL searches it is carried out at loading the main page?? How much blocks are pumped up from other sites??

Now we shall consider a simple and effective way of caching of such data.

As an example we shall consider a case of import of news from the external server: http://news.novgorod.ru/ultramode.txt

Our problem{task} to receive this file, to process it{him} and to deduce{remove} to his{its} user. The program doing{making} it, looks as follows:

<?

$d = file (” http: // news.novgorod.ru/ultramode.txt “);

for ($i=0; $i <11; $i ++) {

$msg = $ d [2 + $ i*8];

$url = $ d [3 + $ i*8];

$date = $ d [4 + $ i*8];

print ” <a href =’http: // news.novgorod.ru ” .trim ($url). ” ‘> “;

print trim ($msg.) ” </a> (“. $date. “)”;

}

?>

At each loading our page there is a reference{manipulation} to the external server news.novgorod.ru, therefrom under report HTTP the file ultramode.txt is downloaded. And now we shall present a situation, that the server is on the other end of a planet and connection and furthermore the file transfer borrows{occupies} enough more time. Or moreover, the server can be temporarily inaccessible.

Conclusion. What for each time podgruzhat` a file ultamode.txt if news are updated not more often than an once at two – three o’clock?? Correctly. It is necessary kehshirovat`.

And we shall do{make} it very simply. As you could notice, the comment is stored{kept} in the first line of a file. The comment is not necessary for us and on his{its} place it is possible to place date of last updating of a file wonderfully.

Algorithm of job the following:

1. We read – out kehshirovannuju the version

2. If kehshirovannaja the version is obsolete, on its{her} place we read – out a file http://news.novgorod.ru/ultramode.txt, we replace the first line with current time and is saved in a cache.

3. We make out and is deduced{removed} given to the user.

The code making all this is below resulted:

<?

// We read – out a file from a cache

$d = file (“/tmp/news.cache”);

$fs=trim ($d [1]);

// If the file was not updated 3600 seconds (1 hour) or the file is empty,

// That we update it{him}

if (intval ($d [0]) <time ()-600 || empty ($fs)) {

$d = file (” http: // news.novgorod.ru/ultramode.txt “);

$fs=trim ($d [1]);

// We save new versju in a cache

if (! empty ($fs)) {

$fw=fopen (“/tmp/news.cache”, “wt”);

flock ($fw, LOCK_EX);

// In the first line we shall write down current time

fputs ($fw, time (). “n”);

// We shall save other lines

for ($i=1; $i <count ($d); $i ++) fputs ($fw, trim ($d [$i]). “n”);

fclose ($fw);

}

// If, for some reason, the file has not been downloaded,

// That we shall use kehshirovannoj the version

else $d = file (“/tmp/news.cache”);

}

// A conclusion of the data

for ($i=0; $i <11; $i ++) {

$msg = $ d [2 + $ i*8];

$url = $ d [3 + $ i*8];

$date = $ d [4 + $ i*8];

print ” <a href =’http: // news.novgorod.ru ” .trim ($url). ” ‘> “;

print trim ($msg.) ” </a> (“. $date. “)”;

}

That’s all. In the same way it is possible kehshirovat` and SQL searches. Using this mechanism I managed to reduce time of creation of the main page with 1.5 sek till 0.05 sek

How to send letters PHP attachami

How to send the letter in HTML a kind? Attach to the letter attach with the name message.html and the letter to turn to the HTML-letter!

<?

// Functions. It is possible to bear{take out} in dpugoj a file.

class html_mime_mail {

var $headers;

var $multipart;

var $mime;

var $html;

var $parts = array ();

function html_mime_mail ($headers = “”) {

$this-> headers = $ headers;

}

function add_html ($html = “”) {

$this-> html. = $ html;

}

function build_html ($orig_boundary, $kod) {

$this-> multipart. = ” – $orig_boundaryn “;

if ($kod == ‘w’ || $kod == ‘win’ || $kod == ‘windows-1251′) $kod =’windows-1251 ‘;

else $kod =’koi8-r ‘;

$this-> multipart. = ” Content-Type: text/html; charset = $ kodn “;

$this-> multipart. = ” BCC: del@ipo.spb.run “;

$this-> multipart. = ” Content-Transfer-Encoding: Quot-Printednn “;

$this-> multipart. = ” $ this-> htmlnn “;

}

function add_attachment ($path = “”, $name = ” “, $c_type = “application/octet-stream”) {

if (! file_exists ($path. $ name)) {

print ” File $path. $ name dosn’t exist. “;

return;

}

$fp=fopen ($path. $ name, “r”);

if (! $fp) {

print ” File $path. $ name coudn’t be read. “;

return;

}

$file=fread ($fp, filesize ($path. $ name));

fclose ($fp);

$this-> parts [] =array (“body” => $file, “name” => $name, “c_type” => $c_type);

}

function build_part ($i) {

$message_part = “”;

$message_part. = ” Content-Type: “. $this-> parts [$i] ["c_type"];

if ($this-> parts [$i] ["name"]! = ” “)

$message_part. = “; name = ” “. $this-> parts [$i] ["name."] ” “n”;

else

$message_part. = “n”;

$message_part. = ” Content-Transfer-Encoding: base64n “;

$message_part. = ” Content-Disposition: attachment; filename = ” “.

$this-> parts [$i] ["name."] ” “nn”;

$message_part. = chunk_split (base64_encode ($this-> parts [$i] ["body"])). “n”;

return $message_part;

}

function build_message ($kod) {

$boundary = ” = _ “.md5 (uniqid (time));

$this-> headers. = ” MIME-Version: 1.0n “;

$this-> headers. = ” Content-Type: multipart/mixed; boundary = ” $ boundary “n”;

$this-> multipart = “”;

$this-> multipart. = ” This is a MIME encoded message.nn “;

$this-> build_html ($boundary, $kod);

for ($i = (count ($this-> parts)-1); $i> =0; $i-)

$this-> multipart. = ” – $boundaryn “. $this-> build_part ($i);

$this-> mime = ” $this-> multipart – $boundary – n “;

}

function send ($server, $to, $from, $subject = “”, $headers = “”) {

$headers = ” To: $tonFrom: $fromnSubject: $subjectnX-Mailer: The Mouse! n$headers “;

$fp = fsockopen ($server, 25, both $errno, and $errstr, 30);

if (! $fp)

die (” Server $server. Connection failed: $errno, $errstr “);

fputs ($fp, ” HELO $servern “);

fputs ($fp, ” MAIL FROM: $fromn “);

fputs ($fp, ” RCPT TO: $ton “);

fputs ($fp, “DATAn”);

fputs ($fp, $this-> headers);

if (strlen ($headers))

fputs ($fp, “$headersn”);

fputs ($fp, $this-> mime);

fputs ($fp, “n.nQUITn”);

while (! feof ($fp))

$resp. = fgets ($fp, 1024);

fclose ($fp);

}

}

// *************************************************************************

//

// In quality attacha ppisoedinjaem the html-letter (opens automatically).

// The second attach – some file from the catalogue.

// So to cause all that is written above:

//

// *************************************************************************

$mail=new html_mime_mail ();

$mail-> add_html (” <html> <body> <center> <h2> Ppivet! <br> <br> “.

” <br> I Send a binary file [/bin/ls...] “.

” </h2> </center> </body> </html> “);

$mail-> add_attachment (“/bin / “, “ls”);

$mail-> build_message (‘ win ‘); // if not “win”, kodipovka koi8

$mail-> send (‘ POCHTOVYJJ_KHOST_VASHEGO_PROVAJJDERA ‘,

‘ TO WHOM _ (E-MAIL) ‘,

‘ OT_KOGO _ (E-MAIL) ‘,

‘ SUBJECT OF THE LETTER ‘);

//

// After arrival of the letter we swing on FTP original/bin/ls and it is compared with

// Imported from the letter:

//

// X:temp> fc/b ls ls2

// Comparison of files ls and LS2

// FC: distinctions are not found

//

//

// Attention! If you do not have file/bin/ls it is simple zakommentirujte a line

// $mail-> add_attachment (“/bin / “, “ls”) that the program did not try to attach

// To the letter necuhhestvujuhhie files.

//

?>

Comments are closed.

footer

Design & programming by: Web Development Company CONKURENT LLC 2003-2009.