-4

I am trying to set up some php includes on a website and I cannot get them to work. The test codes do not echo in the index.php. If someone can see an error in the code I would appreciate it. Thanks

codes.php

<?php

$test1 = '123456';
$test2 = '1234567';
$test3 = '12345678';

?>

index.php

<html>
<head>
<title>Testing</title>
</head>
<body>

<span><?php include 'http://www.mydomain.com/includes/codes.php'; echo "$test1"; ?></span>
<span><?php include 'http://www.mydomain.com/includes/codes.php'; echo "$test2"; ?></span>
<span><?php include 'http://www.mydomain.com/includes/codes.php'; echo "$test3"; ?></span>

</body>
</html>
Paul
  • 375
  • 2
  • 6
  • 10

5 Answers5

3

If you include the file as http://www.mydomain.com/includes/codes.php, PHP will see exactly the same as when you go to http://www.mydomain.com/includes/codes.php in a browser: nothing.

You need to include a local file: include 'includes/codes.php';

You also don't need to include the same file multiple times.

deceze
  • 510,633
  • 85
  • 743
  • 889
1

You are not supposed to use absolute path, you should use relative instead. I would rather use __DIR__

The directory of the file. If used inside an include, the directory of the included file is returned. This is equivalent to dirname(FILE). This directory name does not have a trailing slash unless it is the root directory. (Added in PHP 5.3.0.)

Fabio
  • 23,183
  • 12
  • 55
  • 64
1

Place the <?php include('..'); ?> at the top of the file, then you will be able to echo out each variable accordingly. You do not need to include the same location multiple times.

Dylan N
  • 527
  • 3
  • 13
1

I would suggest you to have a look at this SO question: Should I allow 'allow_url_fopen' in PHP?

You are trying to access external php files from include which is a vulnerability issue and MUST be avoided.

Community
  • 1
  • 1
Lenin
  • 570
  • 16
  • 36
1

If you use include with a URL (http), the Server will first interpret the file and you will get the output. Therefore you cannot access the variables you have defined in your codes.php.

If your codes.php is in the same directory as your index.php, you can include it via

 include(dirname(__FILE__) . DIRECTORY_SEPARATOR . "codes.php";

or

 include('codes.php')

FILE is a magic variable within PHP that uses the path of the current file. This comes in handy if you include a file which again has an include (those kind of includes can make a mess if you use static file pathes). DIRECTORY_SEPARATOR uses "/" with Linux Servers and "\" with windows servers.

Matthias S
  • 3,358
  • 3
  • 21
  • 31