1. Go to this page and download the library: Download perimeterx/php-sdk library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
perimeterx / php-sdk example snippets
use Perimeterx\Perimeterx;
$perimeterxConfig = [
'app_id' => 'APP_ID',
'cookie_key' => 'COOKIE_SECRET',
'auth_token' => 'AUTH_TOKEN',
'blocking_score' => 60,
'module_mode' => Perimeterx::$ACTIVE_MODE
];
/* Obtain PerimeterX SDK instance */
$px = Perimeterx::Instance($perimeterxConfig);
/* run verify at the beginning of a page request */
$px->pxVerify();
/**
* @param \Perimeterx\PerimeterxContext $pxCtx
*/
$perimeterxConfig['custom_block_handler'] = function ($pxCtx)
$block_score = $pxCtx->getScore();
$block_uuid = $pxCtx->getUuid();
$full_url = $pxCtx->getFullUrl();
// user defined logic goes here
};
$px = Perimeterx::Instance($perimeterxConfig);
$px->pxVerify();
/**
* @param \Perimeterx\PerimeterxContext $pxCtx
*/
$perimeterxConfig['custom_block_handler'] = function ($pxCtx) {
$block_score = $pxCtx->getScore();
$block_uuid = $pxCtx->getUuid();
$action = $pxCtx->getBlockAction();
/* user defined logic comes here */
error_log('px score for user is ' . $block_score);
error_log('px recommended action for user is ' . $action);
error_log('px page uuid is ' . $block_uuid);
switch ($action) {
case "block":
log("do block");
break;
case "captcha":
log("do captcha");
break;
default:
log("unknown action");
}
}
/**
* @param \Perimeterx\PerimeterxContext $pxCtx
*/
$perimeterxConfig['custom_user_ip'] = function ($pxCtx)
{
$headers = getallheaders();
/* using a socket ip */
$ip = $_SERVER['REMOTE_ADDR'];
/* using an ip from a x-forwarded-for header */
$xff = explode(",", $headers['X-Forwarded-For']);
$ip = $xff[count($xff)-1];
/* using an ip from a custom header */
$ip = $headers['X-REAL-CLIENT-IP'];
return $ip;
};
$perimeterxConfig = [
..
'ip_headers' => ['X-TRUE-IP', 'X-Forwarded-For']
..
]
$px = Perimeterx::Instance($perimeterxConfig);
$px->pxVerify();
/**
* @param array $customParamsArray
*/
$perimeterxConfig['enrich_custom_params'] = function ($customParamsArray)
{
// user defined logic comes here
};
$px = Perimeterx::Instance($perimeterxConfig);
$px->pxVerify();
$perimeterxConfig['px_compromised_credentials_header'] = 'px-comp-creds';
$perimeterxConfig['px_login_credentials_extraction_enabled'] = true;
$perimeterxConfig['px_login_credentials_extraction'] = [
[
"path" => "/login", // login path, automatically added to sensitive routes
"method" => "POST", // supported methods: POST
"sent_through" => "body", // supported sent_throughs: body, header, query-param
"pass_field" => "password", // name of the password field in the request
"user_field" => "username" // name of the username field in the request
], [ ... ], ...
]
$perimeterxConfig['px_enable_login_creds_extraction'] = true;
$perimeterxConfig['px_login_creds_extraction'] = [
[
"path" => "/login", // login path
"method" => "POST", // supported methods: POST
"callback_name" => "extractCreds" // name of custom extraction callback
], ...
];
function extractCreds() {
// custom implementation resulting in $username and $password
if (empty($username) || empty($password)) {
return null;
}
return [
"user" => $username,
"pass" => $password
];
}
$perimeterxConfig['px_login_successful_reporting_method'] = 'status';
$perimeterxConfig['px_login_successful_status'] = [200, 202]; // number or array of numbers
$perimeterxConfig['px_login_successful_reporting_method'] = 'custom';
// anonymous callback function
$perimeterxConfig['px_login_successful_custom_callback'] = function() {
// ...
return $isLoginSuccessful;
};
// name of defined function as string
$perimeterxConfig['px_login_successful_custom_callback'] = 'isLoginSuccessfulCallback';
function isLoginSuccessfulCallback() {
// ...
return $isLoginSuccessful;
}
// $px is an instance of the Perimeterx class
function handleLogin() {
// login flow resulting in boolean $isLoginSuccessful
$px->pxSendAdditionalS2SActivity($isLoginSuccessful ? 200 : 401, $isLoginSuccessful);
}