-
-
Notifications
You must be signed in to change notification settings - Fork 3
Home
The default behaviour of PHP user input is handled globally via superglobals such as $_GET
, $_POST
, $_FILES
, etc. While this approach provides a straightforward method for simple user input data handling, it often encounters limitations, particularly when managing HTTP requests involving multiple fields or files.
This library provides a consistent and intuitive interface for handling HTTP user input via the query string, request body, and uploaded files. Type-safe functions are provided for working with the different data types expected with user input from query strings and HTML forms.
After integrating this library, it is recommended to prevent access to superglobals in PHP - see PHP.Gt/ProtectedGlobal for an automated solution.
You may want to validate user input on the server - see PHP.Gt/DomValidation for an automated solution.
This library and associated libraries are all part of PHP.Gt/WebEngine.
Example HTML form:
<form method="post">
<h1>User Profile</h1>
<label>
<span>Your name</span>
<input name="name" placeholder="e.g. Eugene Kaspersky" required />
</label>
<label>
<span>Age</span>
<input type="number" name="age" />
</label>
<label>
<span>Interests</span>
<select name="interest[]" multiple>
<option>Mathematics</option>
<option>Cryptography</option>
<option>Information Security</option>
<option>Cyberwarfare</option>
</select>
</label>
<label>
<span>Photo</span>
<input name="photo" type="file" />
</label>
<button name="do" value="submit">Save profile</button>
</form>
Example PHP utilising data submitted via this form:
use Gt\Input\Input;
function do_submit(int $profileId, Input $input):void {
$profile->update(
$profileId,
// Use type-safe getters to help write maintainable code.
$input->getString("name"),
$input->getInt("age"),
);
// Handle multiple values with type safety.
foreach($input->getMultipleString("interest") as $interest) {
$profile->addInterest($interest);
}
// Handle file uploads with a FileUpload object.
$photoUpload = $input->getFile("photo");
if($photoUpload instanceof FailedFileUpload) {
// Handle a failed upload here.
}
$photoUpload->moveTo("data/upload/$profileId.jpg");
}
Read the first page: Constructing the Input class.
PHP.Gt/Input is a separately maintained component of PHP.Gt/WebEngine.