View Full Version : buy_now issue I was having and the solution

12-09-2010, 07:29 AM
For this particular error/issue I found several posts related to oscommerce and variations there of but no solutions so here is what I found (at least in my case).

NOTE: I inadvertantly caused this issue on my own site. This issue did not exist in the released code.


the "Buy it Now" button added the item to the shopping cart but the screen was simply blank after the action. The URL showing in the navigation bar was still the product page url nad not the shooping cart url as expected.

My thought process and debugging path:

1) I tried my other OSCMax installations and 1 out of the 3 worked fine.

2) I pointed the site that wasn't working to the database of the site that did not have the issue and the issue remained. From that I determined that the issue was in the site files and not in the database.

3) I downloaded both the working and non-working sites to local folders. I removed non-code items like image files. I then ran "windiff" and compared the 2 file structures to each other.

4) At this point I was a little confused because almost everything was identical between the 2. I viewed the differences in the files that were different and did not see anything that would cause an issue.

5) Feeling like I was at a dead end I went a different direction and found the 'add_now' action in the application_top.php file. By inserting 'echo' commands I was able to determine the approximate location of the issue. The 'tep_redirect' function was being executed and the 'echo' of the parameters revealed that the parameters contained valid values but I was still perplexed as to why the browser was not getting re-directed as I thought it should.

6) Going deeper I found the 'tep_redirect' function in one of the php files in the includes\functions folder. After reviewing the code and placing some echo statements I still did not see why it was not doing what I expected.

7) At this point I was looking straight at the PHP 'header' command. It was working for all the other redirects so the question was, "Why won't it work with what appears to be a valid url?"

8) Time to google some info on php and see what is really going on! After reading PHP: header - Manual (http://php.net/manual/en/function.header.php) I found a nice little function 'headers_sent(&$file, &$line);'

I also learned that if anything is sent prior to the 'header' call it won't work which is described on the page linked above.

9) running the 'headers_sent' and echoing the results revealed that something had already been sent from 'index.php' line 2.

What? That didn't seem right but if the function is saying that is where it is I should at least look!

11) You have go to be kidding me !? I had 2 returns (blank lines) in index.php before the '<?php' opening tag.

I removed the blank lines from 'index.php' that preceeded the '<?php' tag and now all is well!

I had tried adding some google analytics code to the index.php file a few days ago and had left the blank lines. I am learning some of these rules about php the hard way but this one is burned into my brain now. Not having ever coded in PHP it took me about 3 hours to decide how to approach the issue and work through the steps listed above. If I had not ignored the files that differed in whitespace only I might have resolved the issue faster but I would not have learned the rule about calling 'header' or about the helpful function 'headers_sent'

Lesson learned and once again I am a happy camper! ;)

Hope this helps some other poor soul strugging with a similar issue. :)