Debugging $_REQUEST

This snippet displays a nice list of all submitted data in a transparent box on the top left. Put this snippet preferable directly after <body>.

The box has some basic styling applied so that it will display a dark fixed box on the top left of the document that will automaticly show a scroll-bar if it becomes too long.

<div style="position:fixed; top:0; left: 0; width: 400px; background: rgb(0,0,0,0); background: rgba(0,0,0,0.8); color: green; margin:0px; padding:5px; max-height: 90%; overflow-y:auto;">
<h2 style="margin:0px;color:white;">$ HEADERS:</h2>
<h3 style="margin:5px;color:white;">GET</h3>
<?php

//var_dump($_GET);
foreach($_GET as $name=>$value) {
       echo $name."  =>  ";
       echo $value."<br />";
}

?>
<h3 style="margin:5px;color:white;">POST</h3>
<?php

//var_dump($_POST);
foreach($_POST as $name=>$value) {
       echo $name."  =>  ";
       echo $value."<br />";
}

?></div>

Comments

  1. User Avatar
    Niels Lange
    Permalink to comment#

    Nice example. You can also use $_REQUEST to debug $_GET and $_POST variables on the same time. Furthermore I often use the following code to debug variables:

    <?php
    
    echo '<pre>';
    print_r($_REQUEST);
    echo '</pre>';
    
    ?>

    These three lines of code above are doing exactly the same than the code snippet using a foreach loop.

  2. User Avatar
    Karl Groves
    Permalink to comment#

    I find var_dump (which you commented out in your examples) to be much more helpful than looping through the array.

    Specifically what I usually do is:

    echo '<pre>';
    var_dump($foo);
    echo '</pre>';

    Doing so gives more valuable information about the item you’re dumping, such as what type the item is, etc.

    print_r, as another commenter has mentioned, also supplies such information.

  3. User Avatar
    Marc
    Permalink to comment#

    1) Install xdebug
    2) Activate extension
    3) Open php.ini file and set “html_errors” to “On”

    After that, you’re able to use var_dump with a full html output like this:

    Show debug image.

Posting Code

You may write comments in Markdown. This makes code easy to post, as you can write inline code like `<div>this</div>` or multiline blocks of code in triple backtick fences (```) with double new lines before and after.

Code of Conduct

Absolutely anyone is welcome to submit a comment here. But not all comments will be posted. Think of it like writing a letter to the editor. All submitted comments will be read, but not all published. Published comments will be on-topic, helpful, and further the discussion or debate.

Want to tell us something privately?

Feel free to use our contact form. That's a great place to let us know about typos or anything off-topic.

Submit a Comment

icon-closeicon-emailicon-linkicon-menuicon-searchicon-tag