Using Eclipse/xdebug n Windows 2008 server. Yesterday I was able to successfully debug one php file. Today, with a very similar php file, it is skipping lines and then going off into oblivion.
Code snippet:
//$data = file_get_contents('php://input');
$data = '[{"ARDivisionNo":"01","CustomerNo":"ABF","ContactCode":"ARTIE JOHN","CustomerName":"American Business Futures","fname":"Artie ","lname":"Johnson","EmailAddress":"philmcintosh#comcast.net","Billing_Address1":"2131 N. 14th Street","Billing_Address2":"Suite 105","Billing_Address3":"Accounting Department","Billing_City":"Irvine","Billing_State":"CA","Billing_Zip":"92618","UDF_FC_ENABLED":"Y","UDF_FC_CUSTOMERID":2.0,"UDF_FC_ADDRESS_BOOK_ID":3.0,"TelephoneNo1":"","FaxNo":""}]';
$json = json_decode($data, true);
Foreach ($json as $i => $row) {
$customers_id = tep_db_prepare_input($row['UDF_FC_CUSTOMERID']);
$customers_firstname = tep_db_prepare_input($row['fname']);
(I pasted in the json for debugging purposes - this worked yesterday in the other file.)
In this file, the first strange thing is the debugger actually stops on the commented out "$data = file_get_contents('php://input');" line.
The after the next "$data =" line, it skips to "$customers_id =" line. At this point $data shows as empty in the variables window.
Any ideas on what is wrong/how to fix?
It is likely that Xdebug seems a different file of source code than that you think you see. Are you sure you have saved the file first before starting to debug?
There are a few cases where Xdebug does skip lines as it doesn't see any breakpoints. This is because PHP itself doesn't always give this possibility. It is often on if/else statements without { and } only though. In order to find out what is going on, you can make a remote debugging log by adding to php.ini:
xdebug.remote_log=/tmp/xdebug.log
Xdebug will write all communication packets into this log for you to look at. Perhaps that gives a clue.
Related
I'm facing a quite weird issue where using php apache_response_headers function returns an array of headers where the keys are truncated by one character
Notes:
I have tested with a bare test.php file with print_r(apache_response_headers()) and got the same results
the issue does not appear as soon as I switch to php <= 5.6
I have tested on a few servers with the same results
I have searched all over but no one seem to have the same issue
Is it something that someone has encountered in the past
Would there be a way to debug this?
thanks in advance
I can confirm that I'm seeing the same issue on Windows, IIS 7.5, PHP 7.0.27. I do not have this issue on Linux, Apache 2.4, 7.0.30.
A pseudo workaround is:
$headers = array() ;
foreach(headers_list() as $header) {
$temp = explode(':',$header,2) ;
$headers[$temp[0]] = trim($temp[1]) ;
}
echo '<pre>' ; var_dump($headers) ;
I'm sure there may be situations that the above hack won't give you what you want, but I am not familiar enough with Apache's headers to know.
This question already has answers here:
making print_r use PHP_EOL
(5 answers)
Closed 6 years ago.
I've been coding in PHP for a long time (15+ years now), and I usually do so on a Windows OS, though most of the time it's for execution on Linux servers. Over the years I've run up against an annoyance that, while not important, has proved to be a bit irritating, and I've gotten to the point where I want to see if I can address it somehow. Here's the problem:
When coding, I often find it useful to output the contents of an array to a text file so that I can view it's contents. For example:
$fileArray = file('path/to/file');
$faString = print_r($fileArray, true);
$save = file_put_contents('fileArray.txt', $faString);
Now when I open the file fileArray.txt in Notepad, the contents of the file are all displayed on a single line, rather than the nice, pretty structure seen if the file were opened in Wordpad. This is because, regardless of OS, PHP's print_r function uses \n for newlines, rather than \r\n. I can certainly perform such replacement myself by simply adding just one line of code to make the necessary replacements, ans therein lies the problem. That one, single line of extra code translates back through my years into literally hundreds of extra steps that should not be necessary. I'm a lazy coder, and this has become unacceptable.
Currently, on my dev machine, I've got a different sort of work-around in place (shown below), but this has it's own set of problems, so I'd like to find a way to "coerce" PHP into putting in the "proper" newline characters without all that extra code. I doubt that this is likely to be possible, but I'll never find out if I never ask, so...
Anyway, my current work-around goes like this. I have, in my PHP include path, a file (print_w.php) which includes the following code:
<?php
function print_w($in, $saveToString = false) {
$out = print_r($in, true);
$out = str_replace("\n", "\r\n", $out);
switch ($saveToString) {
case true: return $out;
default: echo $out;
}
}
?>
I also have auto_prepend_file set to this same file in php.ini, so that it automatically includes it every time PHP executes a script on my dev machine. I then use the function print_w instead of print_r while testing my scripts. This works well, so long as when I upload a script to a remote server I make sure that all references to the function print_w are removed or commented out. If I miss one, I (of course) get a fatal error, which can prove more frustrating than the original problem, but I make it a point to carefully proofread my code prior to uploading, so it's not often an issue.
So after all that rambling, my question is, Is there a way to change the behavior of print_r (or similar PHP functions) to use Windows newlines, rather than Linux newlines on a Windows machine?
Thanks for your time.
Ok, after further research, I've found a better work-around that suite my needs, and eliminates the need to call a custom function instead of print_r. This new work-around goes like this:
I still have to have an included file (I've kept the same name so as not to have to mess with php.ini), and php.ini still has the auto_prepend_file setting in place, but the code in print_w.php is changes a bit:
<?php
rename_function('print_r', 'print_rw');
function print_r($in, $saveToString = false) {
$out = print_rw($in, true);
$out = str_replace("\n", "\r\n", $out);
switch ($saveToString) {
case true: return $out;
default: echo $out;
}
}
?>
This effectively alters the behavior of the print_r function on my local machine, without my having to call custom functions, and having to make sure that all references to that custom function are neutralized. By using PHP's rename_function I was able to effectively rewrite how print_r behaves, making it possible to address my problem.
I have a txt file that contains about 400 urls to images(excerpt below). I wrote the below script to quickly download the files from one server to another (no ssh access).
Text File - pics.txt
http://www.domain.com/pictures/name.jpg
http://www.domain.com/pictures/name1.jpg
http://www.domain.com/pictures/name2.jpg
http://www.domain.com/pictures/name3.jpg
http://www.domain.com/pictures/name4.jpg
My simple PHP Script:
$file = fopen("pics.txt", "r");
while(!feof($file)){
$line = fgets($file);
$filename = basename($line);
$imagename = "tmp/$filename";
echo "Trying to copy ".$line." to: ".$imagename;
if (copy($line,$imagename)) {
echo "Done file ".$line;
} else {
echo "Error occured";
}
}
fclose($file);
However it doesn't seem to work. I just get:
Trying to copy http://www.domain.com/pictures/name.jpg to: tmp/name.jpg Error occured
Trying to copy http://www.domain.com/pictures/name1.jpg to: tmp/name1.jpg Error occured
Trying to copy http://www.domain.com/pictures/name2.jpg to: tmp/name2.jpg Error occured
If I manually type the copy('http://www.domain.com/pictures/name.jpg', 'filename.jpg');
it works fine?
Any ideas what i'm doing wrong?
As requested, and for future readers to the question as to what the problem really was.
Your file contains \n for each line and is a hidden file character, adding a space to your URL, in turn breaking it's true path.
Using trim() will get rid of the extra spaces.
http://php.net/manual/en/function.trim.php
Use error reporting:
http://php.net/manual/en/function.error-reporting.php
From comments:
"Thanks both, I turned on the error reporting and noticed it was putting a space after the url, even though there isnt one in the text file. – Chris"
and
"#Chris that's great Chris (you're welcome) and I'm glad that it was resolved. However and for future readers to the question, the answer given really wasn't the solution. I don't want to sound as the bad man here or bust everybody's balloon; don't get me wrong. However, that being the case, you should have used trim() to get rid of the trailing spaces. The spaces come from the hidden \n in the file which adds a space. – Fred -ii-"
I have a website on a host that recently switched from PHP 5.2 to 5.4, and required us to chose a new php.ini file: 5.4 plain, 5.4 solo (just one php.ini file used throughout the site), and 5.4 fast.
I do not know which one I was using prior to making the switch, but when I did, (I chose 5.4 solo), I noticed that a part of my website that depends on mbstring (multibyte characters) no longer works.
In specific, it opens a text file that is full of characters and then that is used in an encryption script and it stores garbage in the mysql database. Then to retrieve it, it's again run through the script and decrypted, and displayed on the screen.
This worked just fine until the 5.4 change. Now it appears that it's unable to retrieve (open?) the text file. I have tested this with a non-multibyte character version and that works fine, so I don't think the issue is with the code, but rather with the way PHP is treating multibyte chars...and I suspect, just a hunch, that this is fixable by tweaking the PHP.ini file somehow. Zend.multibyte seems to be PHP's new thing.
My problem is that I have no idea what to tweak. I tried several different Zend.multibyte/mbstring combos and that didn't work.
I know that everything works up until a string is sent for encryption. It comes back as a null value, instead of a garbled string. I feel like something in the string is being rejected by PHP and thus it's failing...offering nothing instead of the string it should.
Does anyone have a thought as to what might be happening and why my script no-longer works with 5.4? I have checked and the mbstring module IS loaded, with default values in the php.ini.
Any suggestions would be great...I'm totally stumped. Even some additional reports or ways to test or narrow down the problem would be fantastic.
Thank you!
Here is some code, where I think the problem is:
$this->s1 = "";
$s1array = array("a1.txt", "a2.txt", "a3.txt");
foreach ($s1array as $i => $value) {
$myFile = "../a/dir/somewhere/$s1array[$i]";
$fh = fopen($myFile, 'r');
$theData = fgets($fh);
fclose($fh);
$this->s1 .= html_entity_decode($theData, ENT_NOQUOTES, 'UTF-8');
}
The files ../a/dir/somewhere/a1.txt and ../a/dir/somewhere/a2.txt (etc) are semi-comma delimited strings of html coded letters, for example: & #x0fb0f;& #x02c97;& #x00436;& #x10833;& #x00514; (I added the spaces so it would show code not the HTML values!).
But I guess now, for some reason, this above code isn't returning any results. If I assign the result to a variable and echo that variable, there's nothing. But if I assign $this->s1 = "abcde"; or a longer string and skip the "foreach" part, it will work. So something in this process, this code, no longer works in 5.4. Can anyone tell what's going on here? Thank you!
Why you use fopen and so on for text files when you could use file_put_contents and file_get_contents - they are mostly wrappers for fopen, freads and so on. I have NEVER ever had any problems with UTF8 using that two functions.
Also make sure everything (from php, to db if you are using it, and php files) are encoded or using utf8. There is nothing funnier than *.php files in for example latin2 and all the rest in utf8.
I have the following code:
while (!feof($file)) {
$arrayOfIdToBodyPart = fgetcsv($file,0, "\t");
if (count($arrayOfIdToBodyPart)==2){
the problem is, the contents of the file look like this:
39 ankle
40 tibia
41 Vastus Intermedius
and so on
sometimes, the test in the if will show three entries, with the first being the number, the second being the name, and the third being just... emtpy.
This causes the if block to fail, and me to be sad. I know i can just make the if block test for >=2, but is there any way i can get it to just recognise the fact that there are two items? I don't like that the fgetcsv is finding "mystery" characters at the end of the line.
Is this possibly a unix server running a windows-based file error? If so, and i'm running an ubuntu server without dos2unix, where do i get it?
You probably have tabs at the end of a line:
value<tab>value<tab><newline>
If that's the case, dos2unix won't help you. You might have to do something like read each line into a variable, trim() the variable, and then use str_getcsv() to split it.
Is it possible that you have a tab at the end of those lines? They are invisible and often hard to spot... you might want to double check.
Also if you are working with csv files, while you are running windows locally and the server is unix, I found this line:
ini_set('auto_detect_line_endings', true);
saves a lot of headaches.