PHP: Profiling code and strict environment ~ Improving my coding - php

I would like to update my local working environment to be stricter in an effort to improve my code. I know that my code is okay, but as with most things there is always room for improvement.
I use XAMPP on my local machine, for simplicities sake Apache Friends XAMPP (Basic Package) version 1.7.2 So I've updated my php.ini : error_reporting to be E_ALL | E_STRICT to help with the code standard. I've also enabled the XDebug extension zend_extension = "C:\xampp\php\ext\php_xdebug.dll" which seems to be working, having tested some broken code and got the nice standard orange error notice.
However, having read this question, Profiling PHP code and enabled the profiler, I cannot seem to create a cachegrind file.
Many of the guides that I've looked at seem to think you need to install XDebug in XAMPP which leads me to think they are out of date, as XDebug is bundled with XAMPP these days.
So I would appreciate it if anyone can help point me in the right direction with both configuring XDebug to output grind files, and or just a great set of default settings for the XDebug config in XAMPP. Seems there is very little documentation to go on.
If people have tips on integrating these tools with Netbeans, that would be awesomesauce.
I'm happy to get suggestions on other things that I can do to help tighten up my php code, both syntactically and performance wise
Thanks, and apologies for the rambling question(s)!
Ninja edit
I should menion that I'm using named vhosts as my Apache configuration, which I think is why running XDebug on port 9000 isn't working for me. I guess I'd need to edit my vhost to include port 9000

If you always want to run the profiler:
xdebug.profiler_enable = 1
else if you want to selectively switch the profiler on (a good thing), use:
xdebug.profiler_enable_trigger = 1
If you're triggering, you'll need to using the XDEBUG_PROFILE GET/POST parameter, or send a cookie with the name XDEBUG_PROFILE. There are browser add-ons to help; For Firefox I use Xdebug Helper, but as it seems to have disappeared you could try easy Xdebug.
I found that the default output dir didn't work, so try setting it yourself:
xdebug.profiler_output_dir = /home/deebster/php/profiler
Remember to set the directory's permissions to allow Apache to create files if you don't use /tmp

Related

How do I get NetBeans to show visual code execution for URL with GET parameters?

How do I get NetBeans to show visual code execution for URL with GET parameters?
In NetBeans I am using Xdebug with a remote server. That is working fine.
The problem is Xdebug with NetBeans only works when debugging files that don't contain GET parameters.
Basically if my file is called index.php, and I go to website.com/index.php?XDEBUG_SESSION_START=netbeans-debug It works.
If I go to website.com/index.php?test=1&XDEBUG_SESSION_START=netbeans-debug or website.com/index.php?XDEBUG_SESSION_START=netbeans-debug&test=1 it runs Xdebug, but I can't step through the code visually with NetBeans.
It seems NetBeans doesn't realize that index.php with GET parameters and no GET parameters is a the same file, and thus doesn't show me visually as I step through the code with the debug buttons.
How do I fix this?
Using:
Apache NetBeans 12.6
PHP 7.2
Xdebug 2.8.0
Windows 10
PHP.INI settings for Xdebug (I am using SSH tunnel hence why my IP is 127.0.0.1):
zend_extension=xdebug.so
xdebug.remote_autostart=on
xdebug.remote_enable=on
xdebug.remote_handler=dbgp
xdebug.remote_host=127.0.0.1
xdebug.remote_port=9000
xdebug.remote_mode=req
xdebug.idekey=netbeans-debug
xdebug.mode=develop,coverage,debug,gcstats,profile,trace
xdebug.remote_log="xdebug_remote_log.txt"
Thanks!
P.S: I can't use VSCode, or PHPStorm as I am using Namecheap shared hosting, so both those tools don't work.
Apache Netbeans 12.6, PHP 7.2, Xdebug 2.8.0, Windows 10
You need to upgrade, Xdebug 2 and PHP 7.2, are no longer supported.
Your settings also don't make sense, as you mix Xdebug 3 settings (xdebug.mode) with Xdebug 2 settings (the others).
To get back to your question: using GET/POST parameters to start a debugging session is pretty much deprecated. Instead, you can use a browser extension to trigger debugging requests, while your IDE listens for incoming debugging requests. Unfortunately, this is not something that Netbeans supports. Please don't use Netbeans for debugging, as it is so far behind other IDEs such as VS Code and PhpStorm.

NetBeans + XDebug Breakpoints not working

