Good day. Such a problem: I found an engine wrapper on the Internet, which initially seemed convenient to me, but when I approached writing new modules, I needed to use get-requests. I give the code:

if ( $_SERVER['REQUEST_URI'] == '/' ) $page = 'home'; else{ $page = substr($_SERVER['REQUEST_URI'], 1); if ( !preg_match('/^[A-z0-9]{3,15}$/', $page) ) not_found(); } $CONNECT = mysqli_connect('localhost', 'root', '', 'test'); if ( !$CONNECT ) echo('MySQL error'); if ( file_exists('all/'.$page.'.php') ) include 'all/'.$page.'.php'; else if ( $_SESSION['id'] and file_exists('auth/'.$page.'.php') ) include 'auth/'.$page.'.php'; else if ( !$_SESSION['id'] and file_exists('guest/'.$page.'.php') ) include 'guest/'.$page.'.php'; else not_found(); 

This excerpt from the code page index.php, which is responsible for the pars data from the browser line and the subsequent comparison with other conditions that connect the page. When I register a link to an existing page with absolutely any get-request, I am transferred to page 404 (which is understandable, it can be seen by the code), if I register a link to the same page without GET, everything works, except for, respectively, GET requests .

The question is: what should I add to get get requests in the browser line?

    2 answers 2

    Get rid of the get parameters that in $_SERVER['REQUEST_URI'] follow the sign ? .

    For example:

     $page = '/admin?fff=1'; $tmp_arr = explode('?', $page, 2); $page = $tmp_arr[0]; var_dump($page); 

    As a result on the screen:

     string(6) "/admin" 

    Or so

     $page = '/admin?fff=1'; $length = strcspn ($page, '?'); $page = substr($page, 0, $length); var_dump($page); 

    Or so

     $page = '/admin?fff=1'; $page = strstr($page, '?', true); // PHP 5.3+ var_dump($page); 
    • one
      strstr('/admin?fff=1', '?', true); => /admin ;) PS: PHP 5.3.0 + - E_p
    • Thank you, this is what you need. I implemented it like this: I just inserted the first method instead of the original function substr with assignment. - Alexey S.
    • @E_p, you had to issue a response. And my memory is full of holes: ( - Visman

    Like this:

     if ($_SERVER['REQUEST_URI'] == '/') $page = 'home'; else{ $page = substr($_SERVER['REQUEST_URI'], 1); $length = strcspn ($page, '?'); $page = substr($page, 0, $length); //var_dump($page); if(!preg_match('/^[A-z0-9]{3,15}$/', $page)){ not_found(); } }