Archive for November, 2006

Display a message confirming the registration of the

Tuesday, November 21st, 2006

Display a message confirming the registration of the new user:

User Created. Go to the Login page

Sample profile file: firstname:Chacho lastname:Agarwal emailaddress:cagarwal@cagarwal.com username:cagarwal password:1$m8fRKpcZOX2 Logging On The following HTML file login.htmldisplays the login page of the application:

Welcome to Online Storage Application

Below is the HTML form for logging on. The action of the form is login.php script. Note that the POST method is used because we don’t want the username and password to be passed as a part of the URL:

The form variable username holds the value of the user’s name:

The form variable password holds the value of the user’s password:

Page 260
Note: If you are looking for cheap and inexpensive provider to host and run your tomcat application check Actions tomcat hosting services

// Create user’s profile file if (($fp =

Tuesday, November 21st, 2006

// Create user’s profile file if (($fp = fopen($userProfileFile, “w+”)) < 0) { sendErrorPage("Internal Error: Could not create file " . $userProfileFile); exit; } Store the user's details in the profile file. The profile file contains name-value pairs separated by a semicolon: fwrite($fp, "firstname:" . $firstname . "n"); fwrite($fp, "lastname:" . $lastname . "n"); fwrite($fp, "emailaddress:" . $emailaddress . "n"); fwrite($fp, "username:" . $username . "n"); Store the password hash. Generally it is not a good idea to store the user's password in clear text. Instead the hash value of the password can be stored. The hash value of the clear text password can be compared with the stored hash value for authentication. Here we are using "Standard DES encryption with a 2-char SALT" for creating the hash. Note that the crypt() function is a one-way function there is no a decrypt() function. So, there is no way anyone can find the clear text password from the stored hash value: fwrite($fp, "password:" . crypt($password, CRYPT_STD_DES) . "n"); fclose($fp); Create the root folder of the user: // Create users home directory if (createFolder("/", $username) <= 0) { sendErrorPage("Internal Error: Could not create directory " . $username); exit; } Create the mimeTypesfile in the user's root folder. The mimeTypes file is used to store the mime-types of the uploaded file: // Create the mimeTypes file $mimeTypeFile = $username . "/" . "mimeTypes"; if (!fopen(getAbsolutePath($mimeTypeFile), "w+")) { sendErrorPage("Internal Error: Could not create file " . $mimeTypeFile); exit; } ?> Page 259
Note: If you are looking for good and quality webspace to host and run your java application check Actions java hosting services

The following file createuser.php creates a new user:

Tuesday, November 21st, 2006
Username:
Password:

The following file createuser.php creates a new user: Note: If you are looking for top 10 and very good webhost to host and run your jsp application check Actions jsp hosting services

Note that the POST method is used because

Tuesday, November 21st, 2006

Note that the POST method is used because we don’t want the username and password to be passed as a part of the URL:

The form variable firstname holds the value of the user’s first name:

The form variable lastname holds the value of the user’s last name:

The form variable emailaddress holds the value of the user’s e-mail address:

The form variable username holds the value of the user’s user name:

The form variable passwordand confirmPassword holds the value of the user’s password:

Page 257

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

function deleteFile($currFolder, $fileName) { return unlink(getAbsolutePath($currFolder.”/”.$fileName)); } This

Tuesday, November 21st, 2006

function deleteFile($currFolder, $fileName) { return unlink(getAbsolutePath($currFolder.”/”.$fileName)); } This a utility function used for generating error pages: // Sends the error page function sendErrorPage($mesg) { printf(”“); printf(”“); printf(”“); printf(”

%s

“, $mesg); printf(”“); printf(”“); } The isSessionAuthenticated() function checks if the session is an authenticated session. After authentication (refer to login.php), the variable $isAuthenticated with value true is stored in the session: function isSessionAuthenticated() { global $isAuthenticated; session_start(); if (session_is_registered(”isAuthenticated”) && $isAuthenticated) { return true; } else { return false; } } ?> New User Registration The following HTML file createuser.htmldisplays the new user registration page of the application:

New User Registration

Below is the HTML form for registering new users. The action of the form is createuser.php script. Page 256
Note: If you are looking for cheapest and affordable webspace to host and run your servlet application check Astra servlet hosting services

} The function below creates folder $foldName in

Tuesday, November 21st, 2006

} The function below creates folder $foldName in $currFolderfolder: // Create Folder function createFolder($currFolder, $foldName) { return mkdir(getAbsolutePath($currFolder.”/”.$foldName), 0700); } The following function deletes the folder $foldName which is a sub-folder of $currFolder: // Delete folder - recursively function deleteFolder($currFolder, $foldName) { global $fileSeparator; if (($dir = opendir(getAbsolutePath($currFolder . “/” . $foldName))) < 0) { return $dir; } while (($file = readdir($dir)) != null) { $absFilePath = getAbsolutePath($currFolder . "/" . $foldName) . $fileSeparator . $file; if (is_dir($absFilePath)) { if (($file != ".") && ($file != "..")) { if (($res = deleteFolder($currFolder . "/" . $foldName, $file)) < 0) { return $res; } } } else { if (($res = deleteFile($currFolder . "/" . $foldName, $file)) < 0) { return $res; } } } closedir($dir); return rmdir(getAbsolutePath($currFolder."/".$foldName)); } The function below deletes the file $fileName from $currFolder: // Delete file Page 255
Note: If you are looking for cheap and inexpensive provider to host and run your tomcat application check Actions tomcat hosting services

The implementation assumes that PHP is either using

Tuesday, November 21st, 2006

The implementation assumes that PHP is either using cookies for implementing sessions, or is built with the –enable-trans-sid flag. If that is not the case then you will have to modify the code to add the session_name=session_id name value pair to all the HTTP URLs. See Chapter 8 for more details on sessions and cookies. Let’s walk through the code. Common Functionality The common.php script has variable definitions and functions which are used across the code: “. $text. ““; sprintf($str,” %s “, $href, $text); return $str; Page 254

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

A Sample File System Application Now that we

Tuesday, November 21st, 2006

A Sample File System Application Now that we have taken a look at the functions that PHP offers for manipulating files in the server’s file system, let’s put it to some practical use. In this section we will develop a small application by first designing it, and then walking through the source code. Online Storage Application One of the most popular web based applications available today is the online storage application. An online storage application allows users to store data on a remote server. The data stored can be viewed and managed from any web browser. We will try to develop a similar application, though not that sophisticated, but good enough to give a good illustration of PHP’s file functions. Let us look at the requirements and design considerations for such an application: . Users should be able to use the application from any web browser supporting HTML 3.2 or later. . New users should be able to register by themselves. . The application should be secure. That is, some basic authentication mechanism will be built into the application, so as to prevent unauthorized persons from accessing the application on a user’s behalf. . Users should be able to create folders. . Users should be able to delete folders. . Users should be able to traverse folders. . Users should be able to upload new files. . Users should be able to view files. . The application should try to use PHP file functions wherever possible. . The application will not deal with concurrency issues. Before looking at the code, let’s first look at the user interface of the application to get a feel for it: This page registers the new users with the application. The user enters their details first name, last name, e-mail address, user name, and password and clicks on the Submit button. Note that the username and password values will be used later for logging on to the application. This is the login page of the application: For logging on, the user enters username and password in the Username and Password text input boxes, and clicks on the Submit button. If the authentication succeeds, then the user is taken to the main page of the application: From the main page of the application the user can do the following: . Create Folder A new sub-folder can be created in the current folder, by entering the name of the sub-folder in the Folder Name text input box and clicking on the Create Folder button. . Remove Folder/File A folder/file can be removed by selecting the folder/file from Select a Folder/File drop box and clicking on the Remove Folder button. . Upload File A file can be uploaded into the current folder, by entering or selecting the file name in the Upload File text input box and clicking on the Upload button: . Traverse folders or view files The left hand side of the main page lists the sub-folders and files in the current directory. The user can traverse or view files, by clicking on the name of the folder/file. . Logout User can logout by clicking on the Logout link. Page 253

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

Page 252

Tuesday, November 21st, 2006

Page 252

Hint: This post is supported by Gama besplatan domen provider

Display the details of the uploaded file: printf(”Uploaded

Tuesday, November 21st, 2006

Display the details of the uploaded file: printf(”Uploaded File Details

“); printf(”Name: %s
“, $HTTP_POST_FILES[”userfile”][”name”]); printf(”Temporary Name: %s
“, $HTTP_POST_FILES[”userfile”][”tmp_name”]); printf(”Size: %s
“, $HTTP_POST_FILES[”userfile”][”size”]); printf(”Type: %s

“, $HTTP_POST_FILES[”userfile”][”type”]); Copy the uploaded file to the C:temp directory: if (copy($HTTP_POST_FILES[”userfile”][”tmp_name”], “c:/temp/”.$HTTP_POST_FILES[”userfile”][”name”])) { printf(”File successfully copied“); } else { printf(”Error: failed to copy file“); } ?> Sometimes we may want to set a limit on the size of file that can be uploaded to the system. This can be done by checking the size of the file in the PHP script. For example, to accept files only with a size of one megabyte or less, we could modify upload.php as follows: if ($HTTP_POST_FILES[”userfile”][”size”] > 1024*1024) { printf(” Error: File size is greater than one megabyte“); exit; } if (copy($HTTP_POST_FILES[”userfile”][”tmp_name”], “c:/temp/”.$HTTP_POST_FILES[”userfile”][”name”])) { printf(”File successfully copied“); } else { printf(”Error: failed to copy file“); } The PHP directive upload_max_filesize (default value 2Mb) can be used to specify the max size of the uploaded file that will be handled by PHP. Any upload files greater than this size will not be uploaded by PHP. PHP provides utility functions for handling files uploaded via the HTTP POST method: . is_uploaded_file() returns true, if the file (filename) was uploaded via the HTTP POST method. . move_uploaded_file() copies the uploaded file filename to destination. If the file is not a valid uploaded file, then the function doesn’t do anything. The function returns true on success. Page 251

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


firstname
lastname
email address
username
password
confirm password