what is the correct/nice way to exit php CGI script running on a web server?

From: Alex Hunsley (lard_at_tardis.ed.ac.molar.uk)
Date: 07/26/04


Date: Mon, 26 Jul 2004 00:45:54 GMT

I'm making a web site that does login authentication.
It all works fine, but I have a question about 'correctness'...

Certain php scripts on the site should only run in the user has logged
on. So, the 'private' scripts in question all includes the following php
script:

<?
    // has user authenticated?
    if (! $logged_in) {
        $newPath='login.php';

        echo "<html><head><meta http-equiv=\"REFRESH\" content=\"3;
           url=$newPath\"></head>";

        echo "<body>You can't access this page without logging
in!<p>Redirecting to the <a href=\"$newPath\">login page</a> in 3
seconds.</body></html>";

    }
    // exit php so that no more content gets output!
    exit(0);

?>

As you can see, if the user is not logged on, we output an http refresh
to redirect the users browser to the login screen. It works fine, but my
main question is about the trick of calling exit(0) at the end, which is
  required to stop the php script that includes the above code from
outputting any of its content (which is 'secret' from unauthenticated
users.)
Is calling exit(0) ok to this end? Is it considered a dirty hacky way of
doing it? It does the job, but I'm just wondering if there is a nicer
way of doing it I should be using.

thanks
alex



Relevant Pages

  • Re: "Best" way to send email
    ... straightforward, most reliable, no license hassles" way to send a ... your clients web site loaded with the php scripts (you ... 'Recover' - top rated FoxPro file repair utility. ...
    (microsoft.public.fox.programmer.exchange)
  • Re: Generate HTML index of a directory
    ... my web site because my ISP's web-server doesn't automatically index ... index with relative links to the files. ... Why not use one of those PHP scripts locally, if you can persuade it to ... Use apache and wget to make your static HTML. ...
    (uk.comp.os.linux)
  • Re: When to use a sub-web?
    ... A Subweb is useful when you have a large web site that is sub-divided into ... we recently added a blog software (developed with PHP scripts ...
    (microsoft.public.frontpage.programming)
  • Re: Generate HTML index of a directory
    ... my web site because my ISP's web-server doesn't automatically index ... Why not use one of those PHP scripts locally, if you can persuade it to make relative links? ... Use apache and wget to make your static HTML. ...
    (uk.comp.os.linux)