├── .gitattributes ├── 10-1.php ├── 10-2.php ├── 13-1.php ├── 13-2.php ├── 14-1.php ├── 14-10.php ├── 14-11.php ├── 14-12.php ├── 14-2.php ├── 14-4.php ├── 14-5.sql ├── 14-6.php ├── 14-8.sql ├── 14-9.sql ├── 15-1.php ├── 16-1.php ├── 16-2.php ├── 17-0.php ├── 17-02.php ├── 17-03.php ├── 17-1.php ├── 17-1.sql ├── 17-2.php ├── 18-1.php ├── 18-2.php ├── 18-3.php ├── 18-4.php ├── 18-5.php ├── 19-1.php ├── 19-2.php ├── 20-1.html ├── 20-2.php ├── 20-3.js ├── 27-1.php ├── 27-2.php ├── 28-1.php ├── 28-1.sql ├── 28-10.php ├── 28-11.php ├── 28-2.php ├── 28-3.php ├── 28-4.php ├── 28-5.php ├── 28-6.php ├── 28-7.php ├── 28-8.php ├── 28-9.php ├── 29-1-login.php ├── 29-2-viewbonus.php ├── 29-3.php ├── 30-1.php ├── 30-2.php ├── 31-1.php ├── 32-1.php ├── 32-2.php ├── 33-1.php ├── 33-2.php ├── 33-3.php ├── 34-1.php ├── 35-1.php ├── 35-1.sql ├── 4-1.php ├── 4-2.php ├── 5-1.php ├── 6-1.php ├── 7-1.php ├── 7-2.php ├── 7-3.php ├── 7-4.php ├── 7-5.php ├── 8-1.php ├── 8-2.php ├── 9-1.php ├── 9-2.php ├── 9-3.php ├── 9-4.php ├── 9-5.php ├── 9-6.php ├── 9-7.php ├── 9-8.php ├── 9-9.php ├── 9781430260431.jpg ├── Contributing.md ├── ConvertController.php ├── LICENSE.txt ├── README.md ├── aes.inc ├── authentication.txt ├── convertForm.blade.php ├── errata.md ├── log_service.php └── logger.php /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto 3 | -------------------------------------------------------------------------------- /10-1.php: -------------------------------------------------------------------------------- 1 | 34 | -------------------------------------------------------------------------------- /10-2.php: -------------------------------------------------------------------------------- 1 | isFile()) { 9 | $directorySize += $fileinfo->getSize(); 10 | } 11 | if ($fileinfo->isDir() && !$fileinfo->isDot()) { 12 | $directorySize += directorySize($directory.'/'.$fileinfo->getFilename()); 13 | } 14 | } 15 | 16 | return $directorySize; 17 | 18 | } 19 | 20 | $directory = '/home/frank'; 21 | $totalSize = round((directorySize($directory) / 1048576), 2); 22 | printf("Directory %s: %f MB", $directory, $totalSize); 23 | ?> 24 | -------------------------------------------------------------------------------- /13-1.php: -------------------------------------------------------------------------------- 1 | ", $name); 8 | printf("The address %s will soon be a spam-magnet!
", $email); 9 | } 10 | ?> 11 | 12 |
13 |

14 | Name:
15 | 16 |

17 |

18 | Email Address:
19 | 20 |

21 | 22 |
23 | -------------------------------------------------------------------------------- /13-2.php: -------------------------------------------------------------------------------- 1 | "; 5 | if (is_array($_POST['languages'])) { 6 | foreach($_POST['languages'] AS $language) { 7 | $language = htmlentities($language); 8 | echo "$language
"; 9 | } 10 | } 11 | } 12 | ?> 13 | 14 |
15 | What's your favorite programming language?
(check all that apply):
16 | C#
17 | JavaScript
18 | Perl
19 | PHP
20 | 21 |
22 | -------------------------------------------------------------------------------- /14-1.php: -------------------------------------------------------------------------------- 1 | "; 11 | echo "Your password: {$_SERVER['PHP_AUTH_PW']}
"; 12 | } 13 | ?> 14 | -------------------------------------------------------------------------------- /14-10.php: -------------------------------------------------------------------------------- 1 | prepare("UPDATE logins SET hash=? WHERE email=?"); 13 | $stmt->bind_param('ss', $id, $address); 14 | 15 | $stmt->execute(); 16 | 17 | $email = <<< email 18 | Dear user, 19 | Click on the following link to reset your password: 20 | http://www.example.com/users/lostpassword.php?id=$id 21 | email; 22 | 23 | // Email user password reset options 24 | mail($address,"Password recovery","$email","FROM:services@example.com"); 25 | echo "

Instructions regarding resetting your password have been sent to 26 | $address

"; 27 | ?> 28 | -------------------------------------------------------------------------------- /14-11.php: -------------------------------------------------------------------------------- 1 | prepare("UPDATE logins SET pswd=? WHERE hash=?"); 17 | $stmt->bind_param("ss", password_hash($pswd, PASSWORD_DEFAULT), $id); 18 | $stmt->execute(); 19 | 20 | // Display the new password 21 | echo "

Your password has been reset to {$pswd}.

"; 22 | ?> 23 | -------------------------------------------------------------------------------- /14-12.php: -------------------------------------------------------------------------------- 1 | FB_APP_ID, 6 | 'app_secret' => FB_APP_SECRET, 7 | 'default_graph_version' => 'v2.11', 8 | ]); 9 | 10 | $helper = $fb->getJavaScriptHelper(); 11 | 12 | try { 13 | $accessToken = $helper->getAccessToken(); 14 | $fb->setDefaultAccessToken((string) $accessToken); 15 | $response = $fb->get('/me?fields=id,name'); 16 | } catch(\Facebook\Exceptions\FacebookResponseException $e) { 17 | // When Graph returns an error 18 | Error('Graph returned an error: ' . $e->getMessage()); 19 | exit; 20 | } catch(\Facebook\Exceptions\FacebookSDKException $e) { 21 | // When validation fails or other local issues 22 | Error('Facebook SDK returned an error: ' . $e->getMessage()); 23 | exit; 24 | } 25 | 26 | $me = $response->getGraphUser(); 27 | // $me is an array with the id of the user and any additional fields requested. 28 | -------------------------------------------------------------------------------- /14-2.php: -------------------------------------------------------------------------------- 1 | 34 | -------------------------------------------------------------------------------- /14-5.sql: -------------------------------------------------------------------------------- 1 | CREATE TABLE logins ( 2 | id INTEGER UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY, 3 | username VARCHAR(255) NOT NULL, 4 | pswd CHAR(40) NOT NULL 5 | ); 6 | -------------------------------------------------------------------------------- /14-6.php: -------------------------------------------------------------------------------- 1 | prepare("SELECT username, pswd FROM logins 25 | WHERE username=? AND pswd= ?"); 26 | 27 | $stmt->bind_param('ss', $_SERVER['PHP_AUTH_USER'], password_hash($_SERVER['PHP_AUTH_PW'], PASSWORD_DEFAULT)); 28 | 29 | $stmt->execute(); 30 | 31 | $stmt->store_result(); 32 | 33 | // Remember to check for erres also! 34 | if ($stmt->num_rows == 0) 35 | authenticate_user(); 36 | } 37 | 38 | ?> 39 | -------------------------------------------------------------------------------- /14-8.sql: -------------------------------------------------------------------------------- 1 | CREATE TABLE subscribers ( 2 | id INTEGER UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY, 3 | email VARCHAR(255) NOT NULL, 4 | hash CHAR(40) NOT NULL, 5 | read CHAR(1) 6 | ); 7 | -------------------------------------------------------------------------------- /14-9.sql: -------------------------------------------------------------------------------- 1 | CREATE TABLE logins ( 2 | id TINYINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY, 3 | email VARCHAR(55) NOT NULL, 4 | username VARCHAR(16) NOT NULL, 5 | pswd CHAR(32) NOT NULL, 6 | hash CHAR(32) NOT NULL 7 | ); 8 | -------------------------------------------------------------------------------- /15-1.php: -------------------------------------------------------------------------------- 1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 | 9 |
10 | Class notes must be uploaded in PDF format.

"; 21 | } else { 22 | // Move uploaded file to final destination. 23 | $result = move_uploaded_file($_FILES['classnotes']['tmp_name'], 24 | FILEREPOSITORY . $_POST['lastname'] . '_' . $_FILES['classnotes']['name']); 25 | if ($result == 1) echo "

File successfully uploaded.

"; 26 | else echo "

There was a problem uploading the file.

"; 27 | } 28 | } 29 | } 30 | else { 31 | echo "

There was a problem with the upload. Error code {$_FILES['classnotes']['error']}

"; 32 | } 33 | ?> 34 | -------------------------------------------------------------------------------- /16-1.php: -------------------------------------------------------------------------------- 1 | Scan results for $target

"; 17 | 18 | // Execute the scan 19 | foreach ($range as $port) { 20 | $result = @fsockopen($target, $port,$errno,$errstr,1); 21 | if ($result) echo "

Socket open at port $port

"; 22 | } 23 | 24 | ?> 25 | -------------------------------------------------------------------------------- /16-2.php: -------------------------------------------------------------------------------- 1 |
2 |

3 | IP Address:
4 | . 5 | . 6 | . 7 | 8 |

9 | 10 |

11 | Subnet Mask:
12 | . 13 | . 14 | . 15 | 16 |

17 | 18 | 19 | 20 |
21 | 22 | "; 41 | echo ""; 50 | } 51 | ?> 52 | -------------------------------------------------------------------------------- /17-0.php: -------------------------------------------------------------------------------- 1 | 13 | -------------------------------------------------------------------------------- /17-02.php: -------------------------------------------------------------------------------- 1 | prepare("SELECT first_name FROM users WHERE username = ? and password = ?"); 15 | 16 | $stmt->bind_param('ss', $_POST['username'], $_POST['password']); 17 | 18 | $stmt->execute(); 19 | 20 | $stmt->store_result(); 21 | 22 | if ($stmt->num_rows == 1) 23 | { 24 | 25 | $stmt->bind_result($firstName); 26 | 27 | $stmt->fetch(); 28 | 29 | $_SESSION['first_name'] = $firstName; 30 | 31 | header("Location: http://www.example.com/"); 32 | 33 | } 34 | 35 | } else { 36 | require_once('login.html'); 37 | } 38 | 39 | } else { 40 | echo "You are already logged into the site."; 41 | } 42 | 43 | ?> 44 | -------------------------------------------------------------------------------- /17-03.php: -------------------------------------------------------------------------------- 1 | prepare("SELECT id, title, content FROM articles WHERE id = ?"); 11 | 12 | $stmt->bind_param('i', $_GET['id']); 13 | 14 | $stmt->execute(); 15 | 16 | $stmt->store_result(); 17 | 18 | if ($stmt->num_rows == 1) 19 | { 20 | $stmt->bind_result($id, $title, $content); 21 | #stmt->fetch(); 22 | } 23 | 24 | // Add article title and link to list 25 | $articleLink = "{$title}"; 26 | 27 | if (! in_array($articleLink, $_SESSION['articles'])) 28 | $_SESSION['articles'][] = $articleLink; 29 | 30 | // Display the article 31 | echo "

$title

$content

"; 32 | 33 | // Output list of requested articles 34 | 35 | echo "

Recently Viewed Articles

