SitePoint Sponsor

User Tag List

Results 1 to 4 of 4

Thread: $_GET problem

  1. #1
    SitePoint Member
    Join Date
    Feb 2006
    Posts
    10
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    $_GET problem

    Hi All,

    I am having a problem getting the value of a variable in a query string from my PHP files....based on php rules everything should be working fine, but for unknown reason to me I simply can't get the query string variable value.

    So I will appreciate it so much if someone can please help me find out what is exactly wrong

    On a minor note, I am using smarty template engine + Apache mod_rewrite.


    search_box.tpl
    <table width="100%" border="0" cellspacing="0" cellpadding="0">
    <tr>
    <form name="frmsearch" id="frmsearch" method ="get" >
    <td>
    <input type="text" name="searchterm" value=""/>
    </td>
    <td>
    <input type="submit" value="Search"/>
    </td>
    </form>
    </tr>
    </table>


    index.php

    //I tried once....

    if($searchterm)
    { echo $searchterm;
    echo 'found it';
    }
    else
    { echo $_REQUEST['searchterm'];
    echo 'Error';
    }
    exit;

    //Then tried ....

    if($_GET['searchterm'])
    { echo $searchterm;
    echo 'found it';
    }
    else
    { echo $_REQUEST['searchterm'];
    echo 'Error';
    }
    exit;


    //Then tried ....

    if($_REQUEST['searchterm'])
    { echo $searchterm;
    echo 'found it';
    }
    else
    { echo 'Error';
    }
    exit;


    And they all gave the same result...show the text Error

    On a minor note, I am using Apach mod_rewrite as follows
    RewriteRule ^(.*)-LD-([0-9]+)-P([0-9]+)\.html$ index.php?id=$2&cat=$1&page=$3 [L]

    And search_box.tpl appear in a URL
    http://....../Category-12-LD-5.html

    TO

    http://....../Category-12-LD-5.html?searcterm=hello world


    That is all the info I have...

    Thanks in advance for your help and cooperation

  2. #2
    SitePoint Wizard silver trophy
    Join Date
    Mar 2006
    Posts
    6,132
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i ran into that problem as well.
    if you change it to
    Code:
    RewriteRule ^(.*)-LD-([0-9]+)-P([0-9]+)\.html$ index.php
    then the normal query string will be available to php. seems that when you force your own query string in the rewrite rule, it overwrites the query string the browser sent.
    personally, thats what i did, and then i just get what i want and parse it out using php, using $_SERVER['REQUEST_URI']




    you might be able to get fancy and add the normal query string back in like this though
    Code:
    RewriteRule ^(.*)-LD-([0-9]+)-P([0-9]+)\.html$ index.php?%{QUERY_STRING}&id=$2&cat=$1&page=$3 [L]
    the reason i put %{QUERY_STRING} in before the other variables, and not at the end, is so if the user passes ?id=foo, your id wont get overwritten.

    so now you need to remember you cant use id, cat, or page as variables anymore. if thats a problem, do what i did and just parse the REQUEST_URI using php instead of setting variables in the rewrite rule.


    another option, is to pass those variables to php as enviornment variables, so they wont collide with your normal _GET variables, which you can do with htaccess. i forget the syntax though. then they will be available in the php $_ENV array

  3. #3
    SitePoint Member
    Join Date
    Feb 2006
    Posts
    10
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    clamcrusher,

    Thanks a lot for your help and support...the problem is totally solved

    I've used this solution, and it solved the problem instantly

    RewriteRule ^(.*)-LD-([0-9]+)-P([0-9]+)\.html$ index.php?%{QUERY_STRING}&id=$2&cat=$1&page=$3 [L]

    Just one last question, can you please elaborate more what did you mean by "the reason i put %{QUERY_STRING} in before the other variables, and not at the end, is so if the user passes ?id=foo, your id wont get overwritten."

    Once again thanks a lot for your help and support

  4. #4
    SitePoint Wizard silver trophy
    Join Date
    Mar 2006
    Posts
    6,132
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    if i did it this way
    Code:
    RewriteRule ^(.*)-LD-([0-9]+)-P([0-9]+)\.html$ index.php?id=$2&cat=$1&page=$3%{QUERY_STRING} [L]
    then if you went to
    http://....../Category-12-LD-5.html?id=foo&cat=foo&page=foo

    guess what echo $_GET['id'] is gonna output?
    foo


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
  •