LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   In PHP why does this lead to a server connection crash? (https://www.linuxquestions.org/questions/programming-9/in-php-why-does-this-lead-to-a-server-connection-crash-341600/)

vharishankar 07-09-2005 09:29 AM

In PHP why does this lead to a server connection crash?
 
I noticed a strange thing today.

Till I did this, I had absolutely no problems whatsoever,

PHP Code:

function func ($arg)
{
      
$retval $arg;
      return 
$retval;


Now in some cases I do need to return the same argument that was passed by the function, but this is leading to a bizarre crash, where the http://localhost stops responding and acts as though the site is loading, whereas nothing happens.

When I type http://localhost in konqueror, I at least got this error message:
Code:

An error occurred while loading http://localhost

Connection to host localhost is broken.

Connection to host localhost is broken.

By the way I am passing a constant to the function argument. Could this be the problem?

vharishankar 07-09-2005 09:43 AM

I forgot to mention. The incredible thing is that if I returned like this:
PHP Code:

function func ($arg)
{
     
$newval $arg ' ' ;
     return 
$newval;


It works perfectly. This is bizarre and incredible. Why am I unable to return the argument but able to return the argument with a space appended.

I am unable to return the same value as the argument even by using sprintf to copy like this. This also leads to a server connection crash.

$newval = sprintf ('%s', $val);

vharishankar 07-09-2005 09:52 AM

Now the third incredible thing. This works well in a small sample test, but refuses to work in my main application where the exact problem is reproduced.

How incredible is that? I can confirm that I have no problems with any other code in my main application. It's only this one single thing that is causing all the problem.

I'm sorry for posting repeatedly, but I feel that I must explain my strange problem better.

:confused: :confused:

AnanthaP 07-09-2005 10:26 AM

Easy man. Any more points coming to you? Collect all points and konquer the problem. What else does the function do?

"retval" is a common handle. Any scoping issues? I notice (from the sample code) that you don't explicitly declare it.

End

keefaz 07-09-2005 10:28 AM

Maybe a problem with the HTTP headers, they have to be text/html to render
correctly, try add a
PHP Code:

header('Content-type: text/html'); 

Add this before any output, so maybe near the top of script

vharishankar 07-09-2005 10:31 AM

Why should this lead to a server connection crash?

And also the variable name does not matter. There is no scoping issues either because it is the value returned that causes the problem, not the variable itself.

Eg. I am able to return
PHP Code:

function func ($val)
{
      
$newval $val ' ';
      return 
$newval;


without any problems. But if I use
PHP Code:

function func ($val)
{
      
$newval $val;
      return 
$val;


It is leading to a server crash. What could be the ********* problem here?

vharishankar 07-09-2005 10:35 AM

keefaz, thanks for the suggestion, but that does not solve it. This seems truly an amazing, incredible error. I wish I could explain better, but I have done my best and this has really rendered me stunned.

The return value is valid and I even checked it properly using the command line PHP to output the resulting HTML where everything is tip-top perfect. Why the ******* is the HTTP server having a crash problem?

When this line is removed or a small character is appended to the return value, it works perfectly beautifully.

keefaz 07-09-2005 10:38 AM

Could you post a complete script example with real values ?
This never happened to me, I am curious to see how do
you use your functions

vharishankar 07-09-2005 10:50 AM

Actually it is a very big script indeed (or growing big). This is a project I am doing "Link directory" using PHP with HTML templates and so on. There are several files involved and more than 500 - 600 lines of code already.

Shall I email you the code with the files as the attachments? I don't think I'll be able to post them here. You will need all the files including the templates, the includes and the config settings to be correct for it to work on your end.

keefaz 07-09-2005 10:52 AM

If you can't reproduce this behaviour in a simple script,
obviously the problem isn't in this function, it's elsewhere

vharishankar 07-09-2005 10:53 AM

I have a better idea. I shall upload this to my remote web server and then provide you a URL. Just try testing the program at your end and report the problem.

Then I'll change the code to work properly and then you can try again.

This way, I'll be able to check whether it's just my local server problem or a PHP problem.

If you need any portion of the code, I'll just email it to you tomorrow.

It's now > 9:30 pm here and I think I'm going to bed now.

Thanks for your help!

vharishankar 07-09-2005 11:06 AM

I have narrowed down this problem further and this grows more and more bizarre.

It's not a problem with the function returning. That works perfectly.

Actually it is a problem like this:

My urls for my application are defined as constants like this in a file called config.php:
PHP Code:

        define ('ADMIN_PAGE''admin.php');
        
define ('INDEX_PAGE''index.php');
        
define ('LOGIN_PAGE''login.php');
        
define ('REGISTER_PAGE''register.php'); 

Now, get this. If I create my HTML file using these URLs, then the server crashes.

However, if I use it like this with a space after each URL:
PHP Code:

        define ('ADMIN_PAGE''admin.php ');
        
define ('INDEX_PAGE''index.php ');
        
define ('LOGIN_PAGE''login.php ');
        
define ('REGISTER_PAGE''register.php '); 

Then it works perfectly!!!!!!!!!!!!!!

This is amazingly stupid. In fact, MAD!!!!!!!! It worked perfectly this morning with the old code and now, all of a sudden the server starts crashing because of some silly problem like this.

vharishankar 07-09-2005 11:14 AM

I know the problem.

Really stupid problem in my template handling. This has got to be a big problem for me.

Sorry for misleading, but it really eluded me like hell. Now I got a proper error message in my output and I was able to find the culprit. Don't know why the server crashed all the while though.

Should be a memory leak I figure.

vharishankar 07-09-2005 11:18 AM

Fixed it. Actually the real problem was this.

I wanted to pass the string "index.php" "login.php" etc. to my template file as an absolute string.

However the template handler was designed to parse a file name passed in the string if it existed. So this lead to the problem of "index.php" getting included in "index.php" again and again and again. This led to a memory leak I think.

Now I have checked the condition that unless the passed string contains the text ".html" (that is, it is a template file name, not any file name), then it won't get parsed as a file, but as a pure template string.

I hope this explains everything.

Obviously when I passed the space in front of the string, it led to the fact that the file didn't exist and so it got parsed normally as a string.

And because in HTML, the <A HREF> ignores spaces at the end of a URL string, the app worked fine without any errors.

This led to bizarre results.

:) :D


All times are GMT -5. The time now is 05:43 PM.