#1
  1. meester luva luva
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Posts
    2,715
    Rep Power
    25

    How to assign either a post or get var to a variable?


    I have a form that can either be submitted by post or get, so how do I do

    $var = $_POST["var"] || $_GET["var"]

    ?

    Or is that it?
  2. #2
  3. Retired
    Devshed Supreme Being (6500+ posts)

    Join Date
    Feb 2002
    Location
    Finland
    Posts
    9,143
    Rep Power
    2493
    PHP Code:
    $var $_REQUEST["var"]; 
    This also includes $_COOKIE, $_SESSION etc, but should do the trick.
    Last edited by jabba_29; April 12th, 2006 at 06:52 AM. Reason: damn BBcode extension ;)

    Cheers,
    Jamie

    >_ skiFFie ? | Twitter

    __________________

    Let the might of your compassion arise to bring a quick end
    to the flowing stream of the blood and tears .....
    Please hear my anguished words of truth.

    __________________
  4. #3
  5. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jan 2006
    Location
    India
    Posts
    857
    Rep Power
    548
    If you are talking about $_COOKIE, $_SESSION etc then It should be
    PHP Code:
    $var $_REQUEST["var"]; 
    instead of
    PHP Code:
    $var $_POST["var"]; 
    edit: above post is edited, POST is changed to REQUEST
  6. #4
  7. I fail at spelling
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Sep 2003
    Location
    NDAuNjIxMTExLC03OS4xNTU=
    Posts
    3,232
    Rep Power
    1774
    Originally Posted by jabba_29
    PHP Code:
    $var $_REQUEST["var"]; 
    This also includes $_COOKIE, $_SESSION etc, but should do the trick.
    I thought I read somewhere that it's bad to use $_REQUEST. I don't know where I saw that, but maybe it was about something else.

    Chad
    I am working now with Symfony2, Twig, Doctrine, Composer, Assetic, and HTML5. Enjoying doing what I do everyday!
  8. #5
  9. No Profile Picture
    Redpill
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Nov 2005
    Posts
    1,660
    Rep Power
    151
    Not very good idea to use $_REQUEST. If a bad user knew your script used a POST var called "mysql", he could simply try out "myscript.php?mysql=123" and who knows what THAT would do


    Personally, I'd prefer this. Let's assume you want to give preference to COOKIE values if there is any duplication, then POST, then last of all GET.

    PHP Code:
    $request = array();
    //in reverse order
    foreach($_GET as $key => $val)
    {
        
    $request[$key] = $val;
    }
    foreach(
    $_POST as $key $val)
    {
        
    $request[$key] = $val;
    }
    foreach(
    $_COOKIE as $key => $val)
    {
        
    $request[$key] = $val;

    Comments on this post

    • helloakash agrees : perfect (5)
    Code:
    #include <stdio.h>
    int main(int o,char**O){return o>-1?o-2||!main(-1,1+O)?!!fprintf(stderr,"%s [0-"
    "9]{81}\n",*O):main(-83,++O):o>-83?(*O)[-1-o]?81==(o=-o-1)||o[*O]<'0'||'9'<o[*O]
    ?0:main(-2-o,O):o==-82:o>-164?(*O)[-83-o]<'1'?main(o-82,O):main(--o,O):o+164?o>-
    246?(*O)[-165-o]<'1'?main(o-82,O):main(--o,O):o+246?o>-328?(*O)[o=-o-247]<='8'?(
    main(-328-o,(++o[*O],O)),main(-247-o,O)):!(o[*O]='0'):(o=-o-328)<729?(o%9/3*3-o%
    27+o/243*9+o/81%3&&(*O)[o%81]==(*O)[o%81-o%27+o%9/3*3+o/243*9+o/81%3])||(o%81-o%
    9-o/81*9&&(*O)[o%81]==(*O)[o%9+o/81*9])||(o/81-o%9&&(*O)[o%81]==(*O)[o%81-o%9+o/
    81])?0:main(-409-o,O):main(-165-o%81,O):!puts(*O):0                           ;}
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2010
    Posts
    7
    Rep Power
    0
    Another way is:

    PHP Code:
    if(isset($_GET["var"]))
      
    $myvar=$_GET["var"];
    else
      
    $myvar=$_POST["var"]; 
  12. #7
  13. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,957
    Rep Power
    1046
    Congratulations. I'm sure this will really help them 7 years later.
    The 6 worst sins of security ē How to (properly) access a MySQL database with PHP

    Why canít I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".

IMN logo majestic logo threadwatch logo seochat tools logo