note 117838 added to function.exit

From: Date: Tue, 18 Aug 2015 06:44:14 +0000
Subject: note 117838 added to function.exit
Groups: php.notes 
Request: Send a blank email to php-notes+get-203372@lists.php.net to get a copy of this message
In addition to "void a t informance d o t info", here's a one-liner that requires no constant: <?php basename($_SERVER['PHP_SELF']) == basename(__FILE__) && die('Thou shall not pass!'); ?> Placing it at the beginning of a PHP file will prevent direct access to the script. To redirect to / instead of dying: <?php if (basename($_SERVER['PHP_SELF']) == basename(__FILE__)) { if (ob_get_contents()) ob_clean(); // ob_get_contents() even works without active output buffering header('Location: /'); die; } ?> Doing the same in a one-liner: <?php basename($_SERVER['PHP_SELF']) == basename(__FILE__) && (!ob_get_contents() || ob_clean()) && header('Location: /') && die; ?> A note to security: Even though $_SERVER['PHP_SELF'] comes from the user, it's safe to assume its validity, as the "manipulation" takes place _before_ the actual file execution, meaning that the string _must_ have been valid enough to execute the file. Also, basename() is binary safe, so you can safely rely on this function. ---- Server IP: 72.52.91.14 Probable Submitter: 87.139.127.209 ---- Manual Page -- http://php.net/manual/en/function.exit.php Edit -- https://master.php.net/note/edit/117838 Del: integrated -- https://master.php.net/note/delete/117838/integrated Del: useless -- https://master.php.net/note/delete/117838/useless Del: bad code -- https://master.php.net/note/delete/117838/bad+code Del: spam -- https://master.php.net/note/delete/117838/spam Del: non-english -- https://master.php.net/note/delete/117838/non-english Del: in docs -- https://master.php.net/note/delete/117838/in+docs Del: other reasons-- https://master.php.net/note/delete/117838 Reject -- https://master.php.net/note/reject/117838 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#203372) next »