Learn from your fellow PHP developers with our PHP blogs, or help share the knowledge you've gained by writing your own.
prometheus/client_php
and php-prometheus/client
./metrics
) where Prometheus can scrape the metrics using the Prometheus exposition format.prometheus.yml
) to include the target endpoint and define any additional scraping parameters.prometheus/client_php
library:require 'vendor/autoload.php';
use Prometheus\CollectorRegistry;
use Prometheus\Storage\APC;
use Prometheus\RenderTextFormat;
$registry = new CollectorRegistry(new APC());
$requestDuration = $registry->registerCounter('php_requests_total', 'Total number of PHP requests');
$requestDuration->inc();
$renderer = new RenderTextFormat();
echo $renderer->render($registry->getMetricFamilySamples());
php_requests_total
) to track the total number of PHP requests. We then increment this metric for each request and expose the metrics endpoint using the Prometheus exposition format.
class User {
public function getUserById($userId) {
}
public function updateUser($userId, $userData) {
}
}
<!DOCTYPE html>
<html>
<head> <title>User Profile</title>
</head>
<body> <h1>Welcome, <?php echo $user['username']; ?>!</h1> <p>Email: <?php echo $user['email']; ?></p>
</body>
</html>
class UserController {
public function profile($userId) {
$userModel = new User();
$userData = $userModel->getUserById($userId);
include 'views/profile.php';
}
}
class CreateProductCommand {
public $name;
public $price;
}
class GetProductQuery {
public $productId;
}
class CreateProductCommandHandler {
public function handle(CreateProductCommand $command) {
}
}
class GetProductQueryHandler {
public function handle(GetProductQuery $query) {
}
}
class Product {
public $name;
public $price;
}
class ProductView {
public $name;
public $price;
}
$command = new CreateProductCommand();
$command->name = "Example Product";
$command->price = 99.99;
$handler = new CreateProductCommandHandler();
$handler->handle($command);
$query = new GetProductQuery();
$query->productId = 123;
$handler = new GetProductQueryHandler();
$product = $handler->handle($query);
<?php
$encryptionKey = openssl_random_pseudo_bytes(32);
$plaintext = "Sensitive data to encrypt";
$ciphertext = openssl_encrypt($plaintext, 'aes-256-cbc', $encryptionKey, 0, $iv);
$decryptedText = openssl_decrypt($ciphertext, 'aes-256-cbc', $encryptionKey, 0, $iv);
echo $decryptedText;
?>
<?php
$config = array(
"digest_alg" => "sha512",
"private_key_bits" => 4096,
"private_key_type" => OPENSSL_KEYTYPE_RSA,
);
$keyPair = openssl_pkey_new($config);
openssl_pkey_export($keyPair, $privateKey);
$publicKey = openssl_pkey_get_details($keyPair)["key"];
$plaintext = "Confidential message";
openssl_public_encrypt($plaintext, $encrypted, $publicKey);
openssl_private_decrypt($encrypted, $decrypted, $privateKey);
echo $decrypted;
?>
sudo apt-get install mariadb-server
.CREATE DATABASE my_database;
USE my_database;
CREATE TABLE users (
id INT AUTO_INCREMENT PRIMARY KEY,
username VARCHAR(50) NOT NULL,
email VARCHAR(100) NOT NULL
);
my_database
and a table named users
with columns for id
, username
, and email
.INSERT INTO users (username, email) VALUES ('john_doe', '[email protected]');
SELECT * FROM users;
UPDATE users SET email = '[email protected]' WHERE username = 'john_doe';
DELETE FROM users WHERE username = 'john_doe';