Is this possible? (PHP "toggle")

Discussion in 'PHP' started by dp-user-1, Dec 2, 2006.

  1. #1
    Is it possible to give the user a link that toggles a section of my PHP code on or off, or changes the code itself?

    For instance, say I have a line of code that says "if(++$count >= 5)," and I want it to change to "if(++$count >= 10)" when the user clicks a link. Is this possible?

    If I wanted to, could I just have it omit "if(++$count >= 5)" from the code when the user clicks the link?

    Thanks!

    -Peter

    ::EDIT::

    NOTE: For those of you wondering, I ended up sticking with my original setup: two nearly identical files, different only for those lines of code. Thanks to all who replied.
     
    dp-user-1, Dec 2, 2006 IP
  2. Smaaz

    Smaaz Notable Member

    Messages:
    2,425
    Likes Received:
    160
    Best Answers:
    0
    Trophy Points:
    250
    #2
    You could do something like site.php?value=5

    And in the php code you do "if(++$count >= $value)"

    If you want to do this without a page refresh you can do it with AJAX
     
    Smaaz, Dec 2, 2006 IP
  3. CodyRo

    CodyRo Peon

    Messages:
    365
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Smaaz is right, except he's assuming register globals is on (a no no!)

    
    if (++$count >= $_GET['value']) {
       //blah
    }
    
    PHP:
     
    CodyRo, Dec 2, 2006 IP
  4. TwistMyArm

    TwistMyArm Peon

    Messages:
    931
    Likes Received:
    44
    Best Answers:
    0
    Trophy Points:
    0
    #4
    The only way you could do that would be to use a variable. Say instead of the 5 or the 10 you used a variable called limit. If it's set in a GET parameter use that value, otherwise use 5.

    Something like:
    $limit = ( isset( $_GET['limit'] ) ? $_GET['limit'] : 5 );

    Then on the link you want the user to click, just add the GET parameter (?limit=10).
     
    TwistMyArm, Dec 2, 2006 IP
  5. dp-user-1

    dp-user-1 Well-Known Member

    Messages:
    794
    Likes Received:
    20
    Best Answers:
    0
    Trophy Points:
    110
    #5
    What would the link code be?

    Also, how secure is this?

    (Thanks for all the active replies this early in the morning!)
     
    dp-user-1, Dec 2, 2006 IP
  6. dp-user-1

    dp-user-1 Well-Known Member

    Messages:
    794
    Likes Received:
    20
    Best Answers:
    0
    Trophy Points:
    110
    #6
    What I actually want to do is toggle a few lines of code.

    The way I have it set up now, a user can click a link and it goes to an identical PHP file, minus the lines I want to omit.

    How can I toggle the inclusion of these lines using one file?
     
    dp-user-1, Dec 2, 2006 IP
  7. CodyRo

    CodyRo Peon

    Messages:
    365
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Not quite sure what you want done.. but you could do something along the lines of:

    
    if (isset($_GET['blah'])) {
       //do something
    } else {
     //do something else
    }
    
    PHP:
    Instead of linking to the identical file, just link to file.php?blah and it'll execute the first set of code, otherwise it'll execute the second.

    I can provide a better example if you explain it further..
     
    CodyRo, Dec 2, 2006 IP
  8. dp-user-1

    dp-user-1 Well-Known Member

    Messages:
    794
    Likes Received:
    20
    Best Answers:
    0
    Trophy Points:
    110
    #8
    I sent you a more in-depth explanation via PM, CodyRo.
     
    dp-user-1, Dec 2, 2006 IP
  9. JEET

    JEET Notable Member

    Messages:
    3,832
    Likes Received:
    502
    Best Answers:
    19
    Trophy Points:
    265
    #9
    Anyone can just look at your source code and supply their own number via the URL. Not safe exactly... ;)
    You might want to try session id's if looking for something safer.
    Bye

     
    JEET, Dec 2, 2006 IP
  10. T0PS3O

    T0PS3O Feel Good PLC

    Messages:
    13,219
    Likes Received:
    777
    Best Answers:
    0
    Trophy Points:
    0
    #10
    Or let the link update a DB table for that user which sets the variable there. $_GET is unsafe, someone could set it to 10000000000000 and stall your server.
     
    T0PS3O, Dec 2, 2006 IP
  11. CodyRo

    CodyRo Peon

    Messages:
    365
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    0
    #11
    Hehe, already worked out via PM ;)
     
    CodyRo, Dec 2, 2006 IP
    NinjaNoodles likes this.
  12. T0PS3O

    T0PS3O Feel Good PLC

    Messages:
    13,219
    Likes Received:
    777
    Best Answers:
    0
    Trophy Points:
    0
    #12
    How about you post it then so we can all benefit. After all it's a public forum.
     
    T0PS3O, Dec 2, 2006 IP
  13. CodyRo

    CodyRo Peon

    Messages:
    365
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    0
    #13
    Something like this is posted all over, it was also a site-specific solution which others won't benefit from.

    ;)
     
    CodyRo, Dec 2, 2006 IP
  14. dp-user-1

    dp-user-1 Well-Known Member

    Messages:
    794
    Likes Received:
    20
    Best Answers:
    0
    Trophy Points:
    110
    #14
    Correct, CodyRo helped me with it, and after toying around with a few options, we decided that the best way was what I have set up now (and had originally).

    Thanks for all your help!
     
    dp-user-1, Dec 2, 2006 IP
  15. JEET

    JEET Notable Member

    Messages:
    3,832
    Likes Received:
    502
    Best Answers:
    19
    Trophy Points:
    265
    #15
    What did you have originally? ;)
    Bye
     
    JEET, Dec 2, 2006 IP
  16. dp-user-1

    dp-user-1 Well-Known Member

    Messages:
    794
    Likes Received:
    20
    Best Answers:
    0
    Trophy Points:
    110
    #16
    A link to a file that's identical, save for those lines of code.
     
    dp-user-1, Dec 2, 2006 IP
  17. clancey

    clancey Peon

    Messages:
    1,099
    Likes Received:
    63
    Best Answers:
    0
    Trophy Points:
    0
    #17
    Wowzers . . . once again someone works DP members so hard for solutions to problems in public forums. Peons reply, bury the solution, and get dodgy when asked about it.

    I thought the whole point of DP was a place for people to come and share problems and solutions, and ideas for the mutual benefit of all participants.

    Forums, like open source, rely on people to share their ideas as the best answers to questions evolve.

    I have seen so many examples around DP of people solving issues via PM and then posting the answer so all could benefit. I applaud those people because they help us all find new approaches to what are, in truth, problems each of us will most likely encounter in the future.
     
    clancey, Dec 2, 2006 IP
  18. dp-user-1

    dp-user-1 Well-Known Member

    Messages:
    794
    Likes Received:
    20
    Best Answers:
    0
    Trophy Points:
    110
    #18
    I told you exactly how I solved it. Due to potential security vulnerabilities, I didn't release tons of info about the script (I'm still learning PHP, so I don't want to expose some fatal mistake to a hacker). I tried my best to share what I came up with, which is what I had originally: two files, one with the extra code.
     
    dp-user-1, Dec 2, 2006 IP
  19. CodyRo

    CodyRo Peon

    Messages:
    365
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    0
    #19
    Ahem, as stated it was the nature of the code it was done via PM, if you look at anything else I've done (or even previously in this topic) you'll see theres nothing "dodgy".

    But I love you anyway.
     
    CodyRo, Dec 2, 2006 IP
  20. JEET

    JEET Notable Member

    Messages:
    3,832
    Likes Received:
    502
    Best Answers:
    19
    Trophy Points:
    265
    #20
    First, I want you to know that I don't have problems if you post the code, or not. It's your script, it's your decision. :)

    But just so you know, the way to "prevent hacking" is not to "hide" your code, but to "improve" it... ;)

    Let's finish this topic here. :)
    Bye
     
    JEET, Dec 3, 2006 IP