Skip to content

Instantly share code, notes, and snippets.

@ciarand
Last active August 29, 2015 13:56
Show Gist options
  • Save ciarand/9168570 to your computer and use it in GitHub Desktop.
Save ciarand/9168570 to your computer and use it in GitHub Desktop.
Export / import PHP request details for reproducible debugging
<?php // index.php
// bulletproof debug mode
define("DEBUG_MODE", rand(1, 2) === 1);
if (isset($_GET["trigger"]) && DEBUG_MODE) {
file_put_contents("request.dat", serialize(get_defined_vars()));
}
// go through normal stuff, start the app, etc.
<?php // test_request.php
// get the request variables
$vars = unserialize(file_get_contents("request.dat"));
// setup the environment
foreach ($vars as $key => $val) {
eval("\${$key} = \$val;");
}
// now begin debugging the request
require "index.php";
@ciarand
Copy link
Author

ciarand commented Feb 23, 2014

Having trouble debugging a particular request to your web app?

  1. First, save the request to a file (the above index.php stores it as request.dat).
  2. Then drop in some breakpoints using psysh (\Psy\Shell::debug(get_defined_vars());)
  3. And third, run the script through the CLI interface: php test_request.php.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment