Saturday, November 8, 2008

MySQL Tutorial

Please go through this MySQL Tutorial:
http://www.tizag.com/mysqlTutorial/

Bear in mind that we are using phpMyAdmin to set up and administer our databases.

MVC Architecture

Today we talked about the MVC Architecture, which is a design pattern that nicely separates database operations, logic, and presentation into separate components.

In our blog example, we saw how the blog Model code had separate functions for each CRUD operation. By keeping all direct interaction with the MySQL database in the blog Model, we can separate it from the business logic, contained in the Controller, that determines what to do for each page. This makes our code cleaner, easier to read, and easier to maintain if. If, for example, we decide to change the structure of our database tables, we only have to modify the code in the Model.

Once the Controller has determined what to do on any page, it fetches the appropriate data from the Model, and sends that data to the View, which has all the XHTML code necessary to display the page properly.

One of the nice things about common PHP Frameworks is that they offer clear MVC architecture. For example, I've been using Symfony recently, which forces my code to adhere to strict MVC principles.

Sunday, November 2, 2008

No Class November 29th

There will be no class on November 29th due to the Thanksgiving holiday break. The syllabus is incorrect.

Cookies - Reading, Writing, and Deleting

Here's a decent page about reading, writing, and deleting cookies in PHP:
http://www.w3schools.com/PHP/php_cookies.asp

Saturday, November 1, 2008

File Permissions

Most servers have settings that prevent just anyone from uploading or modifying files on the server. If everyone was allowed to edit every file, the server would have a serious security issue.

However, when you are making a website that allows people to upload files to your server, or to append data to an existing file, you need to make sure that the server knows to allow anyone to do that. Usually, when allowing file uploads, you set aside a particular sub-directory that anyone can modify.

For example, in the upload example we went over in class where users can upload images to the server, we allowed them to put the images into a directory called "files". This directory needs to have permissions settings that allow anyone to write to it. In another example we went over in class, when users save their Contact info into a text file, we need to make sure that text file has the proper permissions settings to allow that.

The easiest way to do this is to use WinSCP or some other file transfer program to set the permissions. In WinSCP, you can right-click on any file or folder on the server, and click "Properties". This pops up a dialog that allows you to modify the permissions settings for that file or folder.

Make sure that the folder where users can upload files on your server has Write permissions for both the Owner of the folder and Others.

Class 7 - In-Class Assignment - Wireframes







Class 7 In-Class Assignment

Your job is to make an image-uploading service. Users of the service can upload images that then show up on the site home page. The home page should show the latest 50 images that have been uploaded.

Users have to log in to see your site. If they are not logged in, they are redirected to the login page.
-this means you need a sign-up page that allows users to register with username/password

The home page displays a list of the 50 latest images that were uploaded
-all images should show the image, a one-line caption, and the username of the user who uploaded it

Here is a step by step breakdown of the tasks that you have to accomplish in order to get this to work:

LOGIN PAGE
-if the user is already logged in, they are redirected to the home page
-otherwise...
-show one text input for username
-show one password input for password
-and a submit button
-this page submits data to a script that checks the login info

SCRIPT TO CHECK THE LOGIN INFO
-receive the data the user entered from the Login Page
-open the users.txt file and see if there are any rows in that file that match the username/password the user entered
-if there is a match, you set a cookie called "loggedin" and set it equal to "true", and then redirect them to the home page
-otherwise, if the login failed, you redirect them to the login page with a user-friendly error message

HOME PAGE
-checks to see if the "loggedin" cookie is equal to "true"
-if it is, you let them see the page
-if it's not, you redirect them to the login page, and show a user-friendly error message
-if they are logged in, you open the "images.txt" file, and load all the data in there
-loop through each line of the text file, and get the image filename, caption, and username associated with that image
-display each image with the caption underneath it along with the username of the user who uploaded that image

UPLOAD PAGE
-users must be logged in to see this page, otherwise they are redirected to the login page with a user-friendly error message
-has a file input for the file the user wants to upload
-has a text input for the caption
-you have to figure out how to get and store the username of the user who is uploading this file
-submits the data to a script called process_upload_image.php that does the actual uploading and storing of the data

PROCESS_UPLOAD_IMAGE.PHP SCRIPT
-checks to make sure the "loggedin" cookie is equal to "true"
-if the user is not logged in, they are redirected to the login page with a user-friendly error
-if they are logged in....
-this script receives the caption and file that the user uploaded
-this script moves that image file to a permanent directory
-you need to make sure you have a variable that stores the path of the where the file was moved to
-this script needs to store the caption, image file path, and username to a text file called "images.txt"
-each new uploaded image will create a new line in the text file
-if there is an error uploading, this page shows a nice user-friendly error message
-if the upload succeeds, it redirects them to the home page with a user-friendly error indicating that their upload succeeded