2

I have the following code when I want to include some code from another PHP file:

$domain = 'www.example.com';
require_once $domain.'/footer.php');

I wonder though if this is slower than it could be, because surely this then has to go through a DNS to find the page?

Is there a quicker way to do this? Maybe I should set my IP as $domain? Or maybe there is some PHP snippet that will detect it for me?

I also pay for DNS hits, so there's that too...

Amy Neville
  • 10,067
  • 13
  • 58
  • 94
  • templates are made there fore – donald123 Jun 26 '15 at 10:09
  • Are these files you're trying to require not on the same server then? – Jonnix Jun 26 '15 at 10:10
  • 4
    Including code from a remote server is seriously dangerous. –  Jun 26 '15 at 10:10
  • No they are on the same server - same folder actually... – Amy Neville Jun 26 '15 at 10:11
  • @AmyNeville So what's wrong with `require_once 'footer.php';`? – Jonnix Jun 26 '15 at 10:12
  • @JonStirling I use a .htaccess file with rewrite rules. So it thinks the file is somewhere else. – Amy Neville Jun 26 '15 at 10:13
  • It should be noted that the provided code btw doesn't go over the network, its looking for the file `www.example.com/footer.php` on your local file system. – Jonnix Jun 26 '15 at 10:13
  • 1
    @AmyNeville You're assuming things about how rewrite rules work. `include` / `require` (unless you actually use a fqdn, which you really shouldn't) don't go through apache, so the rules don't matter. – Jonnix Jun 26 '15 at 10:14
  • 1
    `include` and `require` should both only (really) be used over the filesystem - there shouldn't be any DNS lookup. Incidentally use `require` only if failure to retrieve the file would break your application (a required class for instance) - otherwise use `include` (PHTML/PHP template or view files for instance ... the app will still work but it'll look wonky)... `require` will terminate your script whereas `include` gives you an error that you can deal with. – CD001 Jun 26 '15 at 10:15

1 Answers1

1

No thought You can include file via autoloader but there is no other way to include files than

Require and include in php and both are fast, even if you use temples it would be same.

Richerd fuld
  • 364
  • 1
  • 10
  • 23