This is a collection of useful page functions for PHP web application.
$page = new Page( );
// Include core Page library
require_once 'class.Page.php';
// Initialize Page object
$page = new Page();
Gets client IP address.
string $page->getClientIp( );
// Display visitor IP address
echo 'You are connected from'.$page->getClientIp().'.';
Gets current page URL.
string $page->getCurrentUrl( );
// Display current page URL
echo $page->getCurrentUrl();
Gets browser user agent.
string $page->getUserAgent( );
// Display user agent
echo 'User Agent: ' . $page->getUserAgent();
Gets page referrer.
string $page->getReferer( );
// Display page referer
echo $page->getReferer();
Checks if current page is HTTPS.
bool $page->isHttps( );
// Redirect to HTTPS if current page is not HTTPS
if (false == $page->isHttps()) {
header('Location: https://www.example.com');
die;
}
Rewrites URL with new query string.
string $page->rewrite( string $url[, array $queries] );
$url = 'http://www.example.com/home.php?page=2&action=show&sort=date';
// Rewrite URL with new query string
echo $page->rewrite($url, [
'page' => 3,
'sort' => 'name',
]);
Result:
http://www.example.com/home.php?page=3&action=show&sort=name
###IsPost
Check whether the page request is a POST request.
bool $page->isPost();
if ($page->isPost()) {
echo 'A form is posted.';
}
Gets a value in HTTP GET
or POST
request.
string $page->request( string $key[, int $method = Page::BOTH] );
Method
Page::BOTH
Returns values in GET or POST that matches the key provided.
Page::GET
Returns values in GET that matches the key.
Page::POST
Returns values in POST that matches the key.
// URL - http://www.example.com/home.php?page=1&sort=name&text=<strong>Example</strong>
echo 'Sort: '.$page->request('sort').'<br />';
echo 'Text (HTML): '.$page->request('text').'<br />';
Result:
Sort: name Text (HTML): Example
Gets a single or an array of server variable.
string $page->getVariable( string|array $key );
// Get browser language
echo "Browser Language: " . $page->getVariable('HTTP_ACCEPT_LANGUAGE');
// Get multiple server variables
$values = $page->getVariables(['SERVER_PROTOCOL', 'REQUEST_METHOD', 'QUERY_STRING']);
echo "Protocol : " . $values['SERVER_PROTOCOL'] . "\n";
echo "Method : " . $values['REQUEST_METHOD'] . "\n";
echo "Query : " . $values['QUERY_STRING'] . "\n";
Sets a cookie.
$page->setCookie( string $name, string $value[, int $day[, string $path = '/'[, string $domain = ''[, bool $secure = false[, bool $httpOnly = false]]]]]]);
// Set cookie
$page->setCookie('username', 'foo', 1);
Gets a cookie by name.
$page->getCookie( string $name);
// Get cookie
echo $page->getCookie('username');
Result:
foo
Deletes a cookie by name.
$page->deleteCookie( string $name);
// Delete cookie
$page->deleteCookie('username');