I know there are countless of questions regarding this subject, however no matter what I've tried I've had no luck whatsoever. The problem I'm encountering is regarding breakpoints when using NetBeans and XDebug (on Windows 8.1 if that affects anything).
The issue is that no breakpoints are working. By default when debugging with NetBeans, it stops on the first line and this does successfully happen. Therefore NetBeans is somehow connecting to XDebug. However, any other breakpoints in any other files are not working.
I've tried various different settings in the php.ini file but no luck so far. I've also tried switching on the XDebug log file so that maybe I can analyse what's going on, but this also didn't work. Finally I've tried setting breakpoints using xdebug_break() which also didn't work.
XDebug settings in php.ini
;[XDebug]
; Only Zend OR (!) XDebug
zend_extension="C:\xampp\php\ext\php_xdebug.dll"
xdebug.remote_enable=true
xdebug.remote_host=127.0.0.1
xdebug.remote_port=9000
xdebug.remote_handler=dbgp
xdebug.profiler_enable=1
xdebug.profiler_output_dir="C:\xampp\tmp"
Project Run Configuration on NetBeans
I've already looked at the following:
http://articlebin.michaelmilette.com/making-xdebug-work-with-netbeans-on-windows/
Xdebug ignores breakpoints
http://wiki.netbeans.org/HowToConfigureXDebug#Tips_from_the_NetBeans_Forums
Also note that this was working 3 days ago, however today no breakpoints are working.
Additional Info
I've run the following command in cmd php -v and the following warning was shown:
Warning: Module 'xdebug' already loaded in Unknown on line 0
Does this mean that xdebug is trying to be loaded twice? If so I'm not sure how this can be solved
Figured out what the problem was. I know that this answer will be very specific, however it might help someone in the future.
So while inspecting the code I noticed that someone added code that cleared all cookies when the login page loaded. Once the cookies are removed XDebug didn't work. The reason is that for XDebug to work it sets a cookie with the session id for example 'netbeans-xdebug' which is sent with every request on the website.
It's a very simple issue but it took nearly 2 days to find out what was going on. Once again I know that this is a very specific case but maybe it might help someone out there.
Happy coding!

How to set up remote debugging with phpstorm and Xdebug

I've seen other questions/answer about this topic but none of them seem to have the same issue I have, so here we go:
What I'm trying
I'm using phpStorm 8 to develop PHP websites (CakePHP 2.5.1 in this specific case). I have a copy of the website on my computer, make whatever changes there and upload the new version to the production server via the integrated FTP tool. So far all pretty simple, no issues at all.
Now I would like to start using Xdebug to debug the websites using the production server (PHP 5.3.28), so I'm trying to set up remote debugging with phpStorm and Xdebug.
What I have done so far
I have installed Xdebug 2.1.3 on the production server, and it seems to be working. To test that I've done whats recommended in this other SO question, and all those things work.
This is how the config in php.ini looks like:
zend_extension="/usr/local/src/xdebug-2.1.0/modules/xdebug.so"
xdebug.profiler_enable='0'
xdebug.profiler_enable_trigger='1'
xdebug.profiler_output_dir='/home/username/debug'
xdebug.remote_enable='1'
xdebug.remote_connect_back ='1'
I'm not setting the remote_port variable because I'm fine with the default port (9000). Also, I'm not setting the remote_host IP because I'm using the remote_connect_back option to allow multiple IPs, as explained here.
I've also tried 2 different approaches to set all this up:
I followed this Zero Configuration tutorial, but at step 7 I never get the Incoming Connection dialog.
I also followed this different tutorial but in the Integrating XDebug with PhpStorm step I don't have the choose XDebug from the Debugger drop-down list option on step 3
What I need
If someone could help me figure out what I'm missing or doing wrong that would be great!
I would have added this to the comments, but don't have the needed rep.
Have you set the preferences correctly in your project? Were you able to configure and validate your deployment server (under Deployment)?
After that, set up the server under PHP > Server and validate it as well.
Don't forget to check the firewall on your host.
Make sure that you can get XDebug working without PHPStorm, then circle back around and integrate it.
These are the php.ini settings, other than the driver path, that I am using for my CLI project:
xdebug.remote_enable = 1
xdebug.remote_connect_back = 1
xdebug.remote_autostart = 1
xdebug.remote_host = 192.168.100.1
Most importantly, listen to LazyOne. Specify your remote host. And don't run debuggers on your production gear. Spend some time learning about Virtual Machines. My recommendation is to check out VirtualBox, Vagrant, and SaltStack. Used together, these tools will allow you to debug your code in an environment that is as close to production as possible without adding the burdens and risks involved with your debugging tools.

