Php Exam - help me please :)

Ye' old general discussion board. Basically, for everything that isn't covered elsewhere. Come here to shoot the breeze, shoot your mouth off, or whatever suits your fancy.
This forum is not for asking programming related questions.

Moderator: General Moderators

Post Reply
joshrichardson1
Forum Newbie
Posts: 3
Joined: Tue Mar 22, 2011 6:55 pm

Php Exam - help me please :)

Post by joshrichardson1 »

Hi guys,

Ok so in a few days i have got 2 seen exams in php. Basically i've been working all year and have had no time whatsoever to prepare!! So please if you can help me :D I don't want to cheat, but i really just want someone to point me in the right direction - bear in mind i've forgotten most of the php i previously learnt.

For the first exam i've just gotta make a simple login to direct the correct user to some links, and most of the files are already created. I'll post as much info as i can below. Thanks in advance!!!

link-list server.
A Web application to is required to accept user name and password input to an HTML form and authenticate the user against a database. If the user fails authentication, return her to the login page. If the user is authenticated present the user with a list of HTML links to which she is permitted access. The application consists of the following files

setup.php
A script to create the data files needed by the application.
provided

userlinks.sqlite
SQLite database of user data
created by setup.php

listlinks.array
Serialised array of links
created by setup.php

login.html
The login screen
provided

userlinks.php
PHP script to authenticate user and serve lists of permitted links.
for you to write

Setup.php
<?php
$dbfile='userlinks.sqlite';
$lnfile='listlinks.array';
$dbh = sqlite_open($dbfile);
$sql = "DROP TABLE user;";
@sqlite_exec($dbh,$sql);
$sql = "DROP TABLE users;";
@sqlite_exec($dbh,$sql);
$sql = "CREATE TABLE users (user TEXT PRIMARY KEY,fullname TEXT,".
"pass TEXT,L0 INT,L1 INT,L2 INT,L3 INT,L4 INT,L5 INT,". "L6 INT,L7 INT,L8 INT,L9 INT);";
sqlite_exec($dbh,$sql);
$sql = "INSERT INTO users VALUES('steve',". "'Steve Smethurst','letmein',1,1,1,1,1,1,1,1,1,1);";
sqlite_exec($dbh,$sql);
$sql = "INSERT INTO users VALUES('bob',". "'Bob Glass','anchorman',1,0,0,1,1,0,1,0,1,1);";
sqlite_exec($dbh,$sql);
$sql = "INSERT INTO users VALUES('frances',". "'Frances Johnson','doesitright',1,0,1,0,1,1,1,0,1,1);";
sqlite_exec($dbh,$sql);
$sql = "INSERT INTO users VALUES('richard',". "'Richard Eskins','ontheball',0,0,0,1,1,1,1,0,0,0);";
sqlite_exec($dbh,$sql);
$sql = "INSERT INTO users VALUES('chris',". "'Chris ','neverfails',1,1,0,1,0,1,1,0,1,0);";
sqlite_exec($dbh,$sql);
$sql = "INSERT INTO users VALUES('deirdre',". "'Deirdre Hynes','makesitwork',0,1,0,1,0,1,0,1,1,1);";
sqlite_exec($dbh,$sql);
$sql = "INSERT INTO users VALUES('jonanthan',". ".'Jonathan Wilson','heresjonny',1,1,0,1,1,1,0,0,0,1);";
sqlite_exec($dbh,$sql);
$links = array(
array("http://linuxoutlaws.com/","Linux Outlaws"),
array("http://www.linuxactionshow.com/index.php", "Linux Action Show"),
array("http://www.jodcast.net/","Jodrell Bank Astronomy Podcast"),
array("http://365daysofastronomy.org/","365 Days of Astronomy"),
array("http://php.net/manual/en/index.php","PHP Language Manual"),
array("http://www.sqlite.org/lang.html","SQLite Syntax Manual"),
array("http://www.w3schools.com/js/default.asp", "W3C Schools JavaScript Tutorial"),
array("http://www.scottsigler.com/taxonomy/reverse/40", "Scott Sigler - The Future Dark Overlord"),
array("http://www.thephilrossiexperience.com/crescent/", "Phil Rossi - Darker Than Deep Space"),
array("http://escapepod.org/", "Escape Pod - The Science Fiction Podcast Magazine")
);
file_put_contents($lnfile,serialize($links));
$sql = "SELECT pass FROM users WHERE user='steve';";
$rh = sqlite_query($dbh,$sql,SQLITE_ASSOC);
$row = sqlite_fetch_array($rh);
if ($row['pass'] == 'letmein')
echo "Database has been created and tested<br />";
else
echo "Database does not work<br />";
$filestring = file_get_contents($lnfile);
$array = unserialize($filestring);
if ($array[0][1] == "Linux Outlaws")
echo "Links file has been created and tested<br />";
else
echo "Links file does not work<br />";
sqlite_close($dbh);


userlinks.php – skeleton file
<?php
# read from file and unserialise the links array
# get user & pass from http header
# open the database
# get database row for this user
# authenticate user or return to login
# print HTML page header
# print full name as heading
# print each permitted link in a numbered list
# close the database

Login.html
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<title>Login Links</title>
<meta http-equiv="content-type" content="text/html; charset=UTF-8"/>
<meta http-equiv="expires" content="0"/>
</head>
<body>
<h3>User Login For Access to the Links Server</h3>
<form action="userlinks.php" method="POST">
<label for="user" style="display:block;">User Name</label>
<input id="user" name="user" type="text" />
<label for="pass" style="display: block;">Password</label>
<input id="pass" name="pass" type="password" />
<input type="submit" name="login" value="Login" />
</form>
</body>
</html>

There's quite allot more that i have missed out but i think this should give you the jist so that you can point out what i have to do and where i can start looking for the answers. Sorry for being a complete noob! I literally really want ot be a web developer but until i finish work (this summer) and go back to uni i havn't had any time to carry on learning.

If you need any more of the info just pm me with your e-mail and i will send over the pdf's - there are 2 though!

Cheers guys, Josh. :)
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: Php Exam - help me please :)

Post by Jonah Bron »

Sounds like you need to run setup.php first, to initialize the database and listlinks.array. The comments in userlinks.php describe pretty plainly what needs to be done. I think the best thing to do is to start following those comments, and tell us more specifically the parts you're stuck on.
joshrichardson1
Forum Newbie
Posts: 3
Joined: Tue Mar 22, 2011 6:55 pm

Re: Php Exam - help me please :)

Post by joshrichardson1 »

Sorry Jonah, i was really tired when i first looked at this. It's finally starting to come together!

On the second paper i get given this error:

error_reporting(E_ERROR | E_WARNING | E_PARSE);

But it's not explained very well on everything i found on google, any chance you could help me out with fixing it??

I'll be in touch if i struggle any more, thanks!
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: Php Exam - help me please :)

Post by Jonah Bron »

That's not an error, that's PHP code. That will make it so that you see an error if there's a problem with your code. Put that at the top of userlinks.php (below <?php of course).
joshrichardson1
Forum Newbie
Posts: 3
Joined: Tue Mar 22, 2011 6:55 pm

Re: Php Exam - help me please :)

Post by joshrichardson1 »

Got it all sorted guys :) Thanks.
Post Reply