Jump to content

exit() - unexpected result


Pioden

Recommended Posts

Hi folks. First off I'm having a bad day (and know it) so sorry if this turns out to be something REALLY dumb.

 

I'm coding a basic user authentication system - like I've done many times before. However this time things are not working in a way I expect!

 

For simplicity's sake lets say I have two files - index.php and session.php. Session does the authentication. In index.php I 'require_once' session.php - that works! In session.php I have just this:

 

<?php

 

session_start();

 

if (!isset($_SESSION["defnyddiwr"])) {

echo "login box";

exit();

}

 

?>

 

The result is that the unauthenticated user gets show 'login box' but also the HTML in index.php - exit() seems to have no effect! I was expecting the script to stop at the exit point - including index.php!!

 

What on earth is going wrong? I could have sworn I've used this method in the past without any problems.

Link to comment
https://forums.phpfreaks.com/topic/132272-exit-unexpected-result/
Share on other sites

ok, first off then add

error_reporting(E_ALL);

ini_set('display_errors','On'); 

to the top of the page, just in case there is an error we can't see

then if there are no errors try

 

<?php
echo "test";
exit(" did work");
echo " didn't work";
?>

And see if that works.

Interesting! It returned this

 

login texttest did work

 

and then all the index.php stuff. No errors.

 

Seems like exit(); stops the execution of session.php but not index.php even though it's an  included file. Is this normal? I'm 99% sure I've used this method before without this problem.

Ahh, i'm not sure if file inclusion will exit the page you are viewing, i believe the exit works but only on the page it's in, not when included. If that makes sense,

<?php
//page 1
echo"test";
exit();
echo"stopped";
?>

<?php 
//page 2
include "page1.php";
echo "<br>test2";
?>

That code will stop anything after the exit on page 1 being sent to page 2, but the exit won't affect page 2.

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.