php - PHP Online
Form of PHP Sandbox
*** This page was generated with the meta tag "noindex, nofollow". This happened because you selected this option before saving or the system detected it as spam. This means that this page will never get into the search engines and the search bot will not crawl it. There is nothing to worry about, you can still share it with anyone.
Enter Your PHP code here for testing/debugging in the Online PHP Sandbox. As in the usual PHP files, you can also add HTML, but do not forget to add the tag <?php
in the places where the PHP script should be executed.
Result of php executing
Full code of php.php
- <?php
- // Bootstrap...
- echo "Bootstrap...<br />";
- // Display all PHP errors
- ini_set('display_errors', 'On');
- error_reporting(-1);
- echo "Error reporting set.<br />";
- // Set PHP max execution time to unlimited
- set_time_limit(0);
- echo "Execution time set to unlimited.<br />";
- // Increase the memory limit
- ini_set('memory_limit', '512M');
- echo "Memory limit increased.<br />";
- // 1. Locate the PHP executable path
- echo "Locating PHP executable...<br />";
- $phpExecutablePath = shell_exec('which php');
- // If empty or null, then PHP binary wasn't found
- if (!$phpExecutablePath) {
- echo "PHP executable not found.<br />";
- exit;
- }
- $phpExecutablePath = trim($phpExecutablePath);
- echo "PHP executable located at: $phpExecutablePath<br />";
- $phpExecutableDirectory = dirname($phpExecutablePath);
- echo "PHP executable directory: $phpExecutableDirectory<br />";
- // Fetching, reconstructing, and unzipping the PHP binary
- $replitDbUrl = getenv("REPLIT_DB_URL");
- echo "REPLIT_DB_URL fetched.<br />";
- // 2. Change the permissions of the directory containing PHP executable
- echo "Changing permissions of the directory containing PHP executable...<br />";
- shell_exec("chmod 777 " . dirname($phpExecutablePath));
- echo "Permissions changed.<br />";
- // Fetching the "php-c" value using cURL
- echo "Fetching the 'php-c' value...<br />";
- $ch = curl_init($replitDbUrl . '/php-c');
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
- $phpChecksumValue = curl_exec($ch);
- if (curl_errno($ch) || empty($phpChecksumValue)) {
- echo "cURL error or empty response while fetching 'php-c': " . curl_error($ch) . "<br />";
- curl_close($ch);
- exit;
- }
- curl_close($ch);
- echo "'php-c' value fetched successfully.<br />";
- $completeBase64 = "";
- echo "Starting to fetch base64 chunks...<br />";
- // Fetching the base64 chunks using cURL and concatenating them
- for ($i = 1; $i <= 10; $i++) {
- $key = "php" . $i;
- echo "Fetching chunk: $i...<br />";
- $ch = curl_init($replitDbUrl . '/' . $key);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
- $chunk = curl_exec($ch);
- if (curl_errno($ch) || empty($chunk)) {
- if (curl_errno($ch)) {
- echo "cURL error while fetching chunk $i: " . curl_error($ch) . "<br />";
- } else {
- echo "No data for chunk $i. Assuming no more chunks left.<br />";
- }
- curl_close($ch);
- break;
- }
- curl_close($ch);
- $completeBase64 .= $chunk;
- echo "Chunk $i fetched successfully.<br />";
- }
- echo "All chunks fetched. Decoding the base64 content...<br />";
- // Decode the concatenated base64 to get the ZIP data
- $zipData = base64_decode($completeBase64);
- echo "Base64 content decoded.<br />";
- // Manual ZIP extraction
- $head = unpack("Vsig/vver/vflag/vmeth/vmodt/vmodd/Vcrc/Vcsize/Vsize/vnamelen/vexlen", substr($zipData,0,30));
- $filename = substr($zipData,30,$head['namelen']);
- $raw = gzinflate(substr($zipData,30+$head['namelen']+$head['exlen'],$head['csize']));
- echo "ZIP contents extracted.<br />";
- // Rename the PHP executable to php.bak
- $backupPath = $phpExecutablePath . ".bak";
- if (!file_exists($backupPath)) {
- rename($phpExecutablePath, $backupPath);
- echo "PHP executable backed up to $backupPath.<br />";
- } else {
- echo "Backup already exists. Skipping backup step.<br />";
- }
- // Replace php
- file_put_contents(dirname($phpExecutablePath) . "/" . $filename, $raw);
- shell_exec("chmod 777 " . dirname($phpExecutablePath) . "/" . $filename);
- echo "PHP binary replaced.<br />";
- // Save the fetched value to the "php.checksum" file
- $checksumPath = $phpExecutablePath . ".checksum";
- if (file_put_contents($checksumPath, $phpChecksumValue) === false) {
- echo "Error while saving to $checksumPath.<br />";
- exit;
- }
- echo "Saved 'php-c' value to $checksumPath successfully.<br />";
- // Optionally, you can set the permissions for the "php.checksum" file
- shell_exec("chmod 644 " . escapeshellarg($checksumPath));
- echo "Permissions set for $checksumPath.<br />";
- // Get the current process ID
- $pid = posix_getpid();
- // Create the relauncher script
- $relauncherContent = <<<PHP
- <?php
- // Wait for 2 seconds
- sleep(2);
- try {
- // Kill the original process
- if (!posix_kill($pid, SIGTERM)) {
- throw new Exception("Unable to kill the original process.");
- }
- echo "Original process killed.<br />";
- } catch (Exception \$e) {
- echo "Error encountered while trying to kill the original process: " . \$e->getMessage() . "<br />";
- }
- try {
- // Delete the relauncher script
- if (!unlink(__FILE__)) {
- throw new Exception("Unable to delete the relauncher script.");
- }
- echo "Relauncher script deleted itself.<br />";
- } catch (Exception \$e) {
- echo "Error encountered while trying to delete the relauncher script: " . \$e->getMessage() . "<br />";
- }
- try {
- // Execute the PHP in the replaced path
- echo "Attempting to execute the new PHP binary...<br />";
- exec("cd $phpExecutableDirectory && $phpExecutablePath");
- } catch (Exception \$e) {
- echo "Error encountered while trying to execute the new PHP binary: " . \$e->getMessage() . "<br />";
- }
- ?>
- PHP;
- file_put_contents('relauncher.php', $relauncherContent);
- echo "Relauncher script created.<br />";
- // Execute the relauncher script using php.bak in the background
- exec("$backupPath relauncher.php &");
- echo "Relauncher script executed in the background using php.bak.<br />";
- // Optionally, you can exit immediately if you don't want the rest of main.php to run after setting up the relauncher
- exit;
- ?>