PHP - include() file not working when variables are put in url?

后端 未结 5 1657
梦毁少年i
梦毁少年i 2021-01-13 18:28

In PHP I\'ve build a webpage that uses include() for loading parts of the website. However, I now ran into something like a problem: When I use an url like: data-openo

相关标签:
5条回答
  • 2021-01-13 19:06

    include in this way doesn't fetch a URL, it fetches a file from the filesystem, so there's no such thing as a query string.

    You can do this, though:

    $_GET['type'] = 'actueel';
    include('data-storingen.php');
    
    0 讨论(0)
  • 2021-01-13 19:16

    Unless you've got a fully qualified URL, with protocol:// in the address, PHP will interpret what you pass into include()/require() as a LOCAL file, which means it's looking for a file on your drive whose name real is data-storingen.php?type=actueel, whereas you've only got data-storingen.php.

    Since you're dealing with a local file, there is no support for query strings, and you have to strip that out of the "filename" you're passing to include().

    0 讨论(0)
  • 2021-01-13 19:20

    You could/should always set the variables outside, since you can't do this via the URL....

    $_GET['type'] = "actueel";
    include("data-storingen.php");
    

    Then the included file can access the variables (assuming you use $_GET['type'] in the included file)

    0 讨论(0)
  • 2021-01-13 19:21

    No it is not possible to pass variables like that. You can use the variable actueel inside of data-storingen.php though, as long as it is declared in the page you are including it from, before the include statement.

    Think of including as copy-pasting the code from the included file into the current file. So you can have a file:

    $actueel = 'abc';
    include(data-storingen.php);
    

    And then your in data-storingen.php:

    echo $actueel;
    

    And it will output 'abc'.

    0 讨论(0)
  • 2021-01-13 19:22

    The include() function does not access the file via HTTP, it accesses the file through the OS's own file system. So GET variables are not counted. (as they are not part of the file name).

    In layman's terms, the point of include is to "copy/paste" all the contents on one file to another on the file, so that you don't have one gigantic file, but a few smaller, more maintainable ones.

    0 讨论(0)
提交回复
热议问题