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><m eta http-equiv=\"REFRESH \" content=\"3;
url=$newPath\"> </head>";
echo "<body>You can't access this page without logging
in!<p>Redirecti ng 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
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><m eta http-equiv=\"REFRESH \" content=\"3;
url=$newPath\"> </head>";
echo "<body>You can't access this page without logging
in!<p>Redirecti ng 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
Comment