views:

120

answers:

5

I'm having a miserable time debugging one small function on my new project.

Essentially I'm having a user log out via an AJAX call to my log out script on my server called "userfFunctions.php" I'm using AJAX so that I don't have the headache of writing more regex to match my mod_rewrites. Anyway, every so often, it seems as though my Post data just flat out dies and since the PHP is running behind the scenes, I feel like I have no way of finding out where the data flow is being disrupted. BTW This function works 19 hrs of the day.

Here is the javascript function:

function logOut(){
    var data = new Object;
    data.log_out = true;
    $.ajax({
        type: 'POST',
        url: 'http://www.mydomain.com/User_Validator', //<-- redirects to userFunctions.php
        data: data,
        success: function(data) {
        alert(data); // <-- a response is triggered but with no response data!
        }
    });
}

the php side:

if(isset($_POST['log_out'])){
     echo 'alert this!';
}

here is my awesome response: alt text

+7  A: 

Try using something like the FireBug plugin for Firefox, or the Developer Tools in Chrome, to look at the request being sent out.

Amber
in addition to firePHP so you can debug the php code in same firebug window
Jacob
Firebug + FirePHP is awesome http://firephp.org/
Shiki
@Amber, I'm not exactly sure what to look for in the header request... anything in particular?
Jascha
Don't look at the headers - look to see if the POST variables are actually being passed properly, and if the returned result is correct. Then you can narrow down the issue to either one on the client side, or one on the server side.
Amber
@Amber, forgive my naivete, but where do I find those in the debugger?... found it! It wasn't showing up before, but now it is... I didn't change anything (grumble grumble)... thank you for your input.
Jascha
+1  A: 

Have you tried setting the dataTypeto "text"?

function logOut(){
    var data = {
        "log_out" : true
    };
    $.ajax({
        type: 'POST',
        url: 'http://www.mydomain.com/User_Validator',
        data: data,
        success: function(data) {
            alert(data);
        },
        dataType : 'text'
    });
}

Also, I would change your PHP to this:

print_r($_POST);
nickf
I have tried that, the truth is, I'm not really just looking for a text response, I'm just using the response as a way to debug, in the php function I actually null out cookies and session_destroy and then on success refresh the page with javascript.
Jascha
A: 

I noticed:

//<-- redirects to userFunctions.php

When you do a redirect ( header("Location: "); ) you'll lose your $_POST and $_GET data. (If you're referring to a url rewrite (with mod_rewrite) you should receive the data.)

But this doesn't explain the 19hr symptom.

Bob Fanger
i thank you for your response, it's actually a rewrite and not a redirect.
Jascha
A: 

You could try checking the $_POST array itself, with something like this:

var_dump($_POST);

See if the array is even being populated at all and then work from there. Using firebug, you can also confirm if the AJAX post is truly sending data (check the console or net tabs).

jeanreis
+1  A: 

FirePHP:

FirePHP enables you to log to your Firebug Console using a simple PHP method call.

All data is sent via response headers and will not interfere with the content on your page.

FirePHP is ideally suited for AJAX development where clean JSON and XML responses are required.

Here is a minimalistic implementation I wrote:

function FirePHP($message, $label = null, $type = 'LOG')
{
    static $i = 0;

    if (headers_sent() === false)
    {
        $type = (in_array($type, array('LOG', 'INFO', 'WARN', 'ERROR')) === false) ? 'LOG' : $type;

        if (($_SERVER['HTTP_HOST'] == 'localhost') && (strpos($_SERVER['HTTP_USER_AGENT'], 'FirePHP') !== false))
        {
            $message = json_encode(array(array('Type' => $type, 'Label' => $label), $message));

            if ($i == 0)
            {
                header('X-Wf-Protocol-1: http://meta.wildfirehq.org/Protocol/JsonStream/0.2');
                header('X-Wf-1-Plugin-1: http://meta.firephp.org/Wildfire/Plugin/FirePHP/Library-FirePHPCore/0.3');
                header('X-Wf-1-Structure-1: http://meta.firephp.org/Wildfire/Structure/FirePHP/FirebugConsole/0.1');
            }

            header('X-Wf-1-1-1-' . ++$i . ': ' . strlen($message) . '|' . $message . '|');
        }
    }
}

I wrote it so that it only works on localhost (for security reasons), but you can easily change that by replacing the following code:

if (($_SERVER['HTTP_HOST'] == 'localhost') && (strpos($_SERVER['HTTP_USER_AGENT'], 'FirePHP') !== false))

With:

if (strpos($_SERVER['HTTP_USER_AGENT'], 'FirePHP') !== false)
Alix Axel
Thank you so much for this! Great Approach.
Jascha
@Jascha: No problem, there was a typo in the function I provided - it's fixed now. =)
Alix Axel