1
0
mirror of https://github.com/restoreddev/phpapprentice.git synced 2025-07-30 19:40:43 +02:00

Created initial site setup with hugo

This commit is contained in:
Andrew Davis
2019-07-12 19:21:18 -05:00
parent 77a0d2616b
commit e09557cf2c
80 changed files with 732 additions and 10488 deletions

67
content/web/01-http.md Normal file
View File

@@ -0,0 +1,67 @@
HTTP stands for Hypertext Transfer Protocol. It is a standard for how requests and responses should be formatted for
a server and a web browser. When you open a link in your web browser, you are sending a HTTP request to a server and it
is responding with a HTTP response. The browser then takes the response, parses it and displays it to the user.
Let us look at a HTTP request for PHP Apprentice. This is the request sent to the server from Firefox:
```http
GET /basics.html HTTP/2.0
Host: phpapprentice.com
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.14; rv:65.0) Gecko/20100101 Firefox/65.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate, br
Connection: keep-alive
Upgrade-Insecure-Requests: 1
Pragma: no-cache
Cache-Control: no-cache
```
The first line of the request tells us three things. First, the method of the request. A `GET` request tells the server
that the browser just wants to download whatever is stored at the link. The second value `/basics.html` determines
what should be loaded from `phpapprentice.com`. Finally, the third value tells the server what type of HTTP request it is.
All of the lines below the top line are key/value pairs called headers. They give the server different pieces of information
about the client and the request to the server. For example, `Host` tells the server what website the browser is
trying to access.
In response to the client's HTTP request, the server will respond with a HTTP response. Here is the response for the above request:
```http
HTTP/2.0 200 OK
server: GitHub.com
content-type: text/html; charset=utf-8
last-modified: Sun, 13 Jan 2019 22:57:50 GMT
etag: W/"5c3bc26e-15a0"
access-control-allow-origin: *
expires: Mon, 18 Feb 2019 02:38:31 GMT
cache-control: max-age=600
content-encoding: gzip
x-github-request-id: 8596:461C:46C99E:5BB366:5C6A184F
accept-ranges: bytes
date: Mon, 18 Feb 2019 03:06:02 GMT
via: 1.1 varnish
age: 52
x-served-by: cache-fty21342-FTY
x-cache: HIT
x-cache-hits: 2
x-timer: S1550459163.720652,VS0,VE0
vary: Accept-Encoding
x-fastly-request-id: 00f0eec6f3037e428b8fc86742fe0f9965501a51
content-length: 2084
```
The top line of the response is in two parts, similar to a request.
The first part `HTTP/2.0` indicates the connection type, like the client.
The second part `200 OK` is the response code. `200` indicates a successful response.
There are many different response codes, the most famous being `404 Not Found`.
A response will have headers as well, but they are to give the client information about the response. As you can see,
the `Content-Type` is set to `text/html`, which tells the client how to render the content of the response.
Notice that below the list of response headers, there is also the content of the response. In this case, it is just HTML
which the browser can you use to display the page.
HTTP requests and responses are the core of web communication. Next, we will look at a request type other than `GET`.
Notice that below the list of response headers, there is also the content of the response. In this case, it is just HTML
which the browser can you use to display the page.
HTTP requests and responses are the core of web communication. Next, we will look at a request type other than `GET`.

View File

@@ -0,0 +1,34 @@
HTTP uses multiple different request types for indicating actions that should be performed on the server. The most common ones
you will use are:
- GET -> Retrieve a resource
- POST -> Create a new resource
- PUT -> Replace an entire resource
- PATCH -> Update attributes of a resource
- DELETE -> Delete a resource
We have already seen a GET request. The next most common request type is POST. A POST request is structured the same as a GET request, however, it will contain data in the request body:
```http
POST /login.php HTTP/1.1
Host: localhost:8080
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.14; rv:65.0) Gecko/20100101 Firefox/65.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate
Referer: http://localhost:8080/login.php
Content-Type: application/x-www-form-urlencoded
Content-Length: 30
Connection: keep-alive
Upgrade-Insecure-Requests: 1
Pragma: no-cache
Cache-Control: no-cache
username=php&password=password
```
The above request is an example login request created by Firefox. You can see in the first line that POST is used to declare the request type.
At the bottom of the request, the contents contain the user's username and password. A server can easily parse the data and do something with it. In this case, it can authenticate the user.
POST requests are used to send data to a server. A server will process the data and perform an action or store the data.
We have been talking a lot about how a server can process requests and send responses. How do programmers develop a server to handle HTTP requests? With PHP!
PHP was built with web server development in mind. Let us write our first PHP server script.

View File

@@ -0,0 +1,58 @@
In PHP, you usually use a separate web server program that accepts HTTP requests and passes them to PHP to create a response. Common examples of separate web server programs are Apache and Nginx. However, PHP has a built in web server we can use during development.
To use the development web server, open a terminal and a new folder for holding your code. Create a file called `index.php` and put this PHP code in it:
```php
<?php
echo 'Hello World!';
```
Go back to your terminal and start a PHP web server using this command:
```bash
php -S localhost:8080
```
Now, you can open `localhost:8080` in your web browser and you will see 'Hello World!' in the content of the page.
Next, we can use PHP to analyze all the information about the HTTP request sent from the browser to your PHP script. Change your `index.php` to have this code:
```php
<?php
$method = $_SERVER['REQUEST_METHOD'];
$uri = $_SERVER['REQUEST_URI'];
$protocol = $_SERVER['SERVER_PROTOCOL'];
$headers = getallheaders();
echo "$method $uri $protocol <br/>";
foreach ($headers as $key => $header) {
echo "$key: $header <br/>";
}
```
Reload `localhost:8080` and you will see the main request line and all of the headers printed on the web page. The `<br/>` tag is HTML for a new line. Since we are outputting to a web browser, we can no longer use `"\n"` to create a new line.
By default, PHP will generate the necessary response line and headers. If you open your browser console and open the network tab, you can see the response code and headers.
```http
HTTP/1.1 200 OK
Host: localhost:8080
Date: Sun, 28 Apr 2019 10:19:25 -0500
Connection: close
X-Powered-By: PHP/7.2.17
Content-type: text/html; charset=UTF-8
```
PHP set the response code to `200 OK` and the content type to `text/html`. Even though PHP will output good defaults, we can change them in our code if we want. Update your `index.php` file with this code:
```php
<?php
http_response_code(404);
header('Content-Type: text/plain');
echo "Not Found!";
```
We are changing the status code to `404 Not Found` and the content type to plain text. If you open `localhost:8080` in your browser, you will now see this HTTP response in your console:
```http
HTTP/1.1 404 Not Found
Host: localhost:8080
Date: Sun, 28 Apr 2019 10:26:40 -0500
Connection: close
X-Powered-By: PHP/7.2.17
Content-type: text/plain;charset=UTF-8
```
PHP added the other necessary headers for the response, but this time used `404` in the main response line and set the `Content-type` header to `text/plain`.