How effective is PHP file encryption in preventing unauthorized access to the code?

PHP file encryption can be effective in preventing unauthorized access to the code by encrypting the PHP files containing sensitive information or proprietary code. This encryption can help protect the code from being easily read or modified by unauthorized users. One way to implement PHP file encryption is by using the OpenSSL extension in PHP to encrypt and decrypt files.

// Encrypt PHP file using OpenSSL
$plaintext = file_get_contents('sensitive_file.php');
$password = 'secret_password';
$cipher = 'aes-256-cbc';
$iv = openssl_random_pseudo_bytes(openssl_cipher_iv_length($cipher));
$encrypted = openssl_encrypt($plaintext, $cipher, $password, 0, $iv);
file_put_contents('encrypted_file.php', $iv . $encrypted);

// Decrypt PHP file using OpenSSL
$encrypted_data = file_get_contents('encrypted_file.php');
$iv_length = openssl_cipher_iv_length($cipher);
$iv = substr($encrypted_data, 0, $iv_length);
$encrypted = substr($encrypted_data, $iv_length);
$decrypted = openssl_decrypt($encrypted, $cipher, $password, 0, $iv);
file_put_contents('decrypted_file.php', $decrypted);