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.
Page 1 of 2 12 LastLast
Results 1 to 15 of 16
  1. #1
    Regular Coder
    Join Date
    Dec 2007
    Posts
    269
    Thanks
    28
    Thanked 0 Times in 0 Posts

    big problem with array and global switch

    i have following link in y.php i click it after open x.php
    Code:
    <a href="x.php?p='.1.':'.5.'">'.click.'</a>
    when i clik x.php?do=send link in x.php i can not print $id result

    PHP Code:
    $id explode(":",$_POST["p"]);
    switch (
    $_REQUEST['do']){
            case 
    "send":            
                echo 
    $id[0];
            break;

    where is my mistake ?

  • #2
    Senior Coder TheShaner's Avatar
    Join Date
    Sep 2005
    Location
    Orlando, FL
    Posts
    1,126
    Thanks
    2
    Thanked 40 Times in 40 Posts
    First, your $_POST should be a $_GET because $_GET is used for retrieving values passed in query strings and $_POST is only used for forms with method="post".

    Second, do not use $_REQUEST if you can avoid it. Use either $_GET, $_POST, or $_COOKIE. I'm not sure where do is coming from, so I can't make an appropriate suggestion.

    -Shane

  • #3
    Regular Coder
    Join Date
    Dec 2007
    Posts
    269
    Thanks
    28
    Thanked 0 Times in 0 Posts
    y.php
    PHP Code:
    <?php
    echo '<a href="x.php?p=5">click</a>';
    x.php
    PHP Code:
    <a href="x.php?do=send" target="_self">click</a>

    <?php
    $id 
    $_REQUEST["p"];
    switch (
    $_REQUEST['do']){
        case 
    "send":
            echo 
    $id;
        break;
            }

  • #4
    Regular Coder funnymoney's Avatar
    Join Date
    Aug 2007
    Posts
    364
    Thanks
    17
    Thanked 24 Times in 24 Posts
    try using variable variables

    http://www.php.net/variables.variable

  • #5
    Senior Coder TheShaner's Avatar
    Join Date
    Sep 2005
    Location
    Orlando, FL
    Posts
    1,126
    Thanks
    2
    Thanked 40 Times in 40 Posts
    Quote Originally Posted by zodehala View Post
    y.php
    PHP Code:
    <?php
    echo '<a href="x.php?p=5">click</a>';
    x.php
    PHP Code:
    <a href="x.php?do=send" target="_self">click</a>

    <?php
    $id 
    $_REQUEST["p"];
    switch (
    $_REQUEST['do']){
        case 
    "send":
            echo 
    $id;
        break;
            }
    You offer no explanation on how your page works and just repost code expecting us to just code it for you. Well, guess what? I'm not going to do that because I'm tired of people not trying to help themselves.

    Here's your nudge in the right direction:

    If the person is redirected to x.php from y.php and then the user is supposed to click the link in x.php to activate your switch statement, then your problem is that when they click the link, the value for p is lost, thus so is $id. Have your link in x.php also have p=5 and change your REQUESTs to GETs.

    -Shane

  • #6
    Senior Coder
    Join Date
    Jan 2007
    Posts
    1,648
    Thanks
    1
    Thanked 58 Times in 54 Posts
    Quote Originally Posted by TheShaner View Post
    Second, do not use $_REQUEST if you can avoid it. Use either $_GET, $_POST, or $_COOKIE.
    I've never gotten a good reason from someone why one shouldn't use $_REQUEST. It's dynamic in that it allows you to switch from POST to GET requests (or have a form and a link).

  • #7
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,642
    Thanks
    2
    Thanked 405 Times in 397 Posts
    Quote Originally Posted by aedrin View Post
    It's dynamic in that it allows you to switch from POST to GET requests (or have a form and a link).
    That's also the problem(in my opinion). The default variables_order(or request_order) is "GPCS", so if you're trying to use a form input that is named the same as a cookie/session variable(accidentally or otherwise) it will be overridden with the other value and you wouldn't necessarily want that or expect it.

  • #8
    Senior Coder TheShaner's Avatar
    Join Date
    Sep 2005
    Location
    Orlando, FL
    Posts
    1,126
    Thanks
    2
    Thanked 40 Times in 40 Posts
    Quote Originally Posted by aedrin View Post
    Quote Originally Posted by TheShaner View Post
    Second, do not use $_REQUEST if you can avoid it.
    I've never gotten a good reason from someone why one shouldn't use $_REQUEST. It's dynamic in that it allows you to switch from POST to GET requests (or have a form and a link).
    First, if you have the same index in COOKIE as in POST, REQUEST returns the value of COOKIE. The order is COOKIE, POST, and GET for returning matching indexes. Second, using POST, GET, and COOKIE allow you to keep track of where you are grabbing your information, thus allowing future markups and editing to be conducted easier, esp. by others that may take over your work.

    With the above considerations and really no advantage (in my opinion) to using REQUEST, I see no reason to even use it. Why chance ruining your code when you meant for $_REQUEST['user_id'] to return the value found in your query string and not in your cookie???

    -Shane

  • #9
    Regular Coder funnymoney's Avatar
    Join Date
    Aug 2007
    Posts
    364
    Thanks
    17
    Thanked 24 Times in 24 Posts
    Quote Originally Posted by aedrin View Post
    I've never gotten a good reason from someone why one shouldn't use $_REQUEST. It's dynamic in that it allows you to switch from POST to GET requests (or have a form and a link).
    there is perfectly logical explanation why to use $_REQUEST more then now-and-then in a script.

  • #10
    Regular Coder
    Join Date
    Dec 2007
    Posts
    269
    Thanks
    28
    Thanked 0 Times in 0 Posts
    according to 3.post

    first i clickink in y.php than click link in x.php bu i can not print value (5) why?

  • #11
    Senior Coder TheShaner's Avatar
    Join Date
    Sep 2005
    Location
    Orlando, FL
    Posts
    1,126
    Thanks
    2
    Thanked 40 Times in 40 Posts
    If you know how to get the value (5) from y.php, then you should be able to create the link in x.php to have p=5. Think about it a little.

    -Shane

  • #12
    Regular Coder
    Join Date
    Dec 2007
    Posts
    269
    Thanks
    28
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by TheShaner View Post
    If you know how to get the value (5) from y.php, then you should be able to create the link in x.php to have p=5. Think about it a little.

    -Shane
    i send value (5) via link <a href="x.php?p=5"> and i get value(5) using $_REQUEST

  • #13
    Senior Coder TheShaner's Avatar
    Join Date
    Sep 2005
    Location
    Orlando, FL
    Posts
    1,126
    Thanks
    2
    Thanked 40 Times in 40 Posts
    Quote Originally Posted by zodehala View Post
    i send value (5) via link <a href="x.php?p=5"> and i get value(5) using $_REQUEST
    Correct, and now using $_REQUEST, you can dynamically create the link found in x.php to not only contain do=send, but p=5 also, so that your link looks like:
    Code:
    <a href="x.php?p=5&do=send" target="_self">click</a>
    -Shane

  • #14
    Senior Coder
    Join Date
    Jan 2007
    Posts
    1,648
    Thanks
    1
    Thanked 58 Times in 54 Posts
    The default variables_order(or request_order) is "GPCS", so if you're trying to use a form input that is named the same as a cookie/session variable(accidentally or otherwise)
    Although variables_order affects the order of processing, it does not determine the specific globals that are included. That is controlled by something else (depending on the PHP version). By default it only includes GET, POST and COOKIE.

    Quote Originally Posted by TheShaner View Post
    First, if you have the same index in COOKIE as in POST, REQUEST returns the value of COOKIE. The order is COOKIE, POST, and GET for returning matching indexes.
    If you properly name your cookies, this shouldn't really be an issue.

    Then again, I don't really use cookies so it's not a problem for me. And I suppose it might be an issue for others.

    Quote Originally Posted by TheShaner View Post
    thus allowing future markups and editing to be conducted easier
    REQUEST is input from outside. It shouldn't matter where it comes from.

    The JSP standard only offers something similar to request, and it works quite well (granted that it does not include cookies). Considering the existing userbase and the maturity compared to PHP (mostly enterprise use), I'd say that would be a pretty good support for it.

    The benefit to me is portability, in that my script will take inputs from multiple sources and I don't have to search&replace my script to switch between the two. That alone for me is worth the slight chance that at some point a cookie might interfere with my inputs. But since I control what cookies are available to my site, it should be fine.

    In the end, it is mostly about opinion. And it is better left untouched.

  • #15
    Regular Coder
    Join Date
    Apr 2008
    Location
    New Jersey
    Posts
    116
    Thanks
    0
    Thanked 29 Times in 29 Posts
    Quote Originally Posted by aedrin View Post
    But since I control what cookies are available to my site, it should be fine.
    I think it's worth emphasizing that cookies, like everything else that comes from the user, are not actually under your control. A user can send you whatever cookies they feel like, including ones you never sent. As long as, like you say, cookies are treated as external information with no level of trust, just like GET and POST data, this isn't a problem. It's unfortunate when people desecure their sites by believing that cookies 'belong' to them, though.
    Chaos
    Lost Souls: text based RPG | MUDseek: MUD gaming search | MUDfind: MUD resource sites | Discordian Quotes


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

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