"; 36 | echo ""; 41 | ?> 42 | -------------------------------------------------------------------------------- /17-1.php: -------------------------------------------------------------------------------- 1 | 7 | -------------------------------------------------------------------------------- /17-1.sql: -------------------------------------------------------------------------------- 1 | CREATE TABLE users ( 2 | id INTEGER UNSIGNED NOT NULL AUTO_INCREMENT, 3 | first_name VARCHAR(255) NOT NULL, 4 | username VARCHAR(255) NOT NULL, 5 | password VARCHAR(32) NOT NULL, 6 | PRIMARY KEY(id) 7 | ); 8 | -------------------------------------------------------------------------------- /17-2.php: -------------------------------------------------------------------------------- 1 | _dbLink = new mysqli($host, $user, $pswd, $db); 14 | $this->_sessionName = $sessionName; 15 | $this->_sessionTable = $sessionTable; 16 | 17 | // Set the handlers for open, close, read, write, destroy and garbage collection. 18 | session_set_save_handler( 19 | array($this, "session_open"), 20 | array($this, "session_close"), 21 | array($this, "session_read"), 22 | array($this, "session_write"), 23 | array($this, "session_destroy"), 24 | array($this, "session_gc") 25 | ); 26 | 27 | session_start(); 28 | } 29 | 30 | function session_open($session_path, $session_name) { 31 | $this->_sessionName = $session_name; 32 | return true; 33 | } 34 | 35 | function session_close() { 36 | return 1; 37 | } 38 | 39 | function session_write($SID, $value) { 40 | $stmt = $this->_dbLink->prepare(" 41 | INSERT INTO {$this->_sessionTable} 42 | (sid, value) VALUES (?, ?) ON DUPLICATE KEY 43 | UPDATE value = ?, expiration = NULL"); 44 | 45 | $stmt->bind_param('sss', $SID, $value, $value); 46 | $stmt->execute(); 47 | 48 | session_write_close(); 49 | } 50 | 51 | function session_read($SID) { 52 | // create a SQL statement that selects the value for the cussent session ID and validates that it is not expired. 53 | $stmt = $this->_dbLink->prepare( 54 | "SELECT value FROM {$this->_sessionTable} 55 | WHERE sid = ? AND 56 | UNIX_TIMESTAMP(expiration) + " . 57 | self::SESS_EXPIRE . " > UNIX_TIMESTAMP(NOW())" 58 | ); 59 | 60 | $stmt->bind_param('s', $SID); 61 | 62 | if ($stmt->execute()) 63 | { 64 | $stmt->bind_result($value); 65 | $stmt->fetch(); 66 | 67 | if (! empty($value)) 68 | { 69 | return $value; 70 | } 71 | } 72 | } 73 | 74 | public function session_destroy($SID) { 75 | // Delete the record for the session id provided 76 | $stmt = $this->_dbLink->prepare("DELETE FROM {$this->_sessionTable} WHERE SID = ?"); 77 | $stmt->bind_param('s', $SID); 78 | $stmt->execute(); 79 | } 80 | 81 | public function session_gc($lifetime) { 82 | // Delete records that are expired. 83 | $stmt = $this->_dbLink->prepare("DELETE FROM {$this->_sessionTable} 84 | WHERE UNIX_TIMESTAMP(expiration) < " . UNIX_TIMESTAMP(NOW()) - self::SESS_EXPIRE); 85 | 86 | $stmt->execute(); 87 | } 88 | } 89 | -------------------------------------------------------------------------------- /18-1.php: -------------------------------------------------------------------------------- 1 | '']; 3 | $zip = "98109"; 4 | $base_url = "https://api.openweathermap.org/data/2.5"; 5 | $weather_url = "/weather?zip=" . $zip; 6 | $api_key = "&appid={$OpenWeather['api_key']}"; 7 | $api_url = $base_url . $weather_url . $api_key; 8 | 9 | $weather = json_decode(file_get_contents($api_url)); 10 | print_r($weather); 11 | -------------------------------------------------------------------------------- /18-2.php: -------------------------------------------------------------------------------- 1 | '']; 3 | $zip = "98109"; 4 | $base_url = "https://api.openweathermap.org/data/2.5"; 5 | $weather_url = "/forecast?zip=" . $zip; 6 | $api_key = "&appid={$OpenWeather['api_key']}"; 7 | $api_url = $base_url . $weather_url . $api_key; 8 | 9 | $weather = json_decode(file_get_contents($api_url)); 10 | print_r($weather); 11 | -------------------------------------------------------------------------------- /18-3.php: -------------------------------------------------------------------------------- 1 | 16 | -------------------------------------------------------------------------------- /19-2.php: -------------------------------------------------------------------------------- 1 | encrypt("This message is secure and must be encrypted"); 7 | echo "Encrypted: '$e'\n"; 8 | 9 | $d = $aes->decrypt($e); 10 | echo "Decrypted: '$d'\n"; 11 | -------------------------------------------------------------------------------- /20-1.html: -------------------------------------------------------------------------------- 1 |
2 |

3 | Provide Your E-mail Address
4 | 5 |

6 | 7 |

8 | Choose a Username
9 | 10 | Check Username 11 |

12 | 13 |

14 | Choose and Confirm Password
15 |
16 | 17 |

18 | 19 |

20 | 21 |

