PHP and Flat File Data Storage
June 16, 2007
Using PHP Commands with Flat File Text (txt) Documents
When working with data in flat files, the first thing you must do is open the file. This same function will also create the file if it does not already exist. Once open you can do several things with the contents. Fread () is used to read the contents of the file for as many bytes as you specify, and return the data to the PHP file.
The file () command retrieves data from a flat file, and puts it into an array that you can the use in your PHP script.
Similar to file () is file_get_contents () which also reads the file, but instead of creating an array, returns all the data in a string.
Another way to read a file is line by line. This is how it is read if you use the fgets () function. It then leaves the pointer at the end of the last line read.
If you want to add data to the file you can do so using the fwrite () command. This will write directly from the current position of the pointer.
If you are writing to a file and do not wish to overwrite what is already there, you need to find the end of the file using the feof () function. This will place the pointer at the end of the last data in the file.
PHP With Ajax
June 14, 2007
AJAX and PHP Part 1 – Dynamic HTML and Images
J.D.Campbell So you’re interested in AJAX? AJAX is a powerful addition to JavaScript for browser-to-server intercommunication. We will demonstrate a simple script that sends a GET or POST request to a form handling script on a server, then the server script will return a response to the browser XMLHttpRequest JavaScript object.
A brief technical explanation of AJAX
AJAX is a client/server system. It is a standard that is compatible with all versions of IE browsers, Mozilla/Firefox browsers, the Opera browser and some others. Any browsers that were released in the last few years will work. It uses a JavaScript object commonly referred to as the XMLHttpRequest object within the client-browser HTML page, though there are some variations of this object in MS Internet Explorer browsers. We will demonstrate a simple script that is compatible with all browsers. You use this XMLHttpRequest object to simply send a GET or POST request to a Form handling script on a server, then the server script returns a response to the browser XMLHttpRequest JavaScript object. This text response can be plain text, HTML text with all tags and images or an XML-structured document.
In Part 2 we will cover XML document usage, but here we will cover plain text and full HTML text and images dynamically downloaded using the XMLHttpRequest object. Because the server only needs a script which can handle standard form requests, those being GET or POST, you can thus use Microsoft IIS server, Apache server with PHP or Perl, and Tomcat or any Java-based server. Our examples are of course PHP-based, and have been tested on a Linux Apache/PHP-based server with various browsers.
So you’re interested in AJAX? AJAX is a powerful addition to JavaScript for browser-to-server intercommunication. We will demonstrate a simple script that sends a GET or POST request to a form handling script on a server, then the server script will return a response to the browser XMLHttpRequest JavaScript object.
A brief technical explanation of AJAX
AJAX is a client/server system. It is a standard that is compatible with all versions of IE browsers, Mozilla/Firefox browsers, the Opera browser and some others. Any browsers that were released in the last few years will work. It uses a JavaScript object commonly referred to as the XMLHttpRequest object within the client-browser HTML page, though there are some variations of this object in MS Internet Explorer browsers. We will demonstrate a simple script that is compatible with all browsers. You use this XMLHttpRequest object to simply send a GET or POST request to a Form handling script on a server, then the server script returns a response to the browser XMLHttpRequest JavaScript object. This text response can be plain text, HTML text with all tags and images or an XML-structured document.
In Part 2 we will cover XML document usage, but here we will cover plain text and full HTML text and images dynamically downloaded using the XMLHttpRequest object. Because the server only needs a script which can handle standard form requests, those being GET or POST, you can thus use Microsoft IIS server, Apache server with PHP or Perl, and Tomcat or any Java-based server. Our examples are of course PHP-based, and have been tested on a Linux Apache/PHP-based server with various browsers.
AJAX Example client #1
To get started, we will provide you with the full text listing of our client JavaScript/DHTML page, and we will be assuming you know something about JavaScript and HTML. This HTML page will make a GET request to a PHP server script named “getajax.php”. That server script will then return a response in HTML which includes image links so the client browser can then use JavaScript to update certain elements of the page, dynamically showing the formatted HTML and images, and no refreshing of the client page will occur–that is the beauty of AJAX.
In the following HTML example, note the important factors of AJAX:
The function getXHTTP() creates an XMLHttpRequest object for any browser, and returns that object.
The var http = getXHTTP(); statement puts the XMLHttpRequest object into the http variable.
The function doHttpRequest() uses the http variable with the http.open method and the http.send method to do the data request to the PHP server script located at http://www.jdcampbell.com/ajax/getajax.php.
Also, the function doHttpRequest() contains the statement http.onreadystatechange = getHttpRes; which specifies the getHttpRes function as the handler function for the requested data, and this handler function is automatically called when the data is received from the PHP server script.
Here is the full HTML listing including all required JavaScript functions:
<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Strict//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd”>
<html xmlns=”http://www.w3.org/1999/xhtml” >
<head>
<title>Advanced AJAX Part 1</title>
<script language=”javascript” type=”text/javascript”>
function doHttpRequest() { // This function does the AJAX request
var c = document.getElementById(“code”).value;
http.open(“GET”, “http://www.jdcampbell.com/ajax/getajax.php”, true);
http.onreadystatechange = getHttpRes;
http.send(null);
}
function getHttpRes() {
if (http.readyState == 4) {
res = http.responseText; // These following lines get the response and update the page
document.getElementById(‘txt1′).value = res;
document.getElementById(‘div1′).innerHTML = res;
document.getElementById(‘cell1′).innerHTML = res;
}
}
function getXHTTP() {
var xhttp;
try { // The following “try” blocks get the XMLHTTP object for various browsers…
xhttp = new ActiveXObject(“Msxml2.XMLHTTP”);
} catch (e) {
try {
xhttp = new ActiveXObject(“Microsoft.XMLHTTP”);
} catch (e2) {
// This block handles Mozilla/Firefox browsers…
try {
xhttp = new XMLHttpRequest();
} catch (e3) {
xhttp = false;
}
}
}
return xhttp; // Return the XMLHTTP object
}
var http = getXHTTP(); // This executes when the page first loads.
</script>
</head>
<body>
<form action=”post”>
Enter code: <input type=”text” size=”5″ name=”code” id=”code” onblur=”doHttpRequest();” /> <input type=”button” value=”Press” name=”btn”>
<br><br><br>
Return data…
<textarea id=”txt1″ name=”txt1″ rows=”5″ cols=”60″ style=”width: 100%”>empty</textarea>
</form>
<div id=”div1″>Some text.</div>
<br>
<table width=100% border=1 cellspacing=0>
<tr><td id=”cell1″>cell1</td><td>cell2</td><tr>
</table>
</body>
</html>
The above code references a PHP server script at http://www.jdcampbell.com/ajax/getajax.php (the getajax.php script is live on the web). So you could actually copy the above client #1 script to your computer, run it, and it will work and do an AJAX dynamic download and update of your browser page. However, if you have access to any PHP-based Web server you can put the getajax.php script on your server and edit it to return any DHTML data you wish to be dynamically displayed in your AJAX client browser page. The HTML/text returned by the server script should not include the and and tags.
AJAX client #1 described
When this page first loads the script, line var http = getXHTTP() is executed. This creates a newly created XMLHttpRequest object for use by this AJAX client in the various JavaScript functions. Then it makes a call to doHttpRequest() when the user changes focus (that is the onblur event) from the Input text box named “code”; this onblur event of the “code” textbox is what initiates the AJAX request process by calling the doHttpRequest() function. At this point, the client HTML page has no changes. When the XMLHttpRequest object receives a response from the server PHP script, the function getHttpRes() is automatically called, which updates the specific client HTML page elements with the data returned by the PHP server script. These are the lines which update the HTML client page elements:
document.getElementById(‘txt1′).value = res;
document.getElementById(‘div1′).innerHTML = res;
document.getElementById(‘cell1′).innerHTML = res;
The JavaScript statements above take the contents of the res variable (which contains the returned data of the AJAX request) and updates the “txt1″ Textarea element contents, and the “div1″ Div HTML element contents, and the “cell1″ Table cell element contents. All of these elements are within the last 14 lines of the client #1 HTML page listing above.
If you know a little about JavaScript and HTML, then you know that some elements of an HTML page can contain full HTML, such as div sections and table cells, and as long as you specify an id=”idname” attribute of these elements you can dynamically update them. Other HTML elements like textareas and textboxes of forms can only contain plain text. That is why the first line of our code snippet above uses the property assignment of .value = res (for plain text) and the last two lines of the snippet use the assignment of .innerHTML = res (for full HTML). The last two lines with the .innerHTML = res assignment are assigning the AJAX server response data which is full HTML to the div1 and cell1 page elements with those exact ids. When this occurs, you will see the contents of the table cell1 change to some dynamic HTML, including an image. Additionally, the div1 section will show the same HTML/image content.
For developing AJAX-powered HTML pages which produce dynamic HTML content within elements of the page, you will use the .innerHTML property assignment a lot. Almost any HTML page element that supports this .innerHTML property can contain dynamically updated HTML. You could check a JavaScript reference to see all the HTML page elements that support the .innerHTML property, but basically the “innerHTML” property can be updated (read/write) for all objects in DHTML except the following: COL, COLGROUP, FRAMESET, HTML, STYLE, TABLE, TBODY, TFOOT, THEAD, TITLE, and TR.
Install PHP and Apache in Win32
June 14, 2007
Installing Apache and PHP under Win32
Dennis Wrenn If you are on this site, we’re sure that you already know that PHP is a server side web programming language. Its purpose is very similar to ASP, but in my opinion, it’s better. There are more databases available to use with it and ASP can only communicate with Access and SQL servers. PHP can communicate with SQL Server, MySQL, mSQL, and several others, but that is not the topic of this tutorial.
What’s Apache?
Apache is a web server, similar to PWS, and IIS. They, and it, listen(s) on a specific port (usually it’s 80) and allow you to run a website off of your home computer. Neat, Eh?
Installing Apache
First you need to download Apache Do that here. It’s fairly large (about 3.6mb), so be patient. After downloading, double click the exe. Installing is a breeze, but configuring isn’t that easy.
Configuring Apache
Move to your apache directory after installing. It’s usually C:\Program Files\Apache Group\Apache. Look for, and open, a folder called conf. Open the file called httpd.conf. It may look cryptic and confusing at first, but about half the file is made up of comments, which begin with a “#” mark.
The parts in red need to be found, and the parts in green need to be changed or added. (NOTE: I removed all the file comments for the sake of size – those comments shown below are there just as a guide for you.)
DocumentRoot “C:/website”
#NOTE: you can change c:/website
#to anything you want, that is just
#happens to be where I store my files.
#Another NOTE: All slashes must be forward /
<Directory />
Options FollowSymLinks ExecCGI
AllowOverride None
</Directory>
<Directory “C:/website”>
Order allow,deny
Allow from all
</Directory>
After changing that, place a file (we’ll call ours filename.htm) to the document root you set. Then start Apache (by clicking Start button -> Program Files -> Apache -> Management -> Start Apache). Now use your favorite browser, and type in:
http://localhost/filename.htm
You should get your file. Cool. If so, keep reading. If not, then email me and tell me what the problem is. Be sure to send the complete httpd.conf file.
Installing PHP
This part is fairly easy, the PHP homepage is php.net, you can download the PHP 5.2.3 installer here. This file is quite big too. So, again, be patient. Now, unzip the file to C:\php.
Stop Apache, if it isn’t already. Copy php4ts.dll to the Windows\System directory. In httpd.conf search for #LoadModule it should bring you to a list of modules that can be loaded. Leave those alone, but directly under them, add these lines:
LoadModule php4_module c:/php/sapi/php4apache.dll
AddType application/x-httpd-php .php
Now, write a file called test.php, with this as the content:
<?php
echo “PHP Rules!”;
?>
Save the file, and put it into the directory you set as the document root. You set that up above. Now start up Apache, and type this into the location bar of your favorite web browser:
http://localhost/test.php
Review Database McKoi SQL ( Java Database)
June 13, 2007
Contributed by: Isak_Rickyanto
Ini adalah artikel share / review tentang penggunaan database open source berbasis Java yang bernama McKoi. Penulis review ini adalah Samuel Franklyn.
Pertama kali saya mengetahui ada database Java adalah pada saat saya
menggunakan JBoss. Dalam JBoss ada database Java yang bisa diakses lewat
JDBC API yang nama Hypersonic SQL. Hypersonic SQL ini sekarang namanya
HSQL dan bisa didownload dari http://hsqldb.sourceforge.net/
Saya pelajari database ini ternyata featurenya kurang banyak serta
database ini baru menyimpan data ke file waktu JBoss di shutdown.
Database ini menyimpan dan mengolah semua data dalam database di dalam
memori. Jadi kalau data anda ratusan MB maka database ini juga akan
memakai ratusan MB memori. Kesimpulan saya database ini tidak bisa
dipakai untuk aplikasi bisnis yang serius.
Database Java kedua yang saya ketahui adalah database Cloudscape yang
diikut sertakan dalam J2EE SDK dari Sun. Sayangnya database ini database
komersial. Jadi saya malas mempelajarinya.
McKoi adalah database Java ketiga yang saya kenal. Barulah pada McKoi
saya menemukan database Java yang memuaskan. Featurenya memang tidak
terlalu banyak tapi sudah cukup untuk bisa dibilang RDBMS.
Feature Database McKoi:
- Berbasis file jadi hemat memori
- Besar penggunaan memori bisa dikonfigurasi bisa juga dinamik
- Menggunakan automatic index artinya setiap field diindex.
Ini menghilangkan keperluan untuk tuning dengan konsekuensi insert
yang lebih lambat untuk tabel dengan banyak field. Tapi feature ini
bisa dikonfigurasi
- Relational integrity: Primary Key, Foreign Key, dll.
- Fasilitas transaksi
- Fasilitas field auto-number
- Bisa hot-backup artinya dibackup selagi databasenya masih berjalan
- Bisa digunakan dalam mode embedded atau client/server
Salah satu manfaat terbesar McKoi adalah kalau digunakan sebagai
database yang embedded. Dengan cara ini aplikasi kita bisa diberikan
kepada pemakai dalam bentuk file WAR atau JAR dan langsung bisa
dijalankan. Jadi tidak ada lagi langkah pasang database, ciptakan tabel,
isi tabel dengan nilai awal dan konfigurasi data source. Tentu saja
penggunaan McKoi secara embedded untuk tujuan komersial memerlukan
lisensi komersial soalnya McKoi lisensinya GPL. Tapi kalau kita pakai
McKoi dalam aplikasi GPL maka kita tidak perlu beli lisensi.
McKoi kalau menurut pendapatku bisa dipakai dengan mulus untuk data
tingkat ratusan MB sampai GB. Aku sudah benchmark sederhana performance
select nya cepat sekali cuma kalah cepat sedikit dari MySQL. Hanya saja
performance insert jauh lebih lambat dari MySQL. Tapi ini karena semua
field di index oleh McKoi dan seluruh insert diproteksi pakai transaksi.
Sedangkan di MySQL aku tidak pakai tabel tipe InnoDb yang terlindung
transaksi melainkan cuma tabel MyISAM. Jadi performance insert McKoi dan
MySQL tidak bisa dibandingkan.
Sebenarnya kalau memorynya terlalu low juga McKoi tidak jalan. Raja
efisiensi database dalam pemakaian resource adalah MySQL. Kalau
clientnya memorynya low ya paling bagus pakai MySQL.
Keunggulan McKoi dibanding MySQL adalah jalan dimanapun ada JVM yang
cocok. Jadi misalnya kita pakai Digital Alpha. Nah di platform ini kita
musti kompilasi sendiri MySQL soalnya rasanya tidak ada binarynya di
situs MySQL. Kalau pakai McKoi langsung saja WAR kita yang sudah berisi
mckoidb.jar di taruh dalam Tomcat dan dijalankan. Langsung jalan deh
aplikasi kita. Simple sekali. Jadi keunggulan McKoi adalah keunggulan
Java juga yaitu WORA (Write Once Run Anywhere) dan embedded. Karena
embedded tidak ada perlu langkah setup database.
Situs McKoi SQL
http://mckoi.com/database/