Requests and sessions
A page's PHP reads what the request brought, keeps what has to outlive it in the session, and builds the address the visitor goes to next. The engine already turns request values into variables - $padRequestVars - and keeps the $padSessionVars names in the session; these helpers do the same by name, from PHP, whatever those settings let through.
| function | what it does |
|---|---|
padRequest ( $key, $default )
|
one value of the input by name or dot path - a post over the query, the body of a JSON request - or all of it |
padRequestHas ( $keys )
|
whether every name was sent - an emptied field was |
padRequestFilled ( $key )
|
whether it was sent and is not blank |
padRequestOnly ( $keys )
|
the input with these names only |
padRequestExcept ( $keys )
|
the input without these names |
padRequestMethod ()
|
GET, POST, PUT ... - GET on the command line |
padRequestIs ( $method )
|
whether the method is this one, or one of a list |
padSession ( $key, $default )
|
one value of the session, or all of it - never starts a session |
padSessionPut ( $key, $value )
|
keeps a value in the session, which starts on demand |
padSessionHas ( $keys )
|
whether every name is in the session |
padSessionPull ( $key, $default )
|
a value of the session, taken out of it |
padSessionForget ( $keys )
|
removes names from the session |
padSessionRegenerate ()
|
a new session id, the data kept - after a login |
padFlashInput ( $except )
|
keeps this request's input for the next request only |
padOld ( $key, $default )
|
reads that input there |
padUrl ( $page, $vars, $absolute )
|
a link to a page of this application |
padBack ( $fallback )
|
redirects to the page the visitor came from - never off-site |
padAbort ( $status, $message )
|
ends the request with an error status |
The input
padRequest reads a value by the name the form gave it, a dot path into an array - customer.name for a field named customer[name] - and answers the default when the request did not bring it. A posted value wins over a query value of the same name, the body of a request sent as application/json is input too, and every text is trimmed. The query key that names the page is no input.
|
PHP fragments/requests_and_sessions_1.php |
HTML fragments/requests_and_sessions_1.pad |
Result |
|---|---|---|
|
|
|
- The names of
padRequestHas,padRequestOnlyandpadRequestExceptare an array or one text with commas:'customer.email, note'. - Blank is nothing, an empty or whitespace-only text, an empty array; a
'0'is filled. - A
*in a dot path reads every item of a list:padRequest ( 'items.*.id' ).
The session
padSessionPut starts the session when there is none yet; reading never does - a visitor without a session cookie gets the default, and no session is made to find that out. Dot paths work here too:
|
PHP fragments/requests_and_sessions_2.php |
HTML fragments/requests_and_sessions_2.pad |
Result |
|---|---|---|
|
|
|
A name listed in $padSessionVars is a variable as well as a session value: the helpers keep the two in step, so a value put in is the one the end of the request writes back. After a login, padSessionRegenerate () moves the session to a new id, so an id someone learned before it is worth nothing.
The form after a failed post
The page a form posts to checks it; when something is wrong it keeps the input with padFlashInput () and sends the browser back to the form with padBack (). The form's page reads the input with padOld - on that one next request only, like a Flash messages message. Passwords and the CSRF token are left out unless another list is given.
Without a list, every field whose name looks like a password, a key, a token, a card's code or number or a one-time code stays out as well - new_password, user[password], api_key, card_number, otp_code. That is judged by the words of the name, and no word rule knows every name: a field it does not recognise, a stripe_key, is kept. For a form with such a field, give the list yourself - it replaces the default, so it names every field that stays out - or flash only the fields the form needs back: padFlashInput ( array_diff ( array_keys ( padRequestExcept ( [] ) ), [ 'name', 'email' ] ) ).
|
PHP fragments/requests_and_sessions_3.php |
HTML fragments/requests_and_sessions_3.pad |
Result |
|---|---|---|
|
|
|
// signup.php - the page the form posts to
$errors = padValidate ( [ 'email' => 'required|email' ] );
if ( $errors ) {
padFlash ( 'Please check the form.', 'error' );
padFlashInput ();
padBack ( 'signup/form' );
}
// signup/form.php - the form, filled again
$email = padOld ( 'email' ); // {input 'email', value=$email}
Links
padUrl writes a link to a page of this application: the values as a query, arrays too, in the clean form /myapp/products/42 when $padCleanUrls is on (see Clean urls). Without a page it is the page the visitor asked for; a third argument TRUE makes it absolute, on $padHost, for a mail or a feed.
|
PHP fragments/requests_and_sessions_4.php |
HTML fragments/requests_and_sessions_4.pad |
Result |
|---|---|---|
|
|
|
Going back, and stopping
Both end the request, so they are shown here, not run. padBack follows the browser's Referer only when it is a page of this application on this host, and goes to the fallback page otherwise - the index when none is given. padAbort answers an error status, 400 to 599, with the message as plain text - or the status's own words - through the engine's exit, never PHP's (see Ending the request):
padBack ( 'orders' ); // back where the visitor came from, else orders
if ( ! $order )
padAbort ( 404 ); // 404 Not Found
if ( padRequestIs ( 'delete' ) and ! $admin )
padAbort ( 403, 'Only an administrator can delete an order.' );