Presentation is loading. Please wait.

Presentation is loading. Please wait.

Website Development Registering Users – Introducing Cookies.

Similar presentations


Presentation on theme: "Website Development Registering Users – Introducing Cookies."— Presentation transcript:

1 Website Development Registering Users – Introducing Cookies

2 What you should be able to create at the end of this course A fully working e-commerce site Server side scripting in PHP Saving data on a server using files and MySql

3 What you will achieve this week! Saving data on the client in cookies How to register and log in users (in a simple way)

4 : Customer browser request service access page interpret set data present html return html get data databasescripting language web server Reminder of the general process

5 Registering users data/users : customer registerUser.htmsaveUser.php supply details save *write

6 Please register User Name Password

7 <?php $File = fopen("data/users","a"); fwrite($File,sprintf("%s,%s\n", $UserName, $Password)); fclose($File); ?> saveUser.php

8 What are cookies? A "cookie" is a small piece of information sent by a web server to store on a web browser so it can later be read back from that browser. This is useful for having the browser remember some specific information. What are they used for ? An example is when a browser stores your passwords and user ID's. They are also used to store preferences of start pages, both Microsoft and Netscape use cookies to create personal start pages. http://www.cookiecentral.com/

9 int setcookie (string name [, string value [, int expire ]]) Setcookie() defines a cookie to be sent along with the rest of the header information. Cookies must be sent before any other headers are sent (this is a restriction of cookies, not PHP). This requires you to place calls to this function before any or tags. All the arguments except the name argument are optional. If only the name argument is present, the cookie by that name will be deleted from the remote client. The expire argument is an integer indicating the number of seconds before the cookie expires.

10 Logging in and validating user name and password

11 Please provide your account information: User Name Password

12 include() The include() statement includes and evaluates the specified file. An important note about how this works is that when a file is include()ed, parsing drops out of PHP mode and into HTML mode at the beginning of the target file, and resumes again at the end. For this reason, any code inside the target file which should be executed as PHP code must be enclosed within valid PHP start and end tags.

13 <?php $found = 0; if (file_exists("data/users")) { $userFile = fopen("data/users","r"); while ($input = fgetcsv($userFile,1000)) { list($u, $p) = $input; if (($u == $UserName) && ($p == $Password)) $found = 1; }; fclose($userFile); }; if ($found) { include('welcome.php'); } else { include('NotRegistered.htm'); }; ?> login.php this will be available as a variable in any future page for this web site

14 Welcome.php

15 Sorry, you are not registered as a user, or you have typed your username and password wrongly. Please go to our registration page if you need to register, or to our login page if you think you mistyped. NotRegistered.htm

16 So, what have we got so far? A means of collecting data from the user A means of transmitting it to the server A means of recording it on the server A means of retrieving data from the server Register users, and allow log in Remember the user on the client using cookies


Download ppt "Website Development Registering Users – Introducing Cookies."

Similar presentations


Ads by Google