what is the code meant

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Lphp
Forum Commoner
Posts: 74
Joined: Sun Jun 26, 2011 9:56 pm

what is the code meant

Post by Lphp »

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
some code
not close ?> in php page , what is that code mean? why use this kind of code
thank you!
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: what is the code meant

Post by Christopher »

It checks to see if a constant is defined. The page cannot be accessed directly. It has to be included by another page that defines BASEPATH.
(#10850)
User avatar
twinedev
Forum Regular
Posts: 984
Joined: Tue Sep 28, 2010 11:41 am
Location: Columbus, Ohio

Re: what is the code meant

Post by twinedev »

If your page ends in PHP code (ie, has no HTML after a closing ?> tag, may people will leave off the closing tag. The reason is that if you close the php tag, but then have a blank line after it, in an editor it isn't that noticeable, but if you include that file, that blank like will be sent to the browser, so you can no longer do any header() after you included that file. (unless you have enabled output buffering)

I have gotten in the habit of just ending the file with:

Code: Select all

// EOF: ~/inc/config.php 
or something similar so that when scrolling, I know it is the bottom for sure without the closing tag.

-Greg
Post Reply