Skip to main content
Version: Current

The HTTP Facade

The http class provides access to information about the incoming request (client, server, and request data) as well as helpers for shaping the response (cookies, redirects, headers, and status codes).

Retrieving User Information

IP Address

http::$client["ip"];

Port

http::$client["port"];

User Agent

http::$client["agent"];

Retrieving Server Information

IP Address

Returns the server's IP address:

http::$server["ip"];

Port

Returns the port used to receive the request:

http::$server["port"];

Web Server Type

Shows the name of the web server on the server that received the request:

http::$server["webserver"];

Domain Name

Shows the main host domain name configured in the web server:

http::$server["hostname"];

Retrieving Request Information

Request Method

Displays whether the request is POST or GET:

http::$request["method"];

Request Path

Shows the path of the page requested by the browser and the user:

http::$request["uri"];

Request Receipt Time

Shows the time at which the request was received:

http::$request["time"];

Or you can use:

http::$request["microtime"];

The time output is in timestamp format.

Domain Name

For example, if the domain domain.com is sent to the server by a request, the value below will be domain.com.

http::$request["hostname"];

Connection Type

Shows the connection type, http or https:

http::$request["scheme"];

Referer Address

If a referer address is present, you can retrieve it as follows:

http::$request["referer"];

Checking Whether the Request Is Ajax

If the request was made via ajax, the value below is true; otherwise, it is false.

http::$request["ajax"];

POST Parameters

To retrieve the parameters sent via a form, you can proceed as follows:

http::$request["post"];

Note that the received values have not been validated.

info

For more information, refer to the Validation page.

Retrieving GET Parameters

To retrieve the parameters sent via the address (URL), you can proceed as follows:

http::$request["get"];

Example

http://domain.com/?lang=en

controllers/Main.php
<?php
namespace packages\packagename\controllers;

use packages\base\{controller, response, http, view};
use packages\packagename\views;

class Main extends controller {

public function homepage(): response {
$response = new response(true);
$view = view::byName(views\homepage::class);
$lang = $this->getLang();
$view->setLang($lang);
$response->setView($view);
return $response;
}

public function getLang() {
return isset(http::$request["get"]["lang"]) ? http::$request["get"]["lang"] : "en";
}
}

Note that the received values have not been validated.

info

For more information, refer to the Validation page.

Retrieving the URL

Using the getURL method of the http class, you can retrieve the full address that the user requested.

http::getURL();

Retrieving Parameters

Using the getData method of the http class, you can retrieve the value of a parameter that was sent either via the address or via a form. This method first searches among the parameters sent through the GET address; if it does not find a value with the specified key, it then looks in the parameters received via the POST form. It returns the first value found for the given key, or null if none is found. Note that this method does not validate the sent parameters and returns the values exactly as received.

info

For more information, refer to the Validation page.

http::getData(key);

Example

controllers/Main.php
<?php
namespace packages\packagename\controllers;

use packages\base\{controller, response, http, view};
use packages\packagename\{views, state};

class Main extends controller {

public function getCities(): response {
$response = new response(true);
$city = new state\city();
$city->where("state", http::getData("state"));
$city->orderBy("title_fa", "ASC");
$response->setData($city->get(), "cities");
return $response;
}
}

Retrieving Parameters Sent via the POST Form

info

For more information, refer to the Validation page.

If you want to retrieve the value of a key only among the parameters sent via a form, you can use the getFormData method of the http class. This method returns the first value found for the specified key, or null otherwise.

http::getFormData(key);

Retrieving Parameters Sent via the GET Address

info

For more information, refer to the Validation page.

If you want to retrieve the value of a key only among the parameters sent via the address, you can use the getURIData method of the http class. This method searches for the key it receives only among the parameters within the address; if it finds a value for this key, it returns that value, otherwise it returns null.

http::getURIData(key);

Checking the Request Method

Using the is_post method of the http class, you can determine whether the request sent by the user to the controller is a POST form or a GET request. In this way, you can provide appropriate responses for each request type without conflict. The output of this method is true if the request is of type POST, and false otherwise.

http::is_post();

Example

controllers/Main.php
<?php
namespace packages\packagename\controllers;

use packages\base\{controller, response, http};
use packages\packagename\views;