Eclipse does not run Xdebug or Zend debugger on Mac OSX 10.8.2?

I was able to successfully install both Zend as well as Xdebugger. Verified by Phpinfo as well as Eclipse verifies the Zend debug successfully installed. Although whenever I run debug from eclipse for
Xdebug it hangs at 57%`
and for Zend debugger
the browser keeps processing/busy but nothing happens
Unsure what is causing this
-> Firewall issues?
-> No Selinux in Mac so not sure if it is a permissions issue
Any clues would be helpful.
Thank you.
it seems like a miss configuration of debug. I will try to help you showing my working configurations.
This is my php.ini piece for xdebug.
zend_extension="/usr/lib/php/extensions/no-debug-non-zts-20090626/xdebug.so"
xdebug.remote_enable=On
xdebug.remote_host=127.0.0.1
xdebug.remote_port=9060
xdebug.remote_handler="dbgp"
xdebug.remote_autostart=On
It is important to note about the parameter debug.remote_host. If your client side is not allowed here, you will stuck on 57%. The value here is the IP of the machine where you are running eclipse. If you are running apache/php and eclipse in the same machine the localhost IP should work.
Adapt zend_extension parameter to your world.
Other reason you can stop in 57% is the web site is not running. Apache/PHP only will send xdebug metadata if the site is running. If you got an internal error, for example, you will be stuck on 57% also. Eclipse stops on 57% waiting for a xdebug session. You must be sure apache/php started a debug session.
Make sure the Debug configuration on eclipse is using the correct PHP server. If you are trying to run eclipse in an invalid PHP server or a PHP server which is not well configured you will stop on 57% also. So review the PHP server parameters on Debug Configurations.
Other important review you have to made is about the Xdebug configuration for eclipse. The port Debug port field must be filled with the same value of parameter xdebug.remote_port. If they are different you will be stuck in 57% also.
Please, let me know if it was helpful.
For Xdebug, this is likely a misconfigured path mapping in Eclipse. There are 10s of questions related to that here on stackoverflow and 100s of hits on google. Please check there first.

Breakpoints not working in Eclipse with XDebug

I have been trying to set up debugging using XDebug in Eclipse for the last few hours without success.
I have the following in my etc/php5/apache2/php.ini file
[XDebug]
xdebug.remote_enable=on
xdebug.remote_host="localhost"
xdebug.remote_port=9000
xdebug.remote_handler=dbgp
xdebug.remote_autostart=off
zend_extension = /usr/lib/php5/20090626+lfs/xdebug.so
The zend_extensions = /usr/lib...... was given to me by the custom installation instructions on the XDebug site. I followed the setup instructions exactly as they are on that page - http://xdebug.org/wizard.php
But from what I have read typing the command php -m should show XDebug twice, once under PHP modules and once under Zend modules. Well it is showing up under neither for me. So it seems that XDebug is not getting loaded at all?
Anyone any ideas on how to fix this problem and get XDebug working?
You've editted /etc/php5/apache2/php.ini file which is for apache. You'd want to edit /etc/php5/cli/php.ini for the command line interface's options.
If you pass in the phpinfo() from a web site, that PHP has run through Apache of course. And then the wizard will give you settings for the Apache set-up. If you would have passed the phpinfo() output (php -i) from the command line, it would have given you the settings for the CLI.
Something similar happened to me, the first time I setup xdebug in Eclipse it worked just fine, but the next time I tried it, it wasn't stopping at breakpoints. After a lot of research it turned out that I had 2 Eclipse instances running (one for Java and another for Php) and that caused some kind of issues with the debugger.
After closing both instances and starting Eclipse with php workspace first and then Eclipse with java workspace, xdebug worked fine again. My only idea is that debug port was already used when I tried to use it; or something like that.
An other common problem is that you use a router to get through INTERNET and that you had to forward XDEBUG PORT (generaly 9000) on your PC to make XDEBUG working with ECLIPSE.
But your router gives a dynamic IP to your PC and one day your mobile phone connects to the router before your PC and gets the IP address you configuredon you router IP FORWARDING !
The solution getting a static IP from your router. Here is an explanation: http://webologix.com/fr/blog/xdebug-eclipse-fonctionnement-aleatoire.html

Categories