Introduction
This week I will blog more in depth about PHP. This lecture was about functions, form handling, cookies and sessions mostly.
Functions
We already saw functions in Javascript. Functions are use to seperate a section of code that can be used frequently which can be applied to different data. Functions can also be created by the user. This can be placed anywhere as long as it contains the php starting and ending tag and can also be called internally.
Cookies
Cookies also known as web cookie, http cookie and browser cookie, is a piece of text stored on users computer as a test file by their web browser.Cookies keep track of information about your activity on the site.
This can be used for several things like authentication, storing site preferences. This stored information can be opened by a simple editor and sensitive data and passwords should be encrypted. These are used to identify a visitor, for example when a visitor logs in they can be greeted by their name.
When using IE as a browser the stored cookies are kept in a seperate file in a folder named "Cookies" which is usually found in the "Documents and Settings" folder. Users using Mozilla can find their cookies in a text file named cookies in a folder which is ofter called "Firefox/Profiles". Reasons why website use cookies are: Customization, Distribution, Privacy and Security.
Cookies are done using the setCookie() function, if this is correct it will return true, on the otherhand if it comes to a fail it will return a false. They need several parameters only the name is required the others are optional. Example: setCookie(name, value,expire);. To delete a cookie instead of doing the "+" you use the "-".
This can be used for several things like authentication, storing site preferences. This stored information can be opened by a simple editor and sensitive data and passwords should be encrypted. These are used to identify a visitor, for example when a visitor logs in they can be greeted by their name.
When using IE as a browser the stored cookies are kept in a seperate file in a folder named "Cookies" which is usually found in the "Documents and Settings" folder. Users using Mozilla can find their cookies in a text file named cookies in a folder which is ofter called "Firefox/Profiles". Reasons why website use cookies are: Customization, Distribution, Privacy and Security.
Cookies are done using the setCookie() function, if this is correct it will return true, on the otherhand if it comes to a fail it will return a false. They need several parameters only the name is required the others are optional. Example: setCookie(name, value,expire);. To delete a cookie instead of doing the "+" you use the "-".
Sessions
These are another way how one can hold persistence data between pages. A file is created when PHP starts a session. This file contains variables and values and this information stored is available throughout the entire visit of the Web site.
Task
This weeks task was to created a login page where the username enters its username and password and logs in. Then a remember me button is to be made using cookies and then replacing is with sessions.
These are another way how one can hold persistence data between pages. A file is created when PHP starts a session. This file contains variables and values and this information stored is available throughout the entire visit of the Web site.
Task
This weeks task was to created a login page where the username enters its username and password and logs in. Then a remember me button is to be made using cookies and then replacing is with sessions.
On the left on can find the code for the form. The form's method is "post". The method "post" involves anything like storing or updating data, or ordering a product, or sending E-mail. "Post" method is more secure then get since in "get" method data is added to the URL.The action attribute shows to where the form-data will be sent to when the form is submitted. A check box was used for the remember me button.
PHP
Below are some screen shots of good combinations and a bad combination
![]() |
| Invalid Login |
This was done by displaying an alert in the echo if the usernames and passwords weren't found in the array
| Good Log in |
While if the password and username entered are correct the user is logged in and is greeted with his username and can also choose to log out again.
Remember me check box
This check box is an option which the user can opt to tick and his details will be saved for the specified amount of days. In our case these details will be saved for 60 days. The setCookie() function is used to create a cookie. Three cookies where created one for the username another for the password and also for the data when visited. These cookies expire in 60 days. For the password MD5 was used to ensure more safety, with MD5 the password isn't saved as plain text but it will get encrypted. The last visited date will show the user on the screen when he logs in, the last time he was on his account. This can be of a good use, since if the date shows a date where he didn't log out he will be able to know someone else is entering his account.
I had a problem figuring out how to check if these are being stored or not and then found out about the cookie options from the internet options.
| Cookies being saved |
After using cookies, than we had to switch cookies with sessions. A session starts using session_start(); First a session was done to store variables, for password and username, this was done as shown below. First the variables are stored in the sessions and then displayed using the echo.
To then destroy a session one can use from two options either the unset ($_SESSION["username"] or else the session_destroy() function. This was used for the user to be able to log out of his account.
Conclusion
PHP is a rather interesting language. One can do many things that the client might want and in a not so complex way. There are tones of help on internet which if you get stuck or anything one can find a solution easily. I don't thing that php is such a hard language if you put some mind to it one can learn it easily.

No comments:
Post a Comment