crimsonjade Posted May 26, 2007 Share Posted May 26, 2007 I have this project and so far this is what it's suppose to do: There are 4 pages. The first page asks the user to enter their username and password (these are finite and defined by the second page) and asks the user to choose a file they want to work with. The second page verifies the username and password are correct and echoes the information from the chosen file into a table and has two links underneath: one to take the user back to page 1, and the second to take the user to page 3. Page 3 calls in two functions from page 4 to loop through all the information in that same file using regex to make sure Student Numbers and email addresses are in the correct format. If it's correct, it will alert the user that all the information is legitimate, or else it will write the errors to a log file. What I'm having trouble with is on page 3 and 4. The functions aren't working because Apache says there are undefined variables, and on page 3 I can't seem to get rid of the array to string conversion notice. This is driving me nuts. Here's the code for all four pages: Page 1 <?php /* File: page1.php This file is first major assignment in the PHP level 1 course. The purpose: "To write a php application that helps an instructor set up a website for their courses." Author: name Last modified: May 04, 2007. */ echo '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">'; echo '<html xmlns="http://www.w3.org/1999/xhtml">'; echo '<head>'; echo '<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />'; echo '<title>Comp 1920 - Assignment 1</title>'; echo '</head>'; echo '<body>'; echo '<form action = "./page2.php" method = "get" name = "as1_form">'; echo 'Please enter your username: <input type = "text" name="username"><br /><br />'; echo 'Please enter your password: <input type = "password" name="password"><br /><br />'; $directory = "./courses/"; $fileslist = scandir($directory); echo "Please Select a File:<br />"; echo '<select name="filename">'; foreach($fileslist as $filenames) { //The following code splits the string into 'filename' and 'extension'. list($name, $extension) = explode('.txt', $filenames); if (is_dir ($directory)){ //I'll think of something to put here later... } else { die ("Directory 'courses' or required files do not exist."); } if (is_dir($filenames)) { continue; } elseif (!ereg("^.*\.(txt)$", $filenames) ){ continue; } echo "<option value=\"$filenames\">$name</option><br />"; } echo "</select><br /><br />"; echo '<input type="submit" name="info_btn" value="Submit Query">'; echo '</form>'; echo '</body>'; echo '</html>'; ?> Page 2 <?php /* File: page2.php This file is first major assignment in the PHP level 1 course. The purpose: "To write a php application that helps an instructor set up a website for their courses." Author: name Last modified: May 04, 2007. */ /* The following code determines if the username field has a value. If it doesn't, it will kill the script. */ if (!($_GET["username"])){ die ("Please enter your username."); } elseif ($_GET["username"] !="comp1920"){ die ("Invalid username, sorry."); } /* The following code determines if the password name field has a value/valid value. If it doesn't, it will kill the script. */ if (!($_GET["password"])) { die ("Please enter a password."); } elseif ($_GET["password"] != "php") { die ("Invalid password, sorry."); } /* The following script determines which file was chosen and will echo out the contents in a table for the user to review. It provides links back to page1 or onto page3, depending if they decide to use the file. */ $resultfile = $_GET["filename"]; if (!($_GET["filename"])) { die ("Please select a course file to work with."); } else { $file = file("./courses/".$resultfile); echo "You chose file: $resultfile\n<table border=\"1\">\n"; foreach ($file as $newPiece) { echo "\t<tr>\n"; $newStr = trim($newPiece); $tempArr = split(",",$newStr); foreach ($tempArr as $tempStr) { if (ereg("^[a-zA-Z0-9]*$",$tempStr)){ $finaloutput=ucfirst($tempStr); } elseif (ereg("^[a-zA-Z0-9_.*]*@*[a-z]*\.*[a-z]{2,}",$tempStr)){ $finaloutput=strtolower($tempStr); } echo "\t\t<td>".$finaloutput."</td>\n"; } echo "\t</tr>\n"; } echo "</table>\n"; echo '<br /><br />'; echo '<a href="./page1.php">Choose another course file</a> <a href="./page3.php?filename=',urlencode($resultfile),'">Work with this course file</a>'; } echo '</body>'; echo '</html>'; ?> Page 3 <?php /* File: page3.php This file is first major assignment in the PHP level 1 course. The purpose: "To write a php application that helps an instructor set up a website for their courses." Author: Irene Thanh Last modified: May 04, 2007. */ date_default_timezone_set("America/Vancouver"); $fileN = $_GET["filename"]; $resultingf = "./courses/".$fileN; $date_time = date("F j, Y (h:i:s)"); if (!($fileN)){ die ("Please specify a file."); } else { $fileorg = file($resultingf); $ContentsArr = array(); if (is_file ($resultingf)){ foreach ($fileorg as $infos) { $ContentsArr[] = $infos; } echo "<table border=\"1\">\n"; echo "\t<tr>\n"; $newArrs = split(",",$ContentsArr); //$newL = array(); foreach($newArrs as $separated) { require_once("page4.php"); isStudentNumberWellFormed($separated); isEmailAddressWellFormed($separated); if (!(ereg("^[a-zA-Z]*$",$separated))){ //This is to pass the names. } else { $newResult = $separated; } echo "\t\t<td>".$newResult."</td\n"; } echo "\t</tr>\n"; } else { die ("No such file, sorry."); } } ?> Page 4 <?php /* File: page4.php This file is first major assignment in the PHP level 1 course. The purpose: "To write a php application that helps an instructor set up a website for their courses." Author: name Last modified: May 04, 2007. */ date_default_timezone_set("America/Vancouver"); $fileN = $_GET["filename"]; $resultingf = "./courses/".$fileN; $date_time = date("F j, Y (h:i:s)"); function isStudentNumberWellFormed($studentnumber){ if (!(ereg("^[aA][0-9]{8}$",$studentnumber))){ if ($filehandle = @fopen("./courses/errors.log", "a+")){ $writetext = fwrite($filehandle, "<br />-----"); $writetext = fwrite($filehandle, "<br />$date_time."); $writetext = fwrite($filehandle, "<br />Improper student number from $fileN:"); $writetext = fwrite($filehandle, "<br />$studentnumber"); fclose($filehandle) or die ("Error closing file <br />"); } else { die("Cannot open file 'errors.log', an error has occured."); } } else { $newResult = $separated; } } function isEmailAddressWellFormed($emailaddress){ if (!(ereg("^[a-zA-Z0-9_.]*@[a-z]*\.[a-z]{2,5}$",$emailaddress))){ if ($filehandle = @fopen("./courses/errors.log", "a+")){ $writetext = fwrite($filehandle, "<br />-----"); $writetext = fwrite($filehandle, "<br />$date_time."); $writetext = fwrite($filehandle, "<br />Improper email address from $fileN:"); $writetext = fwrite($filehandle, "<br />$emailaddress"); fclose($filehandle) or die ("Error closing file <br />"); } else { die("Cannot open file 'errors.log', an error has occured."); } } else { $newResult = $separated; } } ?> The script runs fine up until page 2 where you select the link to page 3. This is what happens when I try to access page 3 (that loops through the information using regex functions to check if certain information is in the proper format, for further info, read first paragraph at top) Notice: Array to string conversion in C:\...\page3.php on line 29 Notice: Undefined variable: date_time in C:\...\page4.php on line 20 Notice: Undefined variable: fileN in C:\...\page4.php on line 21 Notice: Undefined variable: date_time in C:\...\page4.php on line 40 Notice: Undefined variable: fileN in C:\...\page4.php on line 41 <table>Array</table> Someone please help! I really can't figure out how to get page 3 to so what it's supposed to do! Link to the project description: http://php1920.redirectme.net/res_COMP1920/Assignment1.htm Quote Link to comment https://forums.phpfreaks.com/topic/53021-using-functions/ Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.