3

I am using LAMP stack to develop a PHP application. I am using require_once to include class files. I need to use the functions in those class files in more than one PHP page. So, I am including those class files in all the required PHP pages using require_once. But, if I include those class files in more than one page, the PHP file goes blank. It displays nothing. View source also displays nothing.

Files: test.php, process.php and class.test.php

test.php has

<?php
    session_start();
    require_once 'classes/class.test.php';

    .
    Few more classes
    .
    .  


?>

<html>   
    <form name = "myForm" method="POST" action="process.php">
       <input type = "text" name="username" value=""/>
       <input type = "submit" value="Submit" />
    </form>   
</html>

process.php

<?php 
    session_start();
    require_once 'classes/class.test.php';

    $obj_test = new test();
    $obj_test->test();
?>

class.test.php

<?php
    session_start();
    require_once 'class.misc.php';
    require_once 'config.php'; //DB connection details

    function test()
    {
        $obj_misc = new misc();
        $id = $obj_misc->random_ID();
        $username = $_POST['username'];             

        $query = "INSERT INTO test_table VALUES ('$id','$username',NOW());
        mysql_query($query);
    }
?>

Now, it returns a blank page. If I comment out the require_once in process.php, the test.php page displays the form, but on submitting the form the process.php throws an error "class test not found".

I am struggling with this problem for the past 2 weeks. :( It was working fine before that. I don't understand what went wrong. Please help.

sarghau
  • 552
  • 1
  • 6
  • 14
  • Maybe you should check the paths to the files where the classes are. They change depending on the location of the files that require them – Felipe Alameda A Nov 22 '12 at 07:25
  • Hi @sarghau, welcome to SO. +1 for a well formatted question. Now, why do you have two `test.php` snippets? Could the problem be you're calling the wrong `test.php` on accident? – Ben Nov 22 '12 at 07:26
  • Have you checked your error log? I normally suggest leaving out the closing ?>, as it can lead to headers being sent prematurely. – Joshua Kaiser Nov 22 '12 at 07:26
  • How commenting something in `process.php` can affect `test.php`, when you say that doesn't interact with `test.php`? You are misexplaining something – Ranty Nov 22 '12 at 07:27
  • Enable php errors so you dont end up with blank page next time. Error will explain what is wrong and you will not need to post question in SO. – wormhit Nov 22 '12 at 07:30
  • @FelipeAlamedaA: The paths are right. I have checked them. It worked fine 2 weeks back. – sarghau Nov 22 '12 at 07:33
  • @Steve: Sorry, the second test.php was class.test.php Corrected now. – sarghau Nov 22 '12 at 07:34
  • @wormhit: I have already enabled the errors. It displays only "session headers already sent" and nothing else. So, I removed error_reporting from the code. – sarghau Nov 22 '12 at 07:36
  • @sarghau It means that some of your code is echoing or printing results! Just include your file after session_start() and everything will be ok. – wormhit Nov 22 '12 at 07:37
  • @sarghau "I am using LAMP stack to develop a PHP application" ... Are you 'developing' on windows and pushing to nix by chance? – Edward J Beckett Nov 22 '12 at 07:38
  • @EddieB: I am using netbeans on Mac OS X and I do a FTP directly to the server. – sarghau Nov 22 '12 at 07:40
  • @JoshuaKaiser: The error_log doesn't have anything related to this. Let me try not closing the tag. – sarghau Nov 22 '12 at 07:42
  • Make sure your line endings are correct and that you don't have extra lines at the end of your files. – Edward J Beckett Nov 22 '12 at 07:42
  • @wormhit - I have included the files after session_start() right? – sarghau Nov 22 '12 at 07:44
  • If you don't mind the audience ... try pasting a gist of your code and sharing the link so we can check it out for ya. – Edward J Beckett Nov 22 '12 at 07:45
  • @EddieB It's a huge code involving lots of classes and other php files. So, I posted only the fragment where it throws an error. – sarghau Nov 22 '12 at 07:49

5 Answers5

5

You have an error in the PHP code for process.php; you are missing a semicolon:

require_once 'classes/class.test.php'

should be:

require_once 'classes/class.test.php';

If that doesn't fix it, then there is probably some other error somewhere in your code. Without access to the full source, we won't be able to do much.

For future reference, if a page goes blank, there is usually a problem with the PHP source code (ie, some type of interpreting error). As part of good debugging tactics, look into display_errors and error_reporting

cegfault
  • 6,442
  • 3
  • 27
  • 49
  • Corrected it. It's a huge code involving lots of classes and other php files. So, I posted only the fragment where it throws an error. – sarghau Nov 22 '12 at 07:49
  • in class.test.php `$query = "INSERT INTO test_table VALUES ('$id','$username',NOW()); mysql_query($query);` Is missing the other quote - " - at the end of the string. does that help? – cegfault Nov 22 '12 at 08:25
  • @cegfault It was a typo ! But that's not the problem. If there is a syntax error, the IDE which I use will point. – sarghau Nov 22 '12 at 08:32
  • Try running `php -l` on all the files, that will show the syntax errors (your IDE seems to have failed here...) – Botond Balázs Nov 22 '12 at 08:40
  • @BotondBalázs - That was a copy paste mistake. Not IDE's. – sarghau Nov 22 '12 at 08:55
  • Please post all of `class.test.php` ... you are calling `$obj_test = new test();`, but I don't see any class named `test` – cegfault Nov 22 '12 at 23:17
3

Try out with

error_reporting(E_ALL);

som
  • 4,650
  • 2
  • 21
  • 36
1

Sounds like your error reporting is turned off. You should check your error logs to see what exception is being thrown when it's failing silently (that will give you a little more insight).

Additionally, you may want to add this at the top of your process script:

error_reporting(E_ALL);
Steven Moseley
  • 15,871
  • 4
  • 39
  • 50
0

Require once is a function ...

try adding the parens ...

 require_once( 'classes/class.test.php' );    
Edward J Beckett
  • 5,061
  • 1
  • 41
  • 41
  • require_once, require, include_once, and include are all like `echo` - you don't *need* parenthesis. The [PHP Manual](http://us3.php.net/manual/en/function.include-once.php) even has examples without parenthesis... – cegfault Nov 22 '12 at 07:31
  • The problem was not with the code. I ran the code in my machine. It worked. There was some problem in the server which was resolved by the provider. – sarghau Dec 28 '12 at 06:26
0

looks like you are missing a closing qoute on the sql query

$query = "INSERT INTO test_table VALUES ('$id','$username',NOW());
mysql_query($query);

should be

$query = "INSERT INTO test_table VALUES ('$id','$username',NOW())";
mysql_query($query);
Eyal Alsheich
  • 452
  • 4
  • 12
  • It was a typo ! But that's not the problem. If there is a syntax error, the IDE which I use will point. – sarghau Nov 22 '12 at 08:30