Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 11 of 11
  1. #1
    New Coder
    Join Date
    Nov 2011
    Posts
    59
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Blocking access or "die" action, when id in url is equal to X...

    My URL looks like this:
    http://www.example.com/example.php?action=edit&id=35

    I want to add to example.php a command that will block access to the page or will have a "die" command, when id = 35 in the URL.

    When it is a different id, the page stays the same as it was.

    I thought of using $_GET['id']

    PHP Code:
    <?php
    $id 
    $_GET['id']

    if (
    $id == "35") {
    die;
    } else {
    // do nothing
    }
    ?>
    Can you suggest?

  • #2
    Senior Coder djm0219's Avatar
    Join Date
    Aug 2003
    Location
    Wake Forest, North Carolina
    Posts
    1,294
    Thanks
    4
    Thanked 203 Times in 200 Posts
    Did you try that? If you did did it not do what you wanted?
    Dave .... HostMonster for all of your hosting needs

  • #3
    New Coder
    Join Date
    Nov 2011
    Posts
    59
    Thanks
    2
    Thanked 0 Times in 0 Posts
    I didn't try. I wanted to know if that's alright and if you think it is the best way to do what I want in the first place - blocking access or just not running the rest of the code ("die")

  • #4
    New Coder
    Join Date
    Nov 2011
    Posts
    59
    Thanks
    2
    Thanked 0 Times in 0 Posts
    is it written correctly?

  • #5
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    No its not, you have a syntactical error and you haven't checked for if the get actually exists. I assume if no get exists that it will continue as normal.
    If you are using hard coded values, I'd use an in_array instead. That will make it easier to add things if necessary.
    PHP Code:
    <?php

    $aDie 
    = array(35);

    if (isset(
    $_GET['id']) && in_array($_GET['id'], $aDie))
    {
        die(
    'invalid id');
    }

    // process stuff.

  • Users who have thanked Fou-Lu for this post:

    tomystein (03-04-2012)

  • #6
    New Coder
    Join Date
    Nov 2011
    Posts
    59
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Thanks for the info - it works.
    A simple question: How do I write, all IDs smaller than 35?

    I want to do "invalid id" when it's smaller than X ID.

  • #7
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    Quote Originally Posted by tomystein View Post
    Thanks for the info - it works.
    A simple question: How do I write, all IDs smaller than 35?

    I want to do "invalid id" when it's smaller than X ID.
    That would require an if instead of an in_array.
    PHP Code:
    if ($value <= 35)
    {
        
    # its less than or equal to 35


  • #8
    New Coder
    Join Date
    Nov 2011
    Posts
    59
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Can you help me and write the new code please? I am a bit confused..

    Thank you.

  • #9
    New Coder
    Join Date
    Nov 2011
    Posts
    59
    Thanks
    2
    Thanked 0 Times in 0 Posts
    PHP Code:
    <?php 

    $value 
    '35'

    if (isset(
    $_GET['id']) && ($_GET['id'], $value <= 35)) 

        die(
    'invalid id'); 


    // process stuff.
    ??
    Last edited by tomystein; 03-05-2012 at 01:55 PM. Reason: forgot something

  • #10
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    No, that would be:
    PHP Code:
    if (isset($_GET['id']) && $_GET['id'] <= 35

  • #11
    New Coder
    Join Date
    Nov 2011
    Posts
    59
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Thanks a lot!


  •  

    Posting Permissions

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