Web Programming Lab 7: Users

This weeks lab was all about users. We were instructed to create a way for users to apply to our page, and then to display a list of users on another page. The catch: we were not allowed to use databases for this. Instead, we needed to use text files. Well, for the assignment that isn’t horrible, but to get the extra credit we needed to make users editable. I took mine one step further, and made it so other users shouldn’t be able to edit another user’s information. Also, the lab dealt a lot with error handling on the page. For the error handling, I used regular expressions. An example is my email regular expression. It should handle most bob@bob.com emails, but will fail with emails like user@mail.plymouth.edu. It looks something like this:
if (!preg_match('/[a-zA-Z0-9\._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}/', $email))
{
$email_message='This is not a valid email address';
$email='';
}

My zip codes are checked more simply by making sure they are five digits and numeric. The more complex work stemmed from the editing of users. This actually completely re-writes the users file when a user is updated. The function looks like this:

function updateUser($id, $first_name, $last_name, $email, $zip, $password){
$users=getAllUsers();
$users[$id]['first_name']=$first_name;
$users[$id]['last_name']=$last_name;
$users[$id]['email']=$email;
$users[$id]['zip']=$zip;
$users[$id]['password']=md5($password);
$page = ("users.txt");
$fp = file_put_contents($page ,'');
foreach($users as $user){
$line=$user['id'].'~'.$user['first_name'].'~'.$user['last_name'].'~'.$user['email'].'~'.$user['zip'].'~'.$user['password'];
$fp = file_put_contents($page , $line, FILE_APPEND);
}
}

To create an account on my site go here, and to check out the users of my site look here.

Advertisements
  1. Had an issue signing up initially. It worked on the second try though.

    Your regexp is more flexible than you have stated. since “.” is included in this part “[a-zA-Z0-9.-]+” mail.plymouth.edu does work, or a.b.c.asdf.monkey.lame.corn This is a good thing.

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: