Debugging PHP code on a hosted server - php

I have a hosted server (rochenhost.com), where I run some PHP code.
In the old days, before I started working as a software developer, and was self taught I printed the variables out.
Now after some years of school and a developer job, and after I have learned to use debuggers, I wounder: Are there any good debugging tools for PHP code, running on a hosted server?

Is the "hosted code" you're working on directly on your production server? Or do you have two separate codebases, one for development (debugging and such) and another for production (displaying to your actual users)? As you probably know, changing code directly on your production server is kind of insane and is almost guaranteed to occasionally bring your site down or create security holes. So my biggest piece of advice would be to get a local development server. This can be as easy as downloading the appropriate XAMP stack for your computer and using your favorite VCS to sync files with the production server once you've debugged them.
Once you have a local development server, check out this question for a list of debuggers with step-through functionality and also this one for a larger list of IDEs available on different platforms.
If you are stuck debugging code on a remote server, here are a couple of other practices that can help. You may already be doing them.
1) Turn on error output. You can do this for a particular script by inserting the following lines at the beginning:
ini_set("display_errors","1");
error_reporting(E-ALL);
This will print (sometimes) informative error messages to the page. It is considered a major security risk to expose this information to visitors, so make sure you remove these lines when you're done testing. If you have a local development server or one that's not accessible to the outside world, you can turn on error reporting for all pages by adding the line display errors = 1 to php.ini.
2) Locate your server's PHP error log. This often contains information about why a page died, even when you're not able to load enough of the page for PHP to display error messages there. You can also use the command error_log('your message here') to print a message to the log, which is useful when you can't just dump the info on your page.

I use the FirePHP extension for FireFox and ChromePhp for Chrome. They put log messages in the console log of the browsers. They have save me hours of debugging time.

Related

What causes Website timeout when it is working flawlessly with vpn?

I have some php (php 7.4) program that is connected to a Webserver that causes problems when I do not use a VPN.
Sadly, we just using the server without any rights to config it.
So if you need something specific I can ask for this Information.
There are some Serverinformations
DB-Server
Server-Type: MariaDB
Server-Version: 10.4.20 MariaDB - MariaDB Server
Webserver
Apache/2.4.37(centos)
PHP-Ext: mysqli, curl, mbstring
PHP-Version: 7.3.20
phpinfo():
PHP 7.4.18
Other People testing the program getting same results.
My php program is working flawlessly with VPN and local.
Now here is the issue:
It is some straight-forward program. You need to answer the questions to get further and after some time you can end the program. A Survey, you can start it by typing the URL in any Browser you find.
#Thats bringing me to this point#
Program interrupts randomly (no specific site or element) while work through. Leaving TimeOut Website and deprecated non-js Modules console-message. Apparently things are deprecated.
#Now I use VPN#
Program does not interrupt at any point. This procedure is working for every Person testing the Program. This means, I do not get the message "deprecated non-js modules".
Either things are not deprecated or there are other reasons making the Program work without timeout.
Error-Code
Website
ERR_CONNECTION_TIMED_OUT
Console of Chrome
VM9:7146 crbug/1173575, non-JS module files deprecated.
My Doings and Ideas:
I checked the whole Program for deprecated elements, tags, functions...
-> Using PHPStorm and:
ini_set('display_errors', 1)
ini_set('display_startup_errors', 1)
error_reporting(E_ALL)
-> There were something. Every used deprecated something could be replaced easily. Lucky me.
IP-Adress is stable while working through the program.
Program is working local. Tested it 50 times without any interruptions.
Program is working with VPN. Tested it...
Session variables expire after 6 hours.
classic mysqli_connect.
session cookies.
using css and js files.
using other php files for configs and better organisation.
####New Facts
I tested some other php program (more complex - needed to add "preventDefault()" to make it work for Chrome/Safari). Using same files and every function of the contaminated php program. And no timeouts without VPN.
-> Now I think it is the specific program. But why? There is nothing new in use.
Differences:
The corrupted program shows some text and answer options you need to click to get to the next site. Website timeouts occur randomly when I press the radio button to send the value.
New program shows also some text and these answer options. But you can interact with it. After you press the radio button, you get pictures and buttons which are telling you what to do. I added a simple event handler (js) to show these pictures and buttons and hide em (depends on the condition the program gives you). It is using the same functions to send values, present pictures/texts, hop to the next page. It has generally more pages, therefore more texts, pictures.
The VPN I use must be very close to the server. But cannot tell for sure. It is something like an university-vpn to access specific things.
Tested it with handy-network (5G). No timeouts! I asked people to test it, so we get some stability-statistics.
It is important that people (not only me) outside this vpn-network can access the program and end it successfully!
Can you show me some lead I can follow?
Thank you. :)
I solved my problem, contacting the server-support.
Firewall rules are very strict and blacklisted people who are testing the program with >1 click per second. Apparently that is the behavior of a bot.
That explains also the working other php-program, which uses same functions, files, everything. There, you couldn't be faster than 1 click per second because you need to wait for some elements to show you what to do before sending produced values.
Special thanks to ADyson! :)

Laravel stopped writing logs on Azure

Up until recently any error messages laravel would produce were written to /app/storage/logs on Azure, as they are supposed to. They still do locally, and my live server uses the exact same laravel-configuration. However, on my live server laravel stopped writing to the log files about 2 months ago.
Log::Info still works, but unless I tell it to write something to the logs it doesn't.
As it works locally and the exact same configuration is live, I don't know where I should start looking, and googling has revealed to be fruitless. I'm sorry I have not included any images or code, but I am completely clueless what could cause the error. Maybe it has something to do with writing privileges? Any ideas?
Progress 1: When something is supposed to be written to the logs-folder, the "Most recently modified at"-date changes to current date and time. However, nothing inside the folder changes.
Progress 2: An error was just printed to the log, but not all errors are printed. There is an imagick error that should be printed to the log that currently isn't, but the fact that another error message was printed changes the entire question. Just found out, going to test some stuff now.
Progress 3: I have confirmed that the only thing not printed to the log is when imagick fails to load a pdf. There are other cases of this particular problem of imagick happening on a windows server, where reading pdf:s causes imagick to crash without any error message. This means that this question is no longer relevant, thank you for all your time people.
You may check whether the disk space is full of your App Service, if so, it may raise your issue.
And every App Service pricing tier has a limit disk space and will shared in all the web apps in this App Service plan.
You can check the metric on dashboard page of your web app portal, e.g.
You can refer to https://azure.microsoft.com/en-us/pricing/details/app-service/ for details.
As a workaround, you can build a web job to run a script continuously to move your old log files to Azure Blob Storage or a database, to save storage space in App Service.

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/MySQL Performance Testing with Just PHP

I'm trying to diagnose a server where the website is loading very slowly, but unfortunately my client has only provided me with FTP access.
I've got FTP access so I can upload PHP scripts, but can't set up any other server side tools.
I have access to phpMyAdmin, but not direct access to the MySQL server. It is also unfortunately a Windows server (and we've been a Linux shop for over a decade now).
So, if I wan to evaluate MySQL & disk speed performance through PHP on a generic server, what is the best way to do this?
There are already tools like:
https://github.com/raphaelm/php-benchmark or https://github.com/InfinitySoft/php-benchmark
But I'm surprised there isn't something that someone has already set up & configured to just run through and do some basic testing of a server's responsiveness.
Every time we evaluate a new server environment it's handy to be able to compare it to an existing one quickly to see if there are any anomalies. I guess I'd just hoped that someone else had written up a script to do this already. I know I have, but that was before Github when there was a handy place to post scraps of code like this.
You've probably already done this, but just in case... If I were in your shoes, the first thing I'd be looking at are the indexes on the mysql tables and the queries in the application. I've seen some sites get huge speed boosts just by fixing a join or adding a missing index.
Don't forget to check the code for performance issues or calls to sleep(). If you haven't yet, it may be helpful to get the code running locally so you can run it through xdebug.

php script to read xml data on remote unix server

I got a situation where I have lots of system configurations/logs off which I have to generate a quick review of the system useful for troubleshooting.
At first I'd like to build kind of web interface(most probably a php site) that gives me the rough snapshot of the system configuration using the available information from support logs. The support logs reside on mirrored servers (call it log server) & the server on which I'll be hosting the site (call it web server) will have to ssh/sftp to access them.
My rough sketch:
The php script on web server will make some kind of connection to the log server & go to the support logs location.
It'll then trigger a perl script at logs server, which will collect relevant stuffs from all the config/log files into some useful xml (there'd be multiple of those).
Someway these xml files are transferred to web server & php will use it to create the html out of it.
I'm very new to php & would like to know if this is feasible or if there's any other alternative/better way of doing this?
It would be great if someone could provide more details for the same.
Thanks in advance.
EDIT:
Sorry I missed to mention that the logs aren't the ones generated on live machine, I'm dealing with sustenance activities for NAS storage device & there'll be plenty of support logs coming from different end customers which folks from my team would like to have a look at.
Security is not a big concern here (I'm ok with using plain text authentication to log servers) as these servers can be accessed only through company's VPN.
Yes, PHP can process XML. A simple way is to use SimpleXML: http://php.net/manual/en/book.simplexml.php
While you can do this using something like expect (I think there is something for PHP too..), I would recommend doing this in two separate steps:
A script, running via Cron, retrieves data from servers and store it locally
The PHP script reads from the local stored data only, in order to generate reports.
This way, you have these benefits:
You don't have to worry about how to make your php script connect via ssh to servers
You avoid the security risks related to allowing your webserver user log in to other servers (high risk in case your script gets hacked)
In case of slow / absent connectivity to servers, long time to retrieve logs, etc. you php script will still be able to quickly show the data -- maybe, along with some error message explaining what went wrong during latest update
In any case, you php script will terminate much quicker since it only has to retrieve data from local storage.
Update: ssh client via php
Ok, from your latest comment I understand that what you need is more a "front-end browser" to display the files, than a report generation tool or similar; in this case you can use Expect (as I stated before) in order to connect to remote machines.
There is a PECL extension for PHP providing expect functionality. Have a look at the PHP Expect manual and in particular at the usage examples, showing how to use it to make SSH connections.
Alternate way: taking files from NFS/SAMBA share
Another way, avoiding to use SSH, is to browse files on the remote machines via locally-mounted share.
This is expecially useful in case interesting files are already shared by a NAS, while I wouldn't recommend this if that would mean sharing the whole root filesystem or huge parts of it.

Categories