Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,15 @@ require_once 'vendor/autoload.php';
use InitPHP\Input\Facade\Inputs as Input;

// GET /?name=Jane
// echo isset($_GET['name']) ? $_GET['name'] : 'John';
echo Input::get('name', 'John'); // 'Jane'
// $name = isset($_GET['name']) ? $_GET['name'] : 'John';
$name = Input::get('name', 'John'); // 'Jane'
```

> **Security:** `Input` returns request data as-is. Values may be arrays and may
> contain markup, so escape them for the context they are printed in, for example
> `htmlspecialchars($name, ENT_QUOTES, 'UTF-8')` inside HTML (after checking
> `is_string($name)`). Never `echo` a raw `Input` value into a page.

### With an instance

```php
Expand Down
11 changes: 9 additions & 2 deletions docs/Quick-Start.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,11 @@ require __DIR__ . '/vendor/autoload.php';
use InitPHP\Input\Facade\Inputs as Input;

// GET /?name=Jane
echo Input::get('name', 'John'); // Jane
$name = Input::get('name', 'John'); // 'Jane'

// Input returns the request value as-is (it can be an array, or contain markup).
// Escape it for the context you print it in — here, HTML:
echo htmlspecialchars(is_string($name) ? $name : 'John', ENT_QUOTES, 'UTF-8');
```

### An instance — explicit and testable
Expand All @@ -31,7 +35,10 @@ use InitPHP\Input\Inputs;

$input = new Inputs(); // reads $_GET, $_POST and php://input

echo $input->get('name', 'John');
$name = $input->get('name', 'John');

// Escape request data before printing it into HTML.
echo htmlspecialchars(is_string($name) ? $name : 'John', ENT_QUOTES, 'UTF-8');
```

Pass the sources in to make the object self-contained (great for tests):
Expand Down
8 changes: 7 additions & 1 deletion docs/Recipes.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,15 @@ Useful for endpoints that take parameters either in the URL or the body.
use InitPHP\Input\Facade\Inputs as Input;

// Checks $_GET first, then the JSON body.
$apiKey = Input::getRaw('api_key', null, ['required']);
$locale = Input::getRaw('locale', null, ['required']);
```

Only do this for non-secret parameters such as `locale`, `page` or `sort`. A
value read from the query string ends up in access logs, browser history and
`Referer` headers, so credentials such as API keys and tokens belong in the
`Authorization` header or the request body — read them with
`raw()` only (see [Source Priority](Source-Priority.md)), never from `$_GET`.

## A login form

```php
Expand Down
Loading