简体   繁体   中英

Prevent continued loading of only current file without interrupting loading of parent file?

Looking at the following structure:

groups.php

<?php
session_start();
include('header.php');
if($_SESSION['admin'])
{ include('groups_admin.php'); }

... html ...

?>

groups_admin.php

<?php
if(!$_SESSION['admin'])
{ die(); }
... html ...
?>

inside of groups_admin.php - performing the die(); causes the rest of the page (including groups.php) to stop loading.

without wrapping all of groups_admin.php is a big if statement and reversing it; is there anyway to tell PHP to just stop loading that specific PHP file while inside of that file?

Just do a return in the include eg:

<?php
if(empty($_SESSION['admin']))
{ return false; }
... html ...
?>

A lot of people don't know you can return a value from an included file.

Just for completion of this answer. You have a couple of other, less elegant (and not recommended) options.

  • You throw an exception and catch it outside of the include
  • Wrap it in an if block

Although this is suboptimal compared to the return solution, I just can't help it.

Guess what: php supports goto !

<?php
if(!$_SESSION['admin'])
{ goto endoffile; }
... html ...
endoffile:
?>

I just found out myself and it zapped me back in time, to my GWBASIC years... (sigh!)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM