How to put debug point - php

I've a php file calling another php file which sometimes calls another php file to execute some actions (all through ajax).
What I use to do was to echo at different points to know upto where the codes are executing properly. But with this approach, I can keep echo-ing.
So how do I know upto where my code is executing?? Is there a tool for Google Chrome browser to detect it??

In your web browser, click the wrench icon, then "Tools", then "Developer tools". You can debug and step through JavaScript, you can see a timeline of requests with the request and response headers and bodies fully inspectable, etc. You should be able to debug all your AJAX request without any additional software/plugins.

Firebug plugin for Firefox allows you to put actual debug breakpoints and step through the code - much easier to debug javascript than echo'ing

If you look on how to set a debugging environent with PHP, you'll find a lot of dead ends, trust me, I did. Therefore you've got 2 choices.
A: Keep echo-ing which will leave a trail of painful echo's in your code.
B: Use a logger. I've used for a while FirePHP, which comes as a FireBug extension and lets you send debugging messages to the browser, which is ideal for debugging Javascript based apps with heavy Ajax.
Hope I can help!

The technique you're using is called bullet tracing. There's no real way to track which code executes and when within an php file (like breakpoints) but you can pinpoint points of errors on lines of code as provided by the php debugger.
When AJAX requests are made, you can track the progress through XMLHttpRequest States. To check the headers and payload of the packets being sent, you could use Firebug in Firefox 3.6.x, Firefox 4 Beta's built-in developer console, or Google Chrome's developer console. (Packet sniffers also work)

Related

PHP and Javascript - log complete workflow

I am working in a complex Webframework with lots of Javascript in the Frontend and lots of PHP in the backend. Since I’m new to it, finding out the workflow is quite a hassle. Is there a way to log the complete sequence of function calls (in PHP, JS or both) from the moment a request is sent until the response is executed (or the JS after the request has been sent is executed?) That would be really helpfull.
There's no perfect solution here but you will probably have to just use browser debugging tools like Firefox's Firebug or Chrome's debugger . Using these tools you can see JavaScript errors, AJAX requests, PHP (server) responses, individual page file loads etc etc.
All I can think of would be using Chrome developer tools, network tab in order to see the sequence of events. Or maybe even better, the HTTPFox plugin in firefox.
That will help you to find out the execution order of JS calls and which PHP files are accessed via AJAX.
What's happening on server side isn't easy to follow, but you could debug your code using XDebug.

How to debug PHP script when it is being accessed by android application