22 |
23 | -------------------------------------------------------------------------------- /20-2.php: -------------------------------------------------------------------------------- 1 | 28 | -------------------------------------------------------------------------------- /20-3.js: -------------------------------------------------------------------------------- 1 | 5 | 35 | -------------------------------------------------------------------------------- /27-1.php: -------------------------------------------------------------------------------- 1 | stmt_init(); 10 | 11 | // Prepare the statement for execution 12 | $stmt->prepare($query); 13 | 14 | // Bind the parameters 15 | $stmt->bind_param('ssd', $sku, $name, $price); 16 | 17 | // Assign the posted sku array 18 | $skuarray = $_POST['sku']; 19 | 20 | // Assign the posted name array 21 | $namearray = $_POST['name']; 22 | 23 | // Assign the posted price array 24 | $pricearray = $_POST['price']; 25 | 26 | // Initialize the counter 27 | $x = 0; 28 | 29 | // Cycle through the array, and iteratively execute the query 30 | while ($x < sizeof($skuarray)) { 31 | $sku = $skuarray[$x]; 32 | $name = $namearray[$x]; 33 | $price = $pricearray[$x]; 34 | $stmt->execute(); 35 | } 36 | 37 | // Recuperate the statement resources 38 | $stmt->close(); 39 | 40 | // Close the connection 41 | $mysqli->close(); 42 | 43 | ?> 44 | -------------------------------------------------------------------------------- /27-2.php: -------------------------------------------------------------------------------- 1 | stmt_init(); 11 | 12 | // Prepare the statement for execution 13 | $stmt->prepare($query); 14 | 15 | // Execute the statement 16 | $stmt->execute(); 17 | 18 | // Bind the result parameters 19 | $stmt->bind_result($sku, $name, $price); 20 | 21 | // Cycle through the results and output the data 22 | 23 | while($stmt->fetch()) 24 | printf("%s, %s, %s
", $sku, $name, $price); 25 | 26 | // Recuperate the statement resources 27 | $stmt->close(); 28 | 29 | // Close the connection 30 | $mysqli->close(); 31 | 32 | ?> 33 | -------------------------------------------------------------------------------- /28-1.php: -------------------------------------------------------------------------------- 1 | getMessage(); 6 | } 7 | ?> 8 | -------------------------------------------------------------------------------- /28-1.sql: -------------------------------------------------------------------------------- 1 | CREATE TABLE products ( 2 | id INT NOT NULL AUTO_INCREMENT, 3 | sku CHAR(8) NOT NULL, 4 | title VARCHAR(100) NOT NULL, 5 | PRIMARY KEY(id) 6 | ); 7 | -------------------------------------------------------------------------------- /28-10.php: -------------------------------------------------------------------------------- 1 | ", $row[1], $row[0]); 5 | } 6 | 7 | // Execute the query 8 | $stmt = $dbh->query('SELECT sku, title FROM products ORDER BY title'); 9 | 10 | // Retrieve all of the rows 11 | $rows = $stmt->fetchAll(); 12 | 13 | // Output the rows 14 | echo explode(array_map('formatRow', $rows)); 15 | 16 | ?> 17 | -------------------------------------------------------------------------------- /28-11.php: -------------------------------------------------------------------------------- 1 | prepare($query); 8 | $stmt->execute(); 9 | 10 | // Bind according to column offset 11 | $stmt->bindColumn(1, $sku); 12 | 13 | // Bind according to column title 14 | $stmt->bindColumn('title', $title); 15 | 16 | // Fetch the row 17 | $row = $stmt->fetch(PDO::FETCH_BOUND); 18 | 19 | // Output the data 20 | printf("Product: %s (%s)", $title, $sku); 21 | ?> 22 | -------------------------------------------------------------------------------- /28-2.php: -------------------------------------------------------------------------------- 1 | getMessage()); 6 | } 7 | 8 | $query = "INSERT INTO product(id, sku, title) 9 | VALUES(NULL, 'SS873221', 'Surly Soap') "; 10 | 11 | $dbh->exec($query); 12 | 13 | echo $dbh->errorCode(); 14 | ?> 15 | -------------------------------------------------------------------------------- /28-3.php: -------------------------------------------------------------------------------- 1 | getMessage()); 6 | } 7 | 8 | $query = "INSERT INTO product(id, sku, title) 9 | VALUES(NULL, 'SS873221', 'Surly Soap') "; 10 | 11 | $dbh->exec($query); 12 | 13 | print_r($dbh->errorInfo()); 14 | 15 | ?> 16 | -------------------------------------------------------------------------------- /28-4.php: -------------------------------------------------------------------------------- 1 | getAttribute(PDO::ATTR_CONNECTION_STATUS); 5 | 6 | ?> 7 | -------------------------------------------------------------------------------- /28-5.php: -------------------------------------------------------------------------------- 1 | exec($query); 6 | echo "Total rows affected: $affected"; 7 | 8 | ?> 9 | -------------------------------------------------------------------------------- /28-6.php: -------------------------------------------------------------------------------- 1 | query($query) AS $row) { 7 | printf("Product: %s (%s)
", $row[‘title’], $row[‘sku’]); 8 | } 9 | 10 | ?> 11 | -------------------------------------------------------------------------------- /28-7.php: -------------------------------------------------------------------------------- 1 | prepare($query); 8 | 9 | // Execute the query 10 | $stmt->execute( [':sku' => 'MN873213', ':title' => 'Minty Mouthwash'] ); 11 | 12 | // Execute again 13 | $stmt->execute( [':sku' => 'AB223234', ':title' => 'Lovable Lipstick'] ); 14 | ?> 15 | -------------------------------------------------------------------------------- /28-8.php: -------------------------------------------------------------------------------- 1 | prepare($query); 9 | 10 | $sku = 'MN873213'; 11 | $title = 'Minty Mouthwash'; 12 | 13 | // Bind the parameters 14 | $stmt->bindParam(':sku', $sku); 15 | $stmt->bindParam(':title', $title); 16 | 17 | // Execute the query 18 | $stmt->execute(); 19 | 20 | $sku = 'AB223234'; 21 | $title = 'Lovable Lipstick'; 22 | 23 | // Bind the parameters 24 | $stmt->bindParam(':sku', $sku); 25 | $stmt->bindParam(':title', $title); 26 | 27 | // Execute again 28 | $stmt->execute(); 29 | ?> 30 | -------------------------------------------------------------------------------- /28-9.php: -------------------------------------------------------------------------------- 1 | query('SELECT sku, title FROM products ORDER BY title'); 8 | 9 | while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) { 10 | printf("Product: %s (%s)
", $row[‘title’], $row[‘sku’]); 11 | } 12 | 13 | ?> 14 | -------------------------------------------------------------------------------- /29-1-login.php: -------------------------------------------------------------------------------- 1 |
2 | Employee ID:
3 | 4 | 5 |
6 | -------------------------------------------------------------------------------- /29-2-viewbonus.php: -------------------------------------------------------------------------------- 1 | prepare("SELECT calculate_bonus(?) AS bonus"); 11 | 12 | $stmt->bind_param('s', $eid); 13 | 14 | $stmt->execute(); 15 | 16 | $stmt->bind_result($bonus); 17 | 18 | $stmt->fetch(); 19 | 20 | printf("Your bonus is \$%01.2f",$bonus); 21 | ?> 22 | -------------------------------------------------------------------------------- /29-3.php: -------------------------------------------------------------------------------- 1 | query("CALL get_employees()"); 7 | 8 | // Loop through the results 9 | while (list($employee_id, $name, $position) = $result->fetch_row()) { 10 | echo "$employee_id, $name, $position
"; 11 | } 12 | 13 | ?> 14 | -------------------------------------------------------------------------------- /30-1.php: -------------------------------------------------------------------------------- 1 | query($query); 19 | 20 | ?> 21 | -------------------------------------------------------------------------------- /30-2.php: -------------------------------------------------------------------------------- 1 | 0, 'max_range' => 1); 8 | $email = filter_var($_POST['email'], FILTER_VALIDATE_EMAIL); 9 | $available = filter_var($_POST['available'], FILTER_VALIDATE_INT, $options); 10 | 11 | // Create the UPDATE query 12 | $stmt = $mysqli->prepare("UPDATE technicians SET available=? WHERE email=?"); 13 | 14 | $stmt->bind_param('is', $available, $email); 15 | 16 | // Execute query and offer user output 17 | if ($stmt->execute()) { 18 | 19 | echo "

Thank you for updating your profile.

"; 20 | 21 | if ($available == 0) { 22 | echo "

Your tickets will be reassigned to another technician.

"; 23 | } 24 | 25 | } else { 26 | echo "

There was a problem updating your profile.

