I'm stuck trying to understand the the functions of the variables define(); and defined();. I'm making an admin page for my website and I have seen in other script they use something like this at the beginning of any admin page... <?php defined( 'ADMIN_ACCESS' ) or die( '' ); ?> PHP: So if someone is not an admin the script will die. How can I do that? how and where do I define what ADMIN_ACCESS is? Thanks in advance!
defined() is identical to isset() <?php defined( 'ADMIN_ACCESS' ) or die( '' ); ?> //<-- This line will check if the constant ADMIN_ACCESS is exists or not. defined is only use to check the existing of constant. define is use to create a new constant. define('ADMIN_PASS','password123') //<-- That is and example of how you create a constant variable in PHP
define(); creates a constant. Use it as such: define (CONSTANT, VALUE); PHP: defined(); checks if a constant has been... defined =) define ('ADMIN_ACCESS', 'yes'); if (!defined('ADMIN_ACCESS') || !ADMIN_ACCESS) die('Not a valid entry point'); PHP: Edit: Oops.. php-lover submitted his reply just before mine =)
Thank you guys for your replies, OK now I see how that works, but how can I use that to block a regular member to access an admin page?
ahowell already give you the code: define ('ADMIN_ACCESS', 'yes'); if (!defined('ADMIN_ACCESS') || !ADMIN_ACCESS) die('Not a valid entry point'); Code (markup): But that is not so user friendly, you should redirect the user to the login page so the user could connect as admin if he/she can. die will stop the execution and nothing will be displayed in the page. of course you should define the ADMIN_ACCESS with the value "yes" after the user is logged as admin.
Hey thanks for the reply. That's where I'm a little bit lost, where and how do I define the user is a admin? Can I use a cookie or a database? and If I can will this code.. if (!defined('ADMIN_ACCESS') || !ADMIN_ACCESS) die('Not a valid entry point'); PHP: be able to tell who's an admin a who's not without me echoing out the cookie or the database? Thanks for any help