I have an Android application that is connectiont to a web server running MySQL DB. On the web server I have php files that run scripts and return JSON Objects to the client (application). I recently upgraded the versions of my web server and as of then, the SELECT statements are no longer returning anything to the client if they have parameteres, however if I run a SELECT with no parameters, it works fine. Has anyone ever encountered a similar problem?
To be more clear about my question:
Is there anyway I can debugphp scripts?
I thought maybe debugging via a web browser wouldn't work because the client is triggering the php files directly and not a direct access to the web.
All help is much appreciated!
Chrome has a set of tools that let you remote debug from your android.
Check them out: https://developers.google.com/chrome-developer-tools/docs/remote-debugging
You could wirte your own error handler an let him wirting some debug informations to a own log-file. than you can read the log file and check it for errors
Look at the MySQL error log and the php error log on your server
Check you activated the correct php_mysql* extensions in the newly upgraded PHP
Run the scripts on the server through a browser with the same parameters that your app uses, watch for errors
Upgrade the PHP code running on the server so it captures any errors and does something OBVIOUS with them.
I would recommend to use XDEBUG for debugging PHP code, or any analogue for yours web server. It is really simple to set-up and use it with any PHP IDE (like Netbeans) and it gives you a lot of abilities to explore your application condition. There is no difference in what browser you will open link with ?XDEBUG_SESSION_START=netbeans-xdebug parameter.
Also you can watch MySQL and Web-server logs.
If you cannot do anything above and you need to watch network action (AJAX requests), you can set-up ADB and use chrome developer console for debugging js (or to see php var_dump() in ajax calls) in you android chrome browser.
If you need to debug js in native android browser - try to redirect to "about:debug" page, and after this you will be able to see browsers js console (in some cases this button appears only when console has something to show - error or any othe message, in some cases it just doesn't work).
Update Watch Duplicate Copy Move
As mentioned by others, you can debug using the error logs given by mysql and php. There are also IDEs which come with debugging tools for scripting languages like php.
With an IDE like phpStorm by JetBrains, which I highly recommend using over just a normal text editor, you can configure data sources like a connection to your MySQL db. This is useful because you can run scripts in the IDE and see whether it's the retreival of the data from your db or something in your scripts or handling of the JSON object back in the client. So if you configure a data source and run those same scripts and you get the correct data back then you know there is either something wrong in the code where you send the data or something wrong in the code where you receive it.
**p.s. I know that I am 5 years (oh my days its been 5 years since 2013) late to this post but none the less you never know who may be having this same issue and they stumble accross this answer
1st - Setup Xdebug for remote debugging with your IDE(Eclipse, PHPStrom, etc), and put breakpoints in your script.(for example here's link for setting up eclipse envirompment).
2-nd - When sending your url request to server add parametres to your url(http://your/url.php&XDEBUG_SESSION_START=ECLIPSE_DBGP&KEY=XXXXX,
-1st parameter - XDEBUG_SESSION_START=ECLIPSE_DBGP starts debugging session. (as for me I am using eclipse IDE for developing, so my key will be ECLIPSE_DBGP).
-2nd parameter - KEY=XXXXXX - is a session number(type any number here).
After that you'll get your code breaks at your debugging point in your script.

PHP debugging tools in browser

I'm developing a Flex application that make calls to PHP scripts. All work fine but sometimes and without any reason the app stops. It seems that something wrong in the call to the PHP script, bad response or simply there is no response and the app wait until such response.
My question is if anyone know how can I trace the response of a PHP script call within the browser. Is there any tool that can monitorize the call and response?
Thanks.
I would recommend using firephp with firebug both are FireFox Add-Ons.
Your PHP script would have to die and report what's broken. This report would go to the browser and you can do whatever you want with it.
Read up on the following functions:
http://php.net/manual/en/function.set-error-handler.php
http://php.net/manual/en/function.error-get-last.php
Or like Marc B said - XDebug will dump trace for you if have access to instalilng modules

How to debug Javascript + PHP + Web services

Disclaimer: May be a insane question but I have suffered a lot so came here.
I am working on a legacy application which uses JS + PHP + Web services (Written in spring).
Flow of the application :
Whenever any web service is called from JS it is redirected to one php file. The php file authenticates the user(using one web service) and then forwards the request to actual web service.
How can I debug this application ? I have debugged JS using Firebug and servr side code using Eclipse but never debugged such a application.
~Ajinkya.
I think there are a variety of things that need to be done, and I must say this question is sufficiently general as to not have a straight answer so I will do my best. As xdazz mentioned, var_dump (and die) are necessary from the PHP standpoint.
Whenever anything is returned to JS console.log it. In addition, ensure XHTTP requests are turned on for Firebug or alternatively view the output of each request in the Chrome Network tab.
With a combination of console.log, var_dump, and die, you can trace non-functioning parts of the application repeatedly step by step until you come across the bug.
Alternatively, and in the long run you ought to be doing this anyway, build error handling code into all the PHP code that is only activated when a debug flag is set to true. This way you can get detailed error messages and then when you deploy, you can turn them off to avoid compromising security.
If you are needing to inspect the entire lifecycle of a Web service request in your scenario you will need to combine a several techniques. Considering the fact that the scope of your scenario spans from client to server you will need to decide with what you will persist the information you need to inspect.
Personally, I would choose the path of least resistance which in my case would probably be cookies. With that being said you should be able chronologically log the necessary information via JavaScript and PHP, both before, during and after the request and even redirect has occurred.
This strategy would then allow for the information logged with cookies to then be dumped or analyzed via JavaScript, WebKit inspector or Firebug. Again, this is probably how I would handle such a scenario. Lastly, you can apply different storage strategies to this technique such as using a session or database for persistence.
Note: You can use something like WebKit Inspector, and possibly Firebug, to analyze data transmitted and received for GET, POST and even WebSocket requests.

How to debug AJAX requests? How to know which PHP function is invoked?

I'm debugging Ajax calls.
How do I know which specific PHP function is called by an ajax call ?
I can only see the request and the javascript code triggering the request, but I dunno how is handled on the request on server side ?
I'm currently inspecting all php code, but I was wondering if there is an easier way..
thanks
An Ajax call does not call a specific function, it merely executes a script. Look at the script, search for its "main" (Whatever name it has in your script) and you'll have the exectued function.
If you want a real debug environment and be able to set breakpoints in your php-code, refer to the zend debugger or xdebug. Both can be controlled with the pdt eclipse-plugin or other IDEs.
If you just want to see some small pieces of information, you can of course just add some echo's or var_dump's to your php-code and then use the firebug firefox extension to view the response to your ajax-requests, which contains the echoed strings.
I would recommend you make use of FireBug and FirePHP. FirePHP allows you to log from PHP to FireBug console. In this way you can log anything for example all the queries being run or log from methods/functions.
Just use firebug and after the request is sent, look for the response. e.g. function1 echo foo, function2 echo bar, if it's blank...none of those 2 function was called.
On my end, I'm using Safari and finding both request / response from an ajax call to a PHP script is a breeze.
Debugging using Firefox's addon FireBug, you can check out the request and response data content easily.
http://www.daniweb.com/web-development/php/code/216729
So by right, Ajax cannot access remote web server content, so you need the proxy page in the middle of the whole architecture flow.
So which means:
[Within Same Server]
PHP page using AJAX -> Web Services / Web Server that contains your response functions
[Within 2 Servers, Connecting to Remote Server]
PHP page using AJAX -> PHP Proxy Page -> Web Services / Web Server that contains your response functions
So the overall proxy page acting like a "middle-man" to connect to remote site.
[IMPORTANT]
Don't confuse router proxy and proxy page, but have the same theory but it is different thing.

Categories