"; 27 | } 28 | 29 | ?> 30 | -------------------------------------------------------------------------------- /31-1.php: -------------------------------------------------------------------------------- 1 | query($query)) { 11 | 12 | printf(""); 13 | printf(""); 14 | 15 | // Output the headers 16 | $fields = $result->fetch_fields(); 17 | foreach ($fields as $field) 18 | printf("", $field->name); 19 | 20 | printf(""); 21 | 22 | // Output the results 23 | while ($employee = $result->fetch_assoc()) { 24 | // Format the phone number 25 | $phone = preg_replace("/([0-9]{3})([0-9]{3})([0-9]{4})/", 26 | "(\\1) \\2-\\3", $employee['Telephone']); 27 | 28 | printf(""); 29 | printf("", $employee['First Name'], $employee['Last Name']); 30 | printf("", $employee['Email Address'], $phone); 31 | printf(""); 32 | 33 | } 34 | 35 | } 36 | ?> 37 | -------------------------------------------------------------------------------- /32-1.php: -------------------------------------------------------------------------------- 1 | ["12309","45633","2010-12-19 01:13:42","$22.04","$5.67","$27.71"], 10 | '1' => ["12310","942","2010-12-19 01:15:12","$11.50","$3.40","$14.90"], 11 | '2' => ["12311","7879","2010-12-19 01:15:22","$95.99","$15.00","$110.99"], 12 | '3' => ["12312","55521","2010-12-19 01:30:45","$10.75","$3.00","$13.75"] 13 | ); 14 | 15 | // Create an array of table attributes 16 | $attributes = array('border' => '1'); 17 | 18 | // Create the table object 19 | 20 | $table = new HTML_Table($attributes); 21 | 22 | // Set the headers 23 | 24 | $table->setHeaderContents(0, 0, "Order ID"); 25 | $table->setHeaderContents(0, 1, "Client ID"); 26 | $table->setHeaderContents(0, 2, "Order Time"); 27 | $table->setHeaderContents(0, 3, "Sub Total"); 28 | $table->setHeaderContents(0, 4, "Shipping Cost"); 29 | $table->setHeaderContents(0, 5, "Total Cost"); 30 | 31 | // Cycle through the array to produce the table data 32 | 33 | for($rownum = 0; $rownum < count($salesreport); $rownum++) { 34 | for($colnum = 0; $colnum < 6; $colnum++) { 35 | $table->setCellContents($rownum+1, $colnum, 36 | $salesreport[$rownum][$colnum]); 37 | } 38 | } 39 | 40 | // Output the data 41 | 42 | echo $table->toHTML(); 43 | 44 | ?> 45 | -------------------------------------------------------------------------------- /32-2.php: -------------------------------------------------------------------------------- 1 | '1'); 11 | 12 | // Create the table object 13 | $table = new HTML_Table($attributes); 14 | 15 | // Set the headers 16 | 17 | $table->setHeaderContents(0, 0, "Order ID"); 18 | $table->setHeaderContents(0, 1, "Client ID"); 19 | $table->setHeaderContents(0, 2, "Order Time"); 20 | $table->setHeaderContents(0, 3, "Sub Total"); 21 | $table->setHeaderContents(0, 4, "Shipping Cost"); 22 | $table->setHeaderContents(0, 5, "Total Cost"); 23 | 24 | // Cycle through the array to produce the table data 25 | 26 | // Create and execute the query 27 | $query = "SELECT id AS `Order ID`, client_id AS `Client ID`, 28 | order_time AS `Order Time`, 29 | CONCAT('$', sub_total) AS `Sub Total`, 30 | CONCAT('$', shipping_cost) AS `Shipping Cost`, 31 | CONCAT('$', total_cost) AS `Total Cost` 32 | FROM sales ORDER BY id"; 33 | 34 | $stmt = $mysqli->prepare($query); 35 | 36 | $stmt->execute(); 37 | 38 | $stmt->bind_result($orderID, $clientID, $time, $subtotal, $shipping, $total); 39 | 40 | // Begin at row 1 so don't overwrite the header 41 | $rownum = 1; 42 | 43 | // Format each row 44 | 45 | while ($stmt->fetch()) { 46 | 47 | $table->setCellContents($rownum, 0, $orderID); 48 | $table->setCellContents($rownum, 1, $clientID); 49 | $table->setCellContents($rownum, 2, $time); 50 | $table->setCellContents($rownum, 3, $subtotal); 51 | $table->setCellContents($rownum, 4, $shipping); 52 | $table->setCellContents($rownum, 5, $total); 53 | 54 | $rownum++; 55 | 56 | } 57 | 58 | // Output the data 59 | echo $table->toHTML(); 60 | 61 | // Close the MySQL connection 62 | $mysqli->close(); 63 | 64 | ?> 65 | -------------------------------------------------------------------------------- /33-1.php: -------------------------------------------------------------------------------- 1 |

2 | Search the employee database:
3 |
4 | Last name:
5 |
6 | 7 | 8 |

9 | 10 | prepare("SELECT firstname, lastname, email FROM employees 21 | WHERE lastname like ?"); 22 | 23 | $stmt->bind_param('s', $_POST['lastname']); 24 | 25 | $stmt->execute(); 26 | 27 | $stmt->store_result(); 28 | 29 | // If records found, output them 30 | if ($stmt->num_rows > 0) { 31 | 32 | $stmt->bind_result($firstName, $lastName, $email); 33 | 34 | while ($stmt->fetch()) 35 | printf("%s, %s (%s)
", $lastName, $firstName, $email); 36 | } else { 37 | echo "No results found."; 38 | } 39 | 40 | } 41 | ?> 42 | -------------------------------------------------------------------------------- /33-2.php: -------------------------------------------------------------------------------- 1 |

2 | Search the employee database:
3 |
4 | Keyword:
5 |
6 | Field:
7 | 12 | 13 | 14 |

15 | 16 | prepare("SELECT firstname, lastname, email 26 | FROM employees WHERE lastname like ?"); 27 | } elseif ($_POST['field'] == "email") { 28 | $stmt = $db->prepare("SELECT firstname, lastname, email 29 | FROM employees WHERE email like ?"); 30 | } 31 | 32 | $stmt->bind_param('s', $_POST['keyword']); 33 | 34 | $stmt->execute(); 35 | 36 | $stmt->store_result(); 37 | 38 | // If records found, output them 39 | if ($stmt->num_rows > 0) { 40 | 41 | $stmt->bind_result($firstName, $lastName, $email); 42 | 43 | while ($stmt->fetch()) 44 | printf("%s, %s (%s)
", $lastName, $firstName, $email); 45 | 46 | } else { 47 | echo "No results found."; 48 | } 49 | } 50 | ?> 51 | -------------------------------------------------------------------------------- /33-3.php: -------------------------------------------------------------------------------- 1 |

2 | Search the online resources database:
3 |
4 | Keywords:
5 |
6 | 7 | 8 |

