Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- include("get_section_problem.php");
- date_default_timezone_set('America/Los_Angeles');
- $METHOD = $_SERVER['REQUEST_METHOD'];
- if ($METHOD == "GET") {
- output_form();
- } else if ($METHOD == "POST") {
- assert_params();
- submit();
- }
- function output_form() {
- $UWNETID = $_SERVER["PHP_AUTH_USER"];
- global $SECTION;
- global $SECTION_NUMBER;
- $SECTION = $SECTION;
- $SECTION_NUMBER = $SECTION_NUMBER;
- ?>
- <!DOCTYPE html><html><head><title>CSE 154 -- <?=$SECTION?> section<?=$SECTION_NUMBER?> ONLINE HW TURNIN</title></head><body>
- <h1>Welcome, <?=$UWNETID?>!</h1>
- <p>This is the section <?=$SECTION?> online homework #<?=$SECTION_NUMBER?> turnin.</p>
- <div style="border:3px dashed black;margin:15px;">
- <?php
- global $SECTION_PROBLEM_FILE;
- print get_section_problem($SECTION_NUMBER);
- ?>
- </div>
- <form method="POST">
- <textarea style="width:100%;height:200px;" name="content">Enter section problem solution here!</textarea>
- <button id="submit">Submit</button>
- <input type="hidden" value="<?=$UWNETID?>" name="uwnetid" />
- <input type="hidden" name="section" value="<?=$SECTION?>" />
- <input type="hidden" name="section_number" value="<?=$SECTION_NUMBER?>" />
- </form>
- </body></html>
- <?php
- }
- function assert_params() {
- if (!isset($_POST["section"]) ||
- !isset($_POST["section_number"]) ||
- !isset($_POST["uwnetid"]) ||
- !isset($_POST["content"])) {
- die("missing params");
- }
- if (strlen($_POST["section"]) != 2) {
- die("section is two letters (ex: AA, AB, BC, etc...");
- }
- if (!is_numeric($_POST["section_number"])) {
- die("section_number should be numeric");
- }
- }
- function submit() {
- $SERVER = "localhost";
- $USER = "zcava";
- $PASSWORD = "k8UrHxcnDWwr4";
- $DBNAME = "sectCheckin";
- define("DBNAME", $DBNAME, true);
- try {
- $DB = new PDO("mysql:dbname=$DBNAME;host=$SERVER", $USER, $PASSWORD);
- } catch (PDOException $err) {
- die("Connection to database failed, alert a TA: ".$err->getMessage());
- }
- // check authentication
- $auth = isset($_SERVER["AUTH_TYPE"]) && $_SERVER["AUTH_TYPE"] == "UWNetID" && isset($_SERVER["PHP_AUTH_USER"]);
- $query = $DB->prepare('INSERT INTO '.DBNAME.'.section (section_number,section,uwnetid,content) VALUES (:section_number,:section,:uwnetid,:content)');
- $query->bindValue(":section" ,$_POST["section"]);
- $query->bindValue(":uwnetid" ,$_POST["uwnetid"]);
- $query->bindValue(":content" ,$_POST["content"]);
- $query->bindValue(":section_number" ,$_POST["section_number"]);
- if(!$query->execute()) {
- print "<h1>FAILURE</h1>";
- } else {
- print "<h1>SUCCESS</h1><p>Your section homework was successfully submitted, ".$_SERVER["PHP_AUTH_USER"]."</p>";
- }
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement