SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Evangelist dmsuperman's Avatar
    Join Date
    Feb 2005
    Location
    A box
    Posts
    516
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    $_GET[$a]; doesn't work, but $a is a defined variable.

    $prefsFile = file("prefs.txt");
    $a = $prefsFile[0];
    $action = $_GET[$a];

    That is a snippet of a code I am making, and I can say I'm not PHP stupid but this is really perplexing me. $prefsFile works fine, $a even works, but $action doesn't. I tried echoing it but it didn't work. Currently $a is defined as action, and when I go to ?action=meh it won't echo meh or anything. Any ideas?

  2. #2
    SitePoint Addict launchcode's Avatar
    Join Date
    Dec 2004
    Location
    Bristol, UK
    Posts
    259
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    <?php
        $a 
    'action';
        
    $action $_GET[$a];
        
    print_r($_GET);
        echo 
    $action;
    ?>
    Works fine for me - I don't think there is anything wrong with the code you posted - could the error come from somewhere else? Dump out the values of $_GET and see what it contains and then dump out the $prefsFile array to see what that contains, just to double-check (print_r() will output an array).
    Richard Davey

    Launchcode
    PHP Security Guide. Think your scripts are secure? Think again.

  3. #3
    SitePoint Evangelist dmsuperman's Avatar
    Join Date
    Feb 2005
    Location
    A box
    Posts
    516
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm not really sure what that does, but this is what it outputs when going to action=hello:
    Array ( [action] => hello ) hello

    I can post the whole code if you want, maybe it's something else. Sorry if I'm causing any problems but I'm a horrible debugger, especially in PHP which I'm fairly new to.

  4. #4
    SitePoint Evangelist dmsuperman's Avatar
    Join Date
    Feb 2005
    Location
    A box
    Posts
    516
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I figured it out, but it was pretty stupid. I was unaware that using file() left the \n's in there, so it wasn't working because I wasn't going to action\n=

    I changed file() to explode("\n", file_get_contents());

  5. #5
    SitePoint Addict launchcode's Avatar
    Join Date
    Dec 2004
    Location
    Bristol, UK
    Posts
    259
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Rather than explode you could trim() the variable, same end result, less processing.
    Richard Davey

    Launchcode
    PHP Security Guide. Think your scripts are secure? Think again.


Bookmarks

Posting Permissions

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