9 | 10 | prepare("SELECT name, url FROM bookmarks 20 | WHERE MATCH(description) AGAINST(?)"); 21 | 22 | $stmt->bind_param('s', $_POST['keywords']); 23 | 24 | $stmt->execute(); 25 | 26 | $stmt->store_result(); 27 | 28 | // Output retrieved rows or display appropriate message 29 | if ($stmt->num_rows > 0) { 30 | 31 | $stmt->bind_result($url, $name); 32 | 33 | while ($result->fetch) 34 | printf("%s
", $url, $name); 35 | } else { 36 | printf("No results found."); 37 | } 38 | } 39 | ?> 40 | -------------------------------------------------------------------------------- /34-1.php: -------------------------------------------------------------------------------- 1 | getParticipantKey(); 7 | 8 | // Retrieve the item seller and price using some fictitious item class 9 | $item = new Item(); 10 | $sellerID = $item->getItemOwner($itemID); 11 | $price = $item->getPrice($itemID); 12 | 13 | // Instantiate the mysqli class 14 | $db = new mysqli("localhost","website","secret","chapter34"); 15 | 16 | // Disable the autocommit feature 17 | $db->autocommit(FALSE); 18 | 19 | // Debit buyer's account 20 | 21 | $stmt = $db->prepare("UPDATE participants SET cash = cash - ? WHERE id = ?"); 22 | 23 | $stmt->bind_param('di', $price, $buyerID); 24 | 25 | $stmt->execute(); 26 | 27 | // Credit seller's account 28 | $query = $db->prepare("UPDATE participants SET cash = cash + ? WHERE id = ?"); 29 | 30 | $stmt->bind_param('di', $price, $sellerID); 31 | 32 | $stmt->execute(); 33 | 34 | // Update trunk item ownership. If it fails, set $success to FALSE 35 | $stmt = $db->prepare("UPDATE trunks SET owner = ? WHERE id = ?"); 36 | 37 | $stmt->bind_param('ii', $buyerID, $itemID); 38 | 39 | $stmt->execute(); 40 | 41 | if ($db->commit()) { 42 | echo "The swap took place! Congratulations!"; 43 | } else { 44 | echo "There was a problem with the swap!"; 45 | } 46 | 47 | ?> 48 | -------------------------------------------------------------------------------- /35-1.php: -------------------------------------------------------------------------------- 1 | query($query); 24 | } 25 | 26 | fclose($fh); 27 | $mysqli->close(); 28 | ?> 29 | -------------------------------------------------------------------------------- /35-1.sql: -------------------------------------------------------------------------------- 1 | CREATE TABLE sales ( 2 | id SMALLINT UNSIGNED AUTO_INCREMENT PRIMARY KEY, 3 | client_id SMALLINT UNSIGNED NOT NULL, 4 | order_time TIMESTAMP NOT NULL, 5 | sub_total DECIMAL(8,2) NOT NULL, 6 | shipping_cost DECIMAL(8,2) NOT NULL, 7 | total_cost DECIMAL(8,2) NOT NULL 8 | ); 9 | -------------------------------------------------------------------------------- /4-1.php: -------------------------------------------------------------------------------- 1 | 0) { 31 | $paymentNumber++; 32 | amortizationTable($paymentNumber, $periodicPayment, 33 | $newBalance, $monthlyInterest); 34 | } 35 | 36 | return $table; 37 | } 38 | 39 | -------------------------------------------------------------------------------- /4-2.php: -------------------------------------------------------------------------------- 1 | "; 40 | echo " 41 | 42 | 43 | "; 44 | 45 | foreach($rows as $row) { 46 | printf("", $row[0]); 47 | printf("", $row[1]); 48 | printf("", $row[2]); 49 | printf("", $row[3]); 50 | printf("", $row[4]); 51 | } 52 | 53 | // Close table 54 | echo "
%s
%s%s%s%s
Payment NumberBalancePaymentPrincipalInterest
%d$%s$%s$%s$%s
"; 55 | ?> 56 | 57 | -------------------------------------------------------------------------------- /5-1.php: -------------------------------------------------------------------------------- 1 |
2 |

3 | Provide up to six keywords that you believe best describe the state in 4 | which you live: 5 |

6 |

Keyword 1:
7 |

8 |

Keyword 2:
9 |

10 |

Keyword 3:
11 |

12 |

Keyword 4:
13 |

14 |

Keyword 5:
15 |

16 |

Keyword 6:
17 |

18 |

19 |
20 | 21 | -------------------------------------------------------------------------------- /6-1.php: -------------------------------------------------------------------------------- 1 | name; 11 | } 12 | 13 | public function setName($name) { 14 | $this->name = $name; 15 | } 16 | 17 | public function sayHello() { 18 | echo "Hi, my name is {$this->getName()}."; 19 | } 20 | 21 | } 22 | 23 | -------------------------------------------------------------------------------- /7-1.php: -------------------------------------------------------------------------------- 1 | name = $name; 6 | } 7 | function getName() { 8 | return $this->name; 9 | } 10 | } 11 | 12 | $emp1 = new Employee(); 13 | $emp1->setName('John Smith'); 14 | $emp2 = $emp1; 15 | $emp2->setName('Jane Smith'); 16 | 17 | echo "Employee 1 = {$emp1->getName()}\n"; 18 | echo "Employee 2 = {$emp2->getName()}\n"; 19 | 20 | -------------------------------------------------------------------------------- /7-2.php: -------------------------------------------------------------------------------- 1 | employeeid = $employeeid; 8 | } 9 | 10 | function getEmployeeID() { 11 | return $this->employeeid; 12 | } 13 | 14 | // Define a setter and getter for $tiecolor 15 | function setTieColor($tiecolor) { 16 | $this->tiecolor = $tiecolor; 17 | } 18 | 19 | function getTieColor() { 20 | return $this->tiecolor; 21 | } 22 | } 23 | 24 | // Create new Employee object 25 | $employee1 = new Employee(); 26 | 27 | // Set the $employee1 employeeid property 28 | $employee1->setEmployeeID("12345"); 29 | 30 | // Set the $employee1 tiecolor property 31 | $employee1->setTieColor("red"); 32 | 33 | // Clone the $employee1 object 34 | $employee2= clone $employee1; 35 | 36 | // Set the $employee2 employeeid property 37 | $employee2->setEmployeeID("67890"); 38 | 39 | // Output the $employee1and $employee2employeeid properties 40 | 41 | printf("Employee 1 employeeID: %d
", $employee1->getEmployeeID()); 42 | printf("Employee 1 tie color: %s
", $employee1->getTieColor()); 43 | 44 | printf("Employee 2 employeeID: %d
", $employee2->getEmployeeID()); 45 | printf("Employee 2 tie color: %s
", $employee2->getTieColor()); 46 | 47 | ?> 48 | 49 | -------------------------------------------------------------------------------- /7-3.php: -------------------------------------------------------------------------------- 1 | setEmployeeID("12345"); 8 | 9 | // Clone the $employee1 object 10 | $employee2 = clone $employee1; 11 | 12 | // Set the $employee2 employeeid property 13 | $employee2->setEmployeeID("67890"); 14 | 15 | // Output the $employee1 and $employee2 employeeid properties 16 | printf("Employee1 employeeID: %d
", $employee1->getEmployeeID()); 17 | printf("Employee1 tie color: %s
", $employee1->getTieColor()); 18 | printf("Employee2 employeeID: %d
", $employee2->getEmployeeID()); 19 | printf("Employee2 tie color: %s
", $ employee2->getTieColor()); 20 | 21 | -------------------------------------------------------------------------------- /7-4.php: -------------------------------------------------------------------------------- 1 | name = $name; 11 | } 12 | 13 | // Define a getter for the private $name property 14 | function getName() { 15 | return "My name is ".$this->name."
"; 16 | } 17 | } // end Employee class 18 | 19 | // Define an Executive class that inherits from Employee 20 | class Executive extends Employee { 21 | 22 | // Define a method unique to Employee 23 | function pillageCompany() { 24 | echo "I'm selling company assets to finance my yacht!"; 25 | } 26 | 27 | } // end Executive class 28 | 29 | // Create a new Executive object 30 | $exec = new Executive(); 31 | 32 | // Call the setName() method, defined in the Employee class 33 | $exec->setName("Richard"); 34 | 35 | // Call the getName() method 36 | echo $exec->getName(); 37 | 38 | // Call the pillageCompany() method 39 | $exec->pillageCompany(); 40 | ?> 41 | 42 | -------------------------------------------------------------------------------- /7-5.php: -------------------------------------------------------------------------------- 1 | name = $name; 9 | } 10 | 11 | function setSalary($salary) { 12 | $this->salary = $salary; 13 | } 14 | 15 | function getSalary() { 16 | return $this->salary; 17 | } 18 | } 19 | 20 | class Executive extends Employee { 21 | function pillageCompany() { 22 | $this->setSalary($this->getSalary() * 10); 23 | } 24 | } 25 | 26 | class CEO extends Executive { 27 | function getFacelift() { 28 | echo "nip nip tuck tuck\n"; 29 | } 30 | } 31 | 32 | $ceo = new CEO(); 33 | $ceo->setName("Bernie"); 34 | $ceo->setSalary(100000); 35 | $ceo->pillageCompany(); 36 | $ceo->getFacelift(); 37 | echo "Bernie's Salary is: {$ceo->getSalary()}\n"; 38 | ?> 39 | 40 | -------------------------------------------------------------------------------- /8-1.php: -------------------------------------------------------------------------------- 1 | language = $language; 6 | $this->errorcode = $errorcode; 7 | } 8 | function getMessageMap() { 9 | $errors = file("errors/{$this->language}.txt"); 10 | foreach($errors as $error) { 11 | list($key,$value) = explode(",", $error, 2); 12 | $errorArray[$key] = $value; 13 | } 14 | return $errorArray[$this->errorcode]; 15 | } 16 | } 17 | try { 18 | throw new MyException("english", 4); 19 | } 20 | catch (MyException $e) { 21 | echo $e->getMessageMap(); 22 | } 23 | -------------------------------------------------------------------------------- /8-2.php: -------------------------------------------------------------------------------- 1 | message = $message; 7 | $this->notifyAdmin($email); 8 | } 9 | 10 | private function notifyAdmin($email) { 11 | mail("admin@example.org","INVALID EMAIL",$email,"From:web@example.com"); 12 | } 13 | } 14 | 15 | /* The Subscribe class validates an e-mail address 16 | and adds the e-mail address to the database. */ 17 | class Subscribe { 18 | function validateEmail($email) { 19 | try { 20 | if ($email == "") { 21 | throw new Exception("You must enter an e-mail address!"); 22 | } else { 23 | list($user,$domain) = explode("@", $email); 24 | if (! checkdnsrr($domain, "MX")) 25 | throw new InvalidEmailException( 26 | "Invalid e-mail address!", $email); 27 | else 28 | return 1; 29 | } 30 | } catch (Exception $e) { 31 | echo $e->getMessage(); 32 | } catch (InvalidEmailException $e) { 33 | echo $e->getMessage(); 34 | $e->notifyAdmin($email); 35 | } 36 | } 37 | /* Add the e-mail address to the database */ 38 | function subscribeUser() { 39 | echo $this->email." added to the database!"; 40 | } 41 | } 42 | 43 | // Assume that the e-mail address came from a subscription form 44 | $_POST['email'] = "someuser@example.com"; 45 | 46 | /* Attempt to validate and add address to database. */ 47 | if (isset($_POST['email'])) { 48 | $subscribe = new Subscribe(); 49 | if($subscribe->validateEmail($_POST['email'])) 50 | $subscribe->subscribeUser($_POST['email']); 51 | } 52 | ?> 53 | -------------------------------------------------------------------------------- /9-1.php: -------------------------------------------------------------------------------- 1 | 'World Wide Web', 8 | 'IRS' => 'Internal Revenue Service', 9 | 'PDF' => 'Portable Document Format'); 10 | 11 | if (isset($acronyms[$matches[1]])) 12 | return $acronyms[$matches[1]] . " (" . $matches[1] . ")"; 13 | else 14 | return $matches[1]; 15 | } 16 | 17 | // The target text 18 | $text = "The IRS offers tax forms in 19 | PDF format on the WWW."; 20 | 21 | // Add the acronyms' long forms to the target text 22 | $newtext = preg_replace_callback("/(.*)<\/acronym>/U", 'acronym', 23 | $text); 24 | 25 | print_r($newtext); 26 | 27 | ?> 28 | -------------------------------------------------------------------------------- /9-2.php: -------------------------------------------------------------------------------- 1 | IRS offers tax forms in 5 | PDF format on the WWW."; 6 | 7 | // Add the acronyms' long forms to the target text 8 | $newtext = preg_replace_callback("/(.*)<\/acronym>/U", 9 | function($matches) { 10 | $acronyms = array( 11 | 'WWW' => 'World Wide Web', 12 | 'IRS' => 'Internal Revenue Service', 13 | 'PDF' => 'Portable Document Format'); 14 | 15 | if (isset($acronyms[$matches[1]])) 16 | return $acronyms[$matches[1]] . " (" . $matches[1] . ")"; 17 | else 18 | return $matches[1]; 19 | }, 20 | $text); 21 | print_r($newtext); 22 | 23 | ?> 24 | -------------------------------------------------------------------------------- /9-3.php: -------------------------------------------------------------------------------- 1 | PHP’s. 7 | summary; 8 | $words = sizeof(explode(' ',strip_tags($summary))); 9 | echo "Total words in summary: $words"; 10 | ?> 11 | -------------------------------------------------------------------------------- /9-4.php: -------------------------------------------------------------------------------- 1 | 22 | -------------------------------------------------------------------------------- /9-5.php: -------------------------------------------------------------------------------- 1 | PHP’s. 10 | summary; 11 | 12 | if (strlen($summary) > $limit) 13 | $summary = substr($summary, 0, strrpos(substr($summary, 0, $limit), 14 | ' ')) . '...'; 15 | echo $summary; 16 | ?> 17 | -------------------------------------------------------------------------------- /9-6.php: -------------------------------------------------------------------------------- 1 | "; 12 | } 13 | ?> 14 | -------------------------------------------------------------------------------- /9-7.php: -------------------------------------------------------------------------------- 1 | $frequency) 8 | echo "Character ".chr($letter)." appears $frequency times
"; 9 | ?> 10 | -------------------------------------------------------------------------------- /9-8.php: -------------------------------------------------------------------------------- 1 | PHP’s. 7 | summary; 8 | $words = str_word_count($summary); 9 | printf("Total words in summary: %s", $words); 10 | ?> 11 | -------------------------------------------------------------------------------- /9-9.php: -------------------------------------------------------------------------------- 1 | PHP’s. 7 | summary; 8 | $words = str_word_count($summary,2); 9 | $frequency = array_count_values($words); 10 | print_r($frequency); 11 | ?> 12 | -------------------------------------------------------------------------------- /9781430260431.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Apress/beginning-php-and-mysql-5e/4ef0f8355ee4f79f9326e9cb3bb3b80e53871742/9781430260431.jpg -------------------------------------------------------------------------------- /Contributing.md: -------------------------------------------------------------------------------- 1 | # Contributing to Apress Source Code 2 | 3 | Copyright for Apress source code belongs to the author(s). However, under fair use you are encouraged to fork and contribute minor corrections and updates for the benefit of the author(s) and other readers. 4 | 5 | ## How to Contribute 6 | 7 | 1. Make sure you have a GitHub account. 8 | 2. Fork the repository for the relevant book. 9 | 3. Create a new branch on which to make your change, e.g. 10 | `git checkout -b my_code_contribution` 11 | 4. Commit your change. Include a commit message describing the correction. Please note that if your commit message is not clear, the correction will not be accepted. 12 | 5. Submit a pull request. 13 | 14 | Thank you for your contribution! -------------------------------------------------------------------------------- /ConvertController.php: -------------------------------------------------------------------------------- 1 | json([ 27 | 'to' => round($_POST['from'] * $_POST['fromUnit'] / $_POST['toUnit'], 2), 28 | ]); 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /LICENSE.txt: -------------------------------------------------------------------------------- 1 | Freeware License, some rights reserved 2 | 3 | Copyright (c) 2018 Frank Kromann 4 | 5 | Permission is hereby granted, free of charge, to anyone obtaining a copy 6 | of this software and associated documentation files (the "Software"), 7 | to work with the Software within the limits of freeware distribution and fair use. 8 | This includes the rights to use, copy, and modify the Software for personal use. 9 | Users are also allowed and encouraged to submit corrections and modifications 10 | to the Software for the benefit of other users. 11 | 12 | It is not allowed to reuse, modify, or redistribute the Software for 13 | commercial use in any way, or for a user’s educational materials such as books 14 | or blog articles without prior permission from the copyright holder. 15 | 16 | The above copyright notice and this permission notice need to be included 17 | in all copies or substantial portions of the software. 18 | 19 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 20 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 21 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 22 | AUTHORS OR COPYRIGHT HOLDERS OR APRESS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 23 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 24 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 25 | SOFTWARE. 26 | 27 | 28 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Apress Source Code 2 | 3 | This repository accompanies [*Beginning PHP and MySQL*](https://www.apress.com/9781430260431) by Frank Kromann (Apress, 2018). 4 | 5 | [comment]: #cover 6 | ![Cover image](9781430260431.jpg) 7 | 8 | Download the files as a zip using the green button, or clone the repository to your machine using Git. 9 | 10 | ## Releases 11 | 12 | Release v1.0 corresponds to the code in the published book, without corrections or updates. 13 | 14 | ## Contributions 15 | 16 | See the file Contributing.md for more information on how you can contribute to this repository. -------------------------------------------------------------------------------- /aes.inc: -------------------------------------------------------------------------------- 1 | key = $key; 9 | $this->cipher = $cipher; 10 | } 11 | 12 | function encrypt($data) { 13 | if (in_array($this->cipher, openssl_get_cipher_methods())) { 14 | $ivlen = openssl_cipher_iv_length($this->cipher); 15 | $iv = openssl_random_pseudo_bytes($ivlen); 16 | $encrypted = openssl_encrypt($data, $this->cipher, $this->key, OPENSSL_RAW_DATA, $iv); 17 | $hmac = hash_hmac('sha256', $encrypted, $this->key, true); 18 | return base64_encode($iv.$hmac.$encrypted); 19 | } 20 | else { 21 | return null; 22 | } 23 | } 24 | 25 | function decrypt($data) { 26 | $c = base64_decode($data); 27 | $ivlen = openssl_cipher_iv_length($this->cipher); 28 | $iv = substr($c, 0, $ivlen); 29 | $hmac = substr($c, $ivlen, $sha2len=32); 30 | $encrypted = substr($c, $ivlen+$sha2len); 31 | $hmac_check = hash_hmac('sha256', $encrypted, $this->key, true); 32 | if (hash_equals($hmac, $hmac_check)) { 33 | return openssl_decrypt($encrypted, $this->cipher, $this->key, OPENSSL_RAW_DATA, $iv); 34 | } 35 | else { 36 | return null; 37 | } 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /authentication.txt: -------------------------------------------------------------------------------- 1 | jason:68c46a606457643eab92053c1c05574abb26f861 2 | donald:53e11eb7b24cc39e33733a0ff06640f1b39425ea 3 | mickey:1aa25ead3880825480b6c0197552d90eb5d48d23 4 | -------------------------------------------------------------------------------- /convertForm.blade.php: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | Unit Converter 8 | 9 | 10 | 11 | 12 | 13 | 65 | 69 | 70 | 71 |
72 |
73 |
74 | Unit Converter 75 |
76 | 77 | 101 |
102 |
103 | 125 | 126 | 127 | -------------------------------------------------------------------------------- /errata.md: -------------------------------------------------------------------------------- 1 | # Errata for *Beginning PHP and MySQL* 2 | 3 | On **page 141** There are 2 errors on this page in the same line of code: 4 | 5 | $carBrands = ["Cheverolet", "Chrysler""Ford", "Honda", "Toyota"); 6 | 7 | The first error is the closing parentheses ) instead it should be a bracket ] The second error is inside the array there is a missing comma between "Chrysler""Ford" it should be "Chrysler", "Ford". 8 | 9 | The correct syntax for this array is: 10 | $carBrands = ["Cheverolet", "Chrysler", "Ford", "Honda", "Toyota"]; ] 11 | 12 | *** 13 | 14 | On **page xx** [Summary of error]: 15 | 16 | Details of error here. Highlight key pieces in **bold**. 17 | 18 | *** 19 | -------------------------------------------------------------------------------- /log_service.php: -------------------------------------------------------------------------------- 1 | authenticate()) { 28 | $entry = gmdate('Y/m/d H:i:s') . ' ' . $_SERVER['REMOTE_ADDR'] . ' ' . $_GET['Msg']; 29 | file_put_contents('/log/' . $filename .'.log', $entry . "\n", FILE_APPEND); 30 | header('Content-Type: application/json'); 31 | echo json_encode(true); 32 | } 33 | else { 34 | header('Content-Type: application/problem+json'); 35 | echo json_encode(false); 36 | } 37 | } 38 | 39 | public function getEvents() { 40 | if ($filename = $this->authenticate()) { 41 | header('Content-Type: text/plain'); 42 | readfile('/log/' . $filename .'.log'); 43 | } 44 | else { 45 | header('Content-Type: application/problem+json'); 46 | echo json_encode(false); 47 | } 48 | } 49 | }; 50 | -------------------------------------------------------------------------------- /logger.php: -------------------------------------------------------------------------------- 1 | 15 | --------------------------------------------------------------------------------