www.webdeveloper.com
Results 1 to 4 of 4

Thread: Modify a php file that contains definition in a form

  1. #1
    Join Date
    Jun 2004
    Posts
    1,299

    Modify a php file that contains definition in a form

    Hi,

    I have a file that has a load of definitions in it. I want to be able to modify these definitions from a form.

    Lets say I have a file with;
    PHP Code:
    /***
    default for template 4
    ***/
    define(DB_MENU_4_NEW"New");
    define(DB_MENU_4_EDIT"Edit");
    define(DB_MENU_4_COPY"Copy");
    define(DB_MENU_4_DELETE"Delete");
    define(DB_MENU_4_HELP"Help");

    /***
    defaults for template 5
    ***/
    define(DB_MENU_5_CLOSE"Close");

    /***
    defaults for template 6
    ***/
    define(DB_MENU_6_CLOSE"Close");
    define(DB_MENU_6_APPLY"Apply"); 
    When my form is submited I will have the number and the name being passed to it.

    So lets say I want to add "Some text" to template 5 I would want it to go UNDER the DB_MENU_5_CLOSE and NOT just appended to the bottom as:
    PHP Code:
    define(DB_MENU_5_SOMETEXT"Some text"); // Added DATETIME 
    I am obviosly creating a string above but my problem is finding how to input it in the correct location on the definitions page.

    There will be consistency with the \*** begining a template and obviously \*** will start the next one see we kinda have the point of references.

    I am hoping that the same would apply if I wanted to alter the value of a definition?

    Thanks
    k0r54
    Last edited by k0r54; 05-26-2010 at 11:29 AM.

  2. #2
    Join Date
    Jul 2009
    Location
    Falls Church, Va.
    Posts
    780
    You might find this response odd...

    First off, define() creates a constant which, according to PHP's own reference page for that function, can only be changed in one way -- if set to the value of another constant. You can see if it's been defined already using defined() but a constant really is an identifier to a variable who's value "cannot change during the execution of a script" as per the PHP docs on constants. There are a few magic constants such as the ever popular "__FILE__" which change based on where they're used, but a normal constant is SUPPOSED to be just that -- constant, unchanging.

    But guess what...

    PHP Code:
    define('TEST','foo',true);
    var_dump(TEST);
    define('TEST','bar');
    var_dump(TEST); 
    The results of that, with no errors, are:

    string(3) "foo" string(3) "bar"

    This is true in PHP 5.3.2 on my system - and seems to exploit a weakness in the third parameter of the define() function which is supposed to determine case sensitivity when creating the constant. It seems if you explicitly define true (even though its the default, case insensitive) this behavior occurs. Bug?

    Just because you can do this now doesn't mean you should!

    So I'm saying don't use constants for things like what you're trying to do.

    -jim

  3. #3
    Join Date
    Jun 2004
    Posts
    1,299
    Hi,

    Ok, was affraid of something like that. It means a complete rebuild of the system.

    What I might do as a work around for the moment is put all these into a database and at the begining of the script create a definition from the results of the sql statement.

    Not sure what speed implications that would have but it is a step in the correct direction whilst I trawl through all parts of the code removing any definition that should not be used in this way.

    thanks for the insight!

  4. #4
    Join Date
    Jul 2009
    Location
    Falls Church, Va.
    Posts
    780
    That would work, or a simple XML file, or CVS, or custom .ini file. Also, you can still generate the PHP script with constants, code a back end CONFIGURATION TOOL or utility which allows admins to create and update said file (i.e. formats suggested above or your current format), just keep the admin config and interface separate, or standalone. No changing the existing code, just extending it. A few cosmetic changes to link the admin page, and maybe store the admin password encrypted in any of said formats or methods.

    -jim

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles