Student Of Fortune
Showing posts with label PHP. Show all posts
Showing posts with label PHP. Show all posts

Looping On PHP

Simple script to print the letters or numbers to over and over again. This process is called looping / iteration by using PHP.
<?php
$kata = $_GET['kata'];
$byk = $_GET['banyak'];

echo “Kata : $kata<br>Cetak Sebanyak : $byk<br><br>Hasil : <br>”;
for($i=0;$i<$byk;$i++){ echo “$kata cetak ke-$i<br>”; }
?>
I wish you like.

PHP Ebook, E-book PHP Security Guide

Previous thank you for all, because you all still have time to visit the ajonksb.blogspot.com.

Now I will give an e-book to learn PHP. On this occasion, I want to give Ebook PHP for application security in PHP. there are some concepts that are described in the E-book this time, ranging from the introduction of what is security? then Data Filtering, Form Processing, Databases and SQL and some concepts that I have not graft it in this tutorial. some of the concepts described in this Ebook. as below.


What Is Security?

Security is a measurement, not a characteristic.
It is unfortunate that many software projects list security as a simple requirement to be met. Is it secure? This
question is as subjective as asking if something is hot.

Security must be balanced with expense.
It is easy and relatively inexpensive to provide a sufficient level of security for most applications. However, if your
security needs are very demanding, because you're protecting information that is very valuable, then you must
achieve a higher level of security at an increased cost. This expense must be included in the budget of the project.

Security must be balanced with usability.
It is not uncommon that steps taken to increase the security of a web application also decrease the usability.
Passwords, session timeouts, and access control all create obstacles for a legitimate user. Sometimes these are
necessary to provide adequate security, but there isn't one solution that is appropriate for every application. It is wise
to be mindful of your legitimate users as you implement security measures.

Security must be part of the design.
If you do not design your application with security in mind, you are doomed to be constantly addressing new security
vulnerabilities. Careful programming cannot make up for a poor design.

Creating a Report (Report) PDF with PHP and HTML2FPDF

For those of us who frequently engaged in the world of web development, certainly never asked to make an application in which there is the report module. In terms of making the report, we sometimes had difficulty in determining the format and libraries that will be used. Some frequently used report format is HTML (and CSS), PDF, Image, CSV and Excel (Spreadsheet). Each format has advantages and limitations of each.

PDF
PDF (Portable Document Format) is one of the options report format often used in web-based applications. Those advantages include a standard format and can be displayed in all operating system platforms (cross platform). In addition, more secure PDF format from the side of safety and validity of the data presented. PDF is also more easily stored or archived for in the form of a file. But on the other hand, deficiency of the current PDF is still seldom available web-based PDF creation libraries are pretty easy to use but powerful.

HTML2FPDF
After doing a search on the internet, I finally found one class (library) PHP that is reliable and easy in terms of making a PDF file. Library is named HTML2FPDF, and is located at http://html2fpdf.sourceforge.net/. In essence, how the work done by the library is changing the HTML commands into the PDF. So, for us who will make the report quite make it through the HTML commands which of course we are more familiar. Next we live "ordered" library HTML2FPDF to turn it into a PDF file. 

HTML2FPDF itself use FPDF library in terms of making a PDF file. So in addition to his classes, we also require the FPDF library on our servers. HTML2FPDF can be obtained from the official website at http://html2fpdf.sourceforge.net/ free of charge (free). But the site does not include library FPDF, so we have to add it yourself. As an alternative, you can get the full version of the library HTML2FPDF (including FPDF and some examples of the program) at the following address:

HTML2FPDF (327.4 KiB, 8,821 hits)  

Example Usage HTML2FPDF
To create a PDF report with the class (library) HTML2FPDF, basically can be done in 2 (two) ways. The first way is to create an HTML file first and then read the contents and send it to the class to converted to PDF. Then the second way is to create HTML files directly in the program code (PHP). Both are basically the same.

Here's an example program to convert files "sample.html" to PDF.
See The Code :
  1. <?
  2. require('html2fpdf/html2fpdf.php');
  3. $pdf=new HTML2FPDF();
  4. $pdf->AddPage();
  5. $fp = fopen("sample.html","r");
  6. $strContent = fread($fp, filesize("sample.html"));
  7. fclose($fp);
  8. $pdf->WriteHTML($strContent);
  9. $pdf->Output("sample.pdf","I");
  10. //echo "PDF file is generated successfully!";
  11. ?>
And here are examples of programs to generate a PDF where the HTML code directly written in PHP.
See The Code :
  1. <?php
  2. require('html2fpdf/html2fpdf.php');
  3. $pdf=new HTML2FPDF();
  4. $pdf->AddPage();
  5. $strContent = "<h1 align=center>Hello World!</h1>
    <p>Now I can easily make a PDF report</p>";
  6. $pdf->WriteHTML($strContent);
  7. $pdf->Output("sample2.pdf","I");
  8. //echo "PDF file is generated successfully!";
  9. ?>
Hopefully useful
Reference
http://html2fpdf.sourceforge.net, HTML2FPDF Official Site
HTML2FPDF using PHP
http://achmatim.net/2008/11/20/membuat-laporan-report-pdf-dengan-php-dan-html2fpdf/#codesyntax_2

Fastest algorithm Printing Prime's Numbers

This tutorial discusses the print primes quickly. A prime number is required not only to 100 or 1000, but up to 100 million.

Sieve of Eratosthenes
After searching on Google, we found one of the classic algorithm to print the prime numbers. The algorithm is called the Sieve of Eratosthenes, I found on Wikipedia. Maybe this is not the fastest algorithm, but at least fast enough compared to when using modulus.

The following algorithm is quoted from Wikipedia.
Suppose we want to find all prime numbers between 1 to an integer n.

  1. Write down all the numbers, ranging from 1 to n. Suppose this is the list A.
  2. Create a list that is still blank, call it a list B.
  3. Strikeout number 1 from list A.
  4. Then write 2 on list B. Then streak 2 and all the increments from the list A
  5. The first number that has not crossed from the list A (eg 3) is prime. Write these numbers in list B, then streak this number and all the increments from list A.
  6. Repeat step 4 until all the numbers in list A are crossed.
Once completed, all numbers in list B is prime.
And here are examples of the application of the above algorithm in the programming language PHP. This script has been tested to show the prime numbers below 1,000,000 and successfully display it within 3 seconds.
See The Code  

  1. <?php
  2. function bilangan_prima($limit) {
  3. $prima = array();
  4. $prima_awal = array(2,3,5,7);
  5. for ($i=2; $i<=$limit; $i++)
  6. $prima[$i] = true;
  7. foreach ($prima_awal as $awal) {
  8. for ($i=2*$awal; $i<=$limit; $i+=$awal) {
  9. $prima[$i] = false;
  10. }
  11. }
  12. foreach ($prima as $bilangan=>$status) {
  13. if ($status) echo "$bilangan ";
  14. }
  15. }
  16. $start=mktime();
  17. bilangan_prima(1000000);
  18. $finish=mktime();
  19. $result=$finish-$start;
  20. echo "<br>Time: $result seconds";
  21. ?>

Crop image in PHP

This tutorial will play around with the image. Prepare a picture with the name "gambar1.jpg". Put this image in a folder with a PHP file. After that create a PHP script with the name "crop.php". If so, type the following script:
See The Code :
crop.php


<? php

/ / specify width, height, file you wish to crop, format file name, and the placement of the image after the crop

cropImage function ($ nw, $ nh, $ source, $ stype, $ dest) {

                 $ size = getimagesize ($ source); / / size of image

                 $ w = $ size [0];

                 $ h = $ size [1];

                 switch ($ stype) {/ / format image
case 'gif':

                                                 $ simg = imagecreatefromgif ($ source);

                                                 break;

                                 case 'jpg':

                                                 $ simg = imagecreatefromjpeg ($ source);

                                                 break;

                                 case 'png':

                                                 $ simg = imagecreatefrompng ($ source);

                                                 break;

                 }

                 $ dimg = imagecreatetruecolor ($ nw, $ nh); / / create new image

                 $ wm = $ w / $ nw;

                 $ hm = $ h / $ nh;

                 $ h_height = $ nh / 2;

                 $ w_height = $ nw / 2;

                 if ($ w> $ h) {
 
 $adjusted_width = $w / $hm;

                                $half_width = $adjusted_width / 2;

                                $int_width = $half_width – $w_height;

                                imagecopyresampled($dimg,$simg,-$int_width,0,0,0,$adjusted_width,$nh,$w,$h);

                } elseif(($w <$h) || ($w == $h)) {

                                $adjusted_height = $h / $wm;

                                $half_height = $adjusted_height / 2;

                                $int_height = $half_height – $h_height;

                                imagecopyresampled($dimg,$simg,0,-$int_height,0,0,$nw,$adjusted_height,$w,$h);

                } else {

                                imagecopyresampled($dimg,$simg,0,0,0,0,$nw,$nh,$w,$h);

                }

                                imagejpeg($dimg,$dest,100);

}
$gambar = “gambar1.jpg”;

$gambar_crop = “gambar1crop.jpg”;
/ / execute the function crop images

/ / width, height, file in the crop, image format, file name after the crop

cropImage (225, 165, '$ image', 'jpg', "$ gambar_crop");

print "Image <h2> before the crop: <br> <img src=$gambar> <br>";

print "Image after the crop: <br> <img src=$gambar_crop>";

?>
hopefully useful
 
Recommended Post Slide Out For Blogger

Recent Comments

My Rank