class Main extends controller {

public function accept_terms(): response {
$response = new response();
$view = view::byName(views\index::class);
$response->setStatus(true);
if (http::is_post()) {
// meaning the user accept terms
$response->Go(base\url("panel"));
}
return $response;
}
}

Using the setcookie method of the http class, you can store a cookie in the user's browser. This method receives, in its first parameter, a name/key; in its second parameter, a value (for the key); in its third parameter, the cookie's expiration duration; in its fourth parameter, the path on which the cookie is active (if "/" is set, the cookie is active on the entire main site); and in its fifth parameter, the domain name. In addition, in the sixth parameter you specify that this cookie should only be active over a secure https connection, and in the seventh and final parameter, if you set this value to true, this cookie will not be available to Javascript.

http::setcookie(name, value, expire, path, domain, secure, httponly);

Example

controllers/Main.php
<?php
namespace packages\packagename\controllers;

use packages\base\{controller, response, http, view};
use packages\packagename\user;

class Main extends controller {

public function login(): response {
$response = new response();
$view = view::byName(views\login::class);
$inputRules = array(
"username" => array(
"type" => "email",
),
"password" => array(),
"remember_me" => array(
"type" => "bool",
"optional" => true,
"default" => false,
)
);
try {
$response->setStatus(false);
$inputs = $this->checkinputs($inputRules);
$user = new user();
$user->where("email", $inputs["username"]);
$user->where("password", md5($inputs["password"]));
if (!$user = $user->getOne()) {
throw new inputValidation("username");
}
if ($inputs["remember_me"]) {
$token = md5(rand(999, 9999) + date::time());
http::setcookie("remember", $token, date::time() + 31536000);
$user->remember_token = $token;
$user->save();
}
$response->setStatus(true);
$response->Go(base\url("panel"));
} catch(inputValidation $error) {
$view->setFormError(FormError::fromException($error));
}
$response->setView($view);
return $response;
}
}

Although cookies are deleted after the expiration duration set at the time of creation has passed, you can also manually delete a cookie using the removeCookie method of the http class. This method receives the cookie's name/key in its parameter.

http::removeCookie(name);

Example

controllers/Main.php
<?php
namespace packages\packagename\controllers;

use packages\base\{controller, response, http};

class Main extends controller {

public function logout(): response {
$response = new response();
$user = $this->getUser(); // return the logged user ;
$user->remember_token = null;
$user->save();
$this->setUser(null); // empty logged user;
if ($cookies = http::$request["cookies"]) {
if (isset($cookies["remember"]) and $cookies["remember"]) {
http::removeCookie("remember");
}
}
$response->Go(base\url("login"));
return $response;
}
}

Redirecting

Using the redirect method of the http class, you can move the browser to another address. This method receives an address in its parameter.

http::redirect(url);

Setting a Header

Using the setHeader method of the http class, you can add any key and value to the response header of the browser's request. This method receives a name in its first parameter and a value in its second parameter.

http::setHeader(name, value);

Setting the Status Code

Using the setHttpCode method of the http class, you can set the status code of the request's response. This method receives a number (which must be one of the HTTP status codes) in its parameter; it interprets this code and applies it to the response header of the browser's request.

http::setHttpCode(code);

Setting the Content Type

Using the setMimeType method, you can set the type of the response. This method receives the type in its first parameter and the charset type in its second parameter.

http::setMimeType(name, charset);

Setting the Content Length

The parameter of the setLength method receives a number and applies this number to the response header of the request.

http::setLength(length);

JSON Output

By calling the tojson method of the http class, the browser will recognize that the response's content type is JSON. This method receives the charset type in its parameter, whose default value is set to utf-8.

http::tojson(charset);
http::tojson();

Validating the Referer Address

Using the is_safe_referer method of the http class, you can validate the address from which the request was referred (redirected). If the referer address and the main domain address are the same, the output of this method is true. In addition, if you have configured a setting for the accepted addresses, it searches among these addresses, and if it matches one of them, the output is true; otherwise, the output is false. This method receives the referer address in its parameter; however, if no address is passed to it, it automatically checks the referer address in the request header.

http::is_safe_referer();
http::is_safe_referer(referer);