SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Guru
    Join Date
    Dec 2005
    Posts
    982
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Form Action: PHP_SELF, REQUEST_URI or what?

    So in the past, it seems like I've always used PHP_SELF as the action of a form. This seems to work fine for me, but I'm always curious for the "best way" to do stuff. I thought about using REQUEST_URI, but I didn't see any advantage, and the best I can tell, that env variable doesn't exist in the CLI, so it may be unreliable.

    I also thought of writing a small function and tucking it away like so:
    PHP Code:
    <?php
    //...
    if (isset($_SERVER['REQUEST_URI'])) $uri .= $_SERVER['REQUEST_URI'];
    else 
    $uri .= $_SERVER['PHP_SELF'] . ((!empty($_SERVER['QUERY_STRING'])) ? '?' $_SERVER['QUERY_STRING'] : '');

    return 
    $uri;
    But I'm basically just using the env variables, so not much is gained I guess.

    Any input or thoughts? I don't have a framework available, so I'm hoping for a short, sweet, pure php idea.
    MySQL v5.1.58
    PHP v5.3.6

  2. #2
    SitePoint Evangelist simshaun's Avatar
    Join Date
    Apr 2008
    Location
    North Carolina
    Posts
    438
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP_SELF will not work if you are using mod_rewrite for friendly urls. PHP_SELF will simply return the script name.

    REQUEST_URI is the workaround of course. And I don't see anything in the manual for REQUEST_URI that says it isnt available in CLI. May be worth testing.

    If the environment variables are not set for you, then you may need to check your variables_order setting in php.ini to ensure E exists in it.

  3. #3
    SitePoint Guru
    Join Date
    Dec 2005
    Posts
    982
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yeah, that's one of the reasons I was trying to get away from PHP_SELF. I use a lot of redirects and I didn't like that my forms were showing the underlying file structure at times (though it seems sporadic in when it shows the redirect and now; probably based on 301 vs Rewrite?). I'm looking at REQUEST_URI now, and it seems like a good bet, but I wonder what kind of bugs I'd run into when the form is set to using a GET method... Does anyone have any experience or ideas for testing that out?

    And as a footnote, it appears that my local server doesn't include REQUEST_URI in the $_SERVER array if I use the CLI ("C:\Program Files\PHP\php.exe" -f serverinfo.php).
    MySQL v5.1.58
    PHP v5.3.6

  4. #4
    SitePoint Wizard
    Join Date
    Mar 2008
    Posts
    1,149
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You should strip away the query portion of REQUEST_URI if you're going to use it.

    REQUEST_URI wouldn't appear in CLI because that's an Apache-specific environmental variable. It's not defined in CGI, and IIS doesn't have it. I don't think there is an equivalent.

  5. #5
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by sk89q View Post
    ...It's not defined in CGI, and IIS doesn't have it...
    Actually IIS does have it. IIS 7 + FastCGI, I have _SERVER["REQUEST_URI"] Could just be my configuration too, unsure.

    To the OP, why can you not hard code the form to submit to a fixed location?
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.



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
  •