define(); and defined();?

Discussion in 'PHP' started by red-x, Aug 14, 2008.

  1. #1
    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! :)
     
    red-x, Aug 14, 2008 IP
  2. php-lover

    php-lover Active Member

    Messages:
    261
    Likes Received:
    21
    Best Answers:
    0
    Trophy Points:
    58
    #2
    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
     
    php-lover, Aug 14, 2008 IP
  3. ahowell

    ahowell Peon

    Messages:
    38
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #3
    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 =)
     
    ahowell, Aug 14, 2008 IP
  4. red-x

    red-x Peon

    Messages:
    48
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    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?
     
    red-x, Aug 14, 2008 IP
  5. gustavorg

    gustavorg Active Member

    Messages:
    37
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    61
    #5
    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.
     
    gustavorg, Aug 15, 2008 IP
  6. red-x

    red-x Peon

    Messages:
    48
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    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 :)
     
    red-x, Aug 15, 2008 IP