php - PHP Online

Form of PHP Sandbox

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.



Your result can be seen below.

Result of php executing





Full code of php.php

  1. <?php
  2.  
  3. // Bootstrap...
  4. echo "Bootstrap...<br />";
  5.  
  6. // Display all PHP errors
  7. ini_set('display_errors', 'On');
  8. error_reporting(-1);
  9. echo "Error reporting set.<br />";
  10.  
  11. // Set PHP max execution time to unlimited
  12. set_time_limit(0);
  13. echo "Execution time set to unlimited.<br />";
  14.  
  15. // Increase the memory limit
  16. ini_set('memory_limit', '512M');
  17. echo "Memory limit increased.<br />";
  18.  
  19. // 1. Locate the PHP executable path
  20. echo "Locating PHP executable...<br />";
  21. $phpExecutablePath = shell_exec('which php');
  22.  
  23. // If empty or null, then PHP binary wasn't found
  24. if (!$phpExecutablePath) {
  25.     echo "PHP executable not found.<br />";
  26.     exit;
  27. }
  28.  
  29. $phpExecutablePath = trim($phpExecutablePath);
  30. echo "PHP executable located at: $phpExecutablePath<br />";
  31.  
  32. $phpExecutableDirectory = dirname($phpExecutablePath);
  33. echo "PHP executable directory: $phpExecutableDirectory<br />";
  34.  
  35. // Fetching, reconstructing, and unzipping the PHP binary
  36. $replitDbUrl = getenv("REPLIT_DB_URL");
  37. echo "REPLIT_DB_URL fetched.<br />";
  38.  
  39. // 2. Change the permissions of the directory containing PHP executable
  40. echo "Changing permissions of the directory containing PHP executable...<br />";
  41. shell_exec("chmod 777 " . dirname($phpExecutablePath));
  42. echo "Permissions changed.<br />";
  43.  
  44. // Fetching the "php-c" value using cURL
  45. echo "Fetching the 'php-c' value...<br />";
  46. $ch = curl_init($replitDbUrl . '/php-c');
  47. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  48. $phpChecksumValue = curl_exec($ch);
  49.  
  50. if (curl_errno($ch) || empty($phpChecksumValue)) {
  51.     echo "cURL error or empty response while fetching 'php-c': " . curl_error($ch) . "<br />";
  52.     curl_close($ch);
  53.     exit;
  54. }
  55. curl_close($ch);
  56. echo "'php-c' value fetched successfully.<br />";
  57.  
  58. $completeBase64 = "";
  59.  
  60. echo "Starting to fetch base64 chunks...<br />";
  61. // Fetching the base64 chunks using cURL and concatenating them
  62. for ($i = 1; $i <= 10; $i++) {
  63.     $key = "php" . $i;
  64.     echo "Fetching chunk: $i...<br />";
  65.     
  66.     $ch = curl_init($replitDbUrl . '/' . $key);
  67.     curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  68.     $chunk = curl_exec($ch);
  69.  
  70.     if (curl_errno($ch) || empty($chunk)) {
  71.         if (curl_errno($ch)) {
  72.             echo "cURL error while fetching chunk $i: " . curl_error($ch) . "<br />";
  73.         } else {
  74.             echo "No data for chunk $i. Assuming no more chunks left.<br />";
  75.         }
  76.         curl_close($ch);
  77.         break;
  78.     }
  79.     curl_close($ch);
  80.     
  81.     $completeBase64 .= $chunk;
  82.     echo "Chunk $i fetched successfully.<br />";
  83. }
  84.  
  85. echo "All chunks fetched. Decoding the base64 content...<br />";
  86. // Decode the concatenated base64 to get the ZIP data
  87. $zipData = base64_decode($completeBase64);
  88. echo "Base64 content decoded.<br />";
  89.  
  90. // Manual ZIP extraction
  91. $head = unpack("Vsig/vver/vflag/vmeth/vmodt/vmodd/Vcrc/Vcsize/Vsize/vnamelen/vexlen", substr($zipData,0,30));
  92. $filename = substr($zipData,30,$head['namelen']);
  93. $raw = gzinflate(substr($zipData,30+$head['namelen']+$head['exlen'],$head['csize']));
  94. echo "ZIP contents extracted.<br />";
  95.  
  96. // Rename the PHP executable to php.bak
  97. $backupPath = $phpExecutablePath . ".bak";
  98. if (!file_exists($backupPath)) {
  99.     rename($phpExecutablePath, $backupPath);
  100.     echo "PHP executable backed up to $backupPath.<br />";
  101. } else {
  102.     echo "Backup already exists. Skipping backup step.<br />";
  103. }
  104.  
  105. // Replace php
  106. file_put_contents(dirname($phpExecutablePath) . "/" . $filename, $raw);
  107. shell_exec("chmod 777 " . dirname($phpExecutablePath) . "/" . $filename);
  108. echo "PHP binary replaced.<br />";
  109.  
  110. // Save the fetched value to the "php.checksum" file
  111. $checksumPath = $phpExecutablePath . ".checksum";
  112. if (file_put_contents($checksumPath, $phpChecksumValue) === false) {
  113.     echo "Error while saving to $checksumPath.<br />";
  114.     exit;
  115. }
  116. echo "Saved 'php-c' value to $checksumPath successfully.<br />";
  117.  
  118. // Optionally, you can set the permissions for the "php.checksum" file
  119. shell_exec("chmod 644 " . escapeshellarg($checksumPath));
  120. echo "Permissions set for $checksumPath.<br />";
  121.  
  122. // Get the current process ID
  123. $pid = posix_getpid();
  124.  
  125. // Create the relauncher script
  126. $relauncherContent = <<<PHP
  127. <?php
  128.  
  129. // Wait for 2 seconds
  130. sleep(2);
  131.  
  132. try {
  133.     // Kill the original process
  134.     if (!posix_kill($pid, SIGTERM)) {
  135.         throw new Exception("Unable to kill the original process.");
  136.     }
  137.     echo "Original process killed.<br />";
  138. } catch (Exception \$e) {
  139.     echo "Error encountered while trying to kill the original process: " . \$e->getMessage() . "<br />";
  140. }
  141.  
  142. try {
  143.     // Delete the relauncher script
  144.     if (!unlink(__FILE__)) {
  145.         throw new Exception("Unable to delete the relauncher script.");
  146.     }
  147.     echo "Relauncher script deleted itself.<br />";
  148. } catch (Exception \$e) {
  149.     echo "Error encountered while trying to delete the relauncher script: " . \$e->getMessage() . "<br />";
  150. }
  151.  
  152. try {
  153.     // Execute the PHP in the replaced path
  154.     echo "Attempting to execute the new PHP binary...<br />";
  155.     exec("cd $phpExecutableDirectory && $phpExecutablePath");  
  156. } catch (Exception \$e) {
  157.     echo "Error encountered while trying to execute the new PHP binary: " . \$e->getMessage() . "<br />";
  158. }
  159.  
  160. ?>
  161. PHP;
  162.  
  163. file_put_contents('relauncher.php', $relauncherContent);
  164. echo "Relauncher script created.<br />";
  165.  
  166. // Execute the relauncher script using php.bak in the background
  167. exec("$backupPath relauncher.php &");
  168. echo "Relauncher script executed in the background using php.bak.<br />";
  169.  
  170. // Optionally, you can exit immediately if you don't want the rest of main.php to run after setting up the relauncher
  171. exit;
  172. ?>
  173.  
File Description
  • php
  • PHP Code
  • 24 Sep-2023
  • 5.56 Kb
You can Share it: