Archive for November, 2006

of the file. This is applicable for all

Monday, November 20th, 2006

of the file. This is applicable for all read calls that take file handle fpas an argument. The following code reads the entire file: if (!($fp = fopen(”a.txt”, “r”))( { printf(”Could not open file a.txt”); } else { while ($buffer = fread($fp, 100)) { // Process the read data } } string fgetc(int fp) The function fgetc() is used to read a single character from the open file. It returns a one character string at the current position of the file handle and increments the current position by one. It returns false(empty string) on reaching the end of the file: string fgets(int fp, int length) The function fgets() reads and returns a string, starting at the current position of the file handle, of up to “length-1″ bytes. The reading ends when length 1 bytes have been read, or a new line or end of file is reached. As a side effect of this call the current position of the file handle points to the next unread character: string fgetss(int fp, int length [,string allowable_tags]) The function fgetss() is identical to fgets(), except that any HTML and PHP tags are stripped from the string. The argument allowable_tags can be used to specify the list of comma separated tags, which should not be stripped. Note that the tags are still counted towards the length of the string: array file(string filename [,int use_include_path]) The function file() reads the contents of file filenameinto an array. The function returns an array, where each element of the array corresponds to a line, with the end of line (line feed and carriage return) character still attached, in the file on success. The file() function should be used to read small files only. It should never be used to read big files, as it may unnecessarily increase the memory footprint of the PHP interpreter. The following code reads the contents of a.txtand outputs it as an HTML document:

Hint: This post is supported by Gama besplatan domen provider

by the operating system to maintain the context

Monday, November 20th, 2006

by the operating system to maintain the context of the opened file. Among other things it stores the current data pointer, which gets incremented after read or write calls. The returned file handle should be referred to in all the subsequent file related functions. The following code opens the binary file C:tempjob.jpgfor reading: if (!($fp=fopen(”c:/temp/job.jpg”, “rb”))) { printf(”Could not open file job.jpg”); } Closing Files int fclose(int fp) The fclose()function is used to close an opened file. The argument fp is the file pointer of the file to be closed. This function returns true on success or falseon failure. It is important to close an open file when you have finished reading or writing it, to free it for access by other scripts or programs. There are operating system specific limits on the number of files that can remain open at any time. Displaying Files int fpassthru(int fp) The contents of an opened file can be sent to the browser using the fpassthru()function. This function reads the content of the file from the current position in the open file to the end of the file, and writes it to the standard output. The argument fp is the file pointer of the open file. This function returns true on success, falseon failure. If the opened file is a binary file, then open the file using bflag. Otherwise the browser will not be able to render the file properly. The readfile() method can also be used to send the contents of the file to the browser, but use fpassthru() to send the contents of a binary file. The following code sends the contents of the binary file C:tempjob.jpgto the browser: if (!($fp=fopen(”c:/temp/job.jpg”, “rb”))) { printf(”Could not open file job.jpg”); } else { fpassthru($fp); } int readfile(string filename) The readfile() method takes the file name as an argument. This method assumes the file is a text file. It returns true on success and falseon failure. Reading from Files string fread(int fp, int length) The fread()function can be used to read a string from the open file. The fread()function returns a string of up to length characters from the file with the pointer fp. If the end of the file is reached before the specified length, then the text up to that point is returned. As a side effect of this call the current position of the file pointer is changed to the next unread character Page 238
Note: If you are looking for top 10 and very good webhost to host and run your jsp application check Actions jsp hosting services

Files A file is a sequence of bytes

Sunday, November 19th, 2006

Files A file is a sequence of bytes stored persistently on a physical medium like a hard disk. Each file is uniquely identified by its absolute path. For example, C:temptextfile.txt on Windows platforms or /private/user/textfile on UNIX platforms. On Windows, both the forward slash (/) and the backslash () can be used in file paths, whereas other operating systems use only the forward slash. As a good programming practice, you should define a global variable fileSeparator and use it for creating paths in your PHP programs. This is an extreme scenario, but, in some operating systems the forward slash (/) can be part of the filename. Most PHP applications follow these steps to read from or write to a file: . Open file . Read from and write to file . Close file Opening Files The fopen() function can be used to open any file in the server’s file system. It can also be used to open files via HTTP or FTP on the Internet. Here we will only look at how this function can be used to open files in the server’s file system: int fopen(string filename, string mode [, string use_include_path]) The argument filename can be the name of the file, or its absolute path. If filename is the name of the file, then it is assumed that the file is in the current working directory. The second argument indicates whether the file is to be opened for reading, writing, or appending. It can have one of the following possible values: Value Description R Open the file for reading only. r+ Open the file for reading and writing. W Open the file for writing only and truncate the file to zero length. All the contents of the file will be lost. If the file does not exist, PHP will try to create it. w+ Open the file for reading and writing. Truncate the file to zero length. If the file does not exist, PHP will try to create it. A Open a file for appending data. Data will be written at the end of an existing file. If the file does not exist, then PHP will try to create it. a+ Open a file for appending and reading data. Data will be written at the end of an existing file. If the file does not exist, then PHP will try to create it. B This flag must be used to read/write binary files on operating systems such as Windows that handle such files differently. The third argument specifies whether PHP should search for files in the include_path too. The include_path can be set in the php.ini configuration file. The fopen() function returns a file handle if successful, or false on failure. The file handle is used Page 237
Note: If you are looking for good and high quality web space to host and run your java application check Vision java hosting services

Chapter 9: File Handling Overview PHP applications that

Sunday, November 19th, 2006

Chapter 9: File Handling Overview PHP applications that work with persistent data mainly use two options for storing data: relational databases and the local file system. Relational databases are more suited for storing structured data, where applications access data based on some search condition. For example, employee information, which contains components like firstname, lastname, employeeid, and salary, should be stored in a relational database. An application can then access the employee information using firstname, lastname, employeeid, or salary as search criteria. File systems are useful for storing simple data like configuration files and unstructured data like images or word processing documents. Here, the text editor application will access the data using the absolute path of the file where the data is stored. In this chapter we will cover: . PHP’s functions that allow us to store and retrieve data from the server’s local file system . A complete online storage application, which stores all its data in files Page 236

Hint: If you are looking for very good and affordable webspace to host and run your j2ee hosting application check Virtualwebstudio j2ee web hosting services

Summary In this chapter we have seen how

Sunday, November 19th, 2006

Summary In this chapter we have seen how to use PHP’s native cookie and session support to our advantage and how we can and should incorporate them into larger-scale business. We saw how to write custom session handling functions and why they are useful. Additionally, we covered what URL session propagation is, and why to avoid it. Page 235
Note: If you are looking for good and quality webspace to host and run your java application check Actions java hosting services

Some More Session Functions We have most of

Sunday, November 19th, 2006

Some More Session Functions We have most of the useful session functions in the above examples. There are some others too: string session_save_path([string path]) Returns the current path in which session files are stored. If the path is specified, it sets the path in which session files are stored: boolean session_is_registered(string name) Returns true if the name passed to the function is a registered session variable, and false if it is not: array session_get_cookie_params() Returns an array of items relating to the session cookie. These items are lifetime (the cookie’s expiration), path (path on the server of files that can access the cookie), domain (the domain setting the cookie), and secure(whether or not the cookie can be accessed outside of a secure connection): void session_set_cookie_params(int lifetime [, string path [, string domain]]) Sets the session cookie values relating to the lifetime, path, and domain of the cookie. This function only lasts for the duration of the script and cannot set the cookie to be required over a secure channel. boolean session_decode(string data) Decodes the session data in the variable passed to the function and stores it in global variables. string session_encode Inserts all session data into a string. This function is useful for passing all session data through the URL (if we are transferring the user to a different server), or to save in a database for later use. string session_cache_limiter([string cache_limiter_string]) Returns the current method of cache limiting if the string isn’t set. Otherwise, caching is set to the method specified in the string. For example, nocache to disable both private and public client-side caching. Though both allow client-side caching, private is slightly more restrictive. Page 234

Hint: This post is supported by Gama hrvatski web hosting services

if (!$test) { // The cookie doesn’t exist

Sunday, November 19th, 2006

if (!$test) { // The cookie doesn’t exist echo (”Please enable cookies in your browser.”); } else { // The cookie exists, send them to a page with cookie support. header(”Location: http://yourserver.com/next.php”); } } ?> Page 233
Note: If you are looking for reliable and quality webspace company to host and run your servlet application check Actions servlet hosting services

} else { echo(” times!”); } ?> The

Sunday, November 19th, 2006

} else { echo(” times!”); } ?> The following error does not show up if the error messages are turned off, and would be virtually impossible to find in a large script with many cookies being set, sessions started, or other header() calls. The following output comes from the above error-prone code: If an error message like this is generated, check to see if any HTML or PHP generated text appears before the setcookie() function call. Also be careful to check for any require() or include() calls that may generate text or send data to the browser. If these check alright, then locate the php.ini file and look for the auto_prepend directive which will automatically require()PHP data into the beginning of your files. If we haven’t compiled PHP with this option, we will not be able to access our cookies by using the variable referenced to the name of the cookie (that is, $cookiename). In this case we will have to access cookies by using $HTTP_COOKIE_VARS[ cookiename ]. It is not necessary to rebuild PHP to fix this; it is changeable. Another common error found in cookie-related code is that the name is set incorrectly. The name for a cookie can only be alphanumeric. Any characters other than a-z, A-Z, 0-9, hyphen (-), and underscore (_) are automatically converted to the underscore character. This means if we use setcookie(”my.initials”, “d.h.o.”), the cookie name would be changed to my_initials. Consequently, we would call the variable set from the cookie $my_ initials, even though it would still hold the value “d.h.o.” However useful cookies are, they should not be required for an application to run. If for some reason, we absolutely require cookies for our application, include code to check for cookies on the user’s side and tell them to turn their cookie acceptance on. The following code will perform a simple check to see if cookies are enabled:

Hint: If you are looking for very good and affordable webspace to host and run your j2ee hosting application check Virtualwebstudio j2ee web hosting services

?> Welcome back, ! Welcome, visitor. We strive

Sunday, November 19th, 2006

?> Welcome back, !

Welcome, visitor. We strive to be as user-friendly as possible, so if you’ll please leave us your name, we’ll kindly greet you on your next visit!

Your name:

When you first visit the site, this is the page you will receive: Clicking Send Me! posts the text. Then, every time the page is visited (before the browser is closed), we receive this page: Problems with Cookies Several mistakes are commonly made when using the setcookie()function. The most common is in trying to set cookie data after the data has already been sent to the client. For example: Putting text or HTML tags here will force actual page content to be sent, causing the cookie to result in error. Thank you for visiting my site. You’ve seen this page Note: If you are looking for high quality webhost to host and run your jsp application check Vision jsp hosting services

these names simply causes PHP to receive them

Sunday, November 19th, 2006

these names simply causes PHP to receive them as components of a single array variable, instead of as separate variables. In the following example, we include both the username and the number of times that user has viewed the site in the cookie. In reality, two cookies are created with names cookie[0] and cookie[1]:


We can make interesting applications using cookie methods and definitions: Note: If you are looking for good and high quality web space to host and run your java application check Vision java hosting services