Jump to content

[SOLVED] "include" to noob


Mr_J

Recommended Posts

Ok, I`m learning:

I have created a LIBRARY File called tax.php. That only contains add_tax and add_tax_rate functions in the php tags.

Now, I want to incorporate tax.php in my load20.php.


include "tax.php" does not work so I used include ('http://medbel.co.za/jaco/sams2/tax.php')


code:

<?php

$price = 95;

include ('http://medbel.co.za/jaco/sams2/tax.php') //absolute location

echo "Price before tax: $price <br>";

echo "Price after tax: ";

echo add_tax($price);

?>

 

Link to comment
Share on other sites

include 'tax.php'; should work. Just make sure both files are in the same folder, or use ../ or foldername1/foldername2/ etc.

 

There is also a problem with tax.php itself:

 

Parse error: syntax error, unexpected ';', expecting '{' in /usr/www/users/medbqq/jaco/sams2/tax.php on line 2

 

so make sure you fix that as it may be the problem.

Link to comment
Share on other sites

The err:

Warning: main() [function.main]: URL file-access is disabled in the server configuration in /usr/www/users/medbqq/jaco/sams2/load20.php on line 2

 

Warning: main(http://medbel.co.za/jaco/sams2/tax.php) [function.main]: failed to open stream: no suitable wrapper could be found in /usr/www/users/medbqq/jaco/sams2/load20.php on line 2

 

Warning: main() [function.include]: Failed opening 'http://medbel.co.za/jaco/sams2/tax.php' for inclusion (include_path='.:/usr/share/php') in /usr/www/users/medbqq/jaco/sams2/load20.php on line 2

Price beforer tax: 95 Price after tax:

Fatal error: Call to undefined function: add_tax() in /usr/www/users/medbqq/jaco/sams2/load20.php on line 6

 

The CODE:

<?php

include ('http://medbel.co.za/jaco/sams2/tax.php');

$amount = 95;

echo "Price beforer tax: $amount";

echo " Price after tax: ";

echo add_tax($amount);

echo "To incorporate an external library file into another script: use INCLUDE <br>";

print "The following includes tax.php so I can call add_tax <br>";

 

?> ???

 

Link to comment
Share on other sites

Including a file using a URL does NOT include the php code. It only includes any content that is output by that file. You must use a file system path to include php code. A URL is not an absolute file system path.

What is the "system path" then? root perhaps?

Link to comment
Share on other sites

<?php
include ("tax.php");
$amount = 95;
echo "Price beforer tax: $amount";
echo " Price after tax: ";
echo add_tax($amount);
echo "To incorporate an external library file into another script: use INCLUDE
";
print "The following includes tax.php so I can call add_tax
";

?>

 

WILL work assuming tax.php is in the same directory you're calling load20.php from

 

The system path is just where the file resides on the server. This is most always different on every machine. If you're unsure of your servers configuration simply running the following code in the same directory as load20.php will tell you the full system path to that file

 

<?php
echo getcwd();
?>

 

However absolute paths aren't required in most cases and a relative path will suffice, for example

 

if tax.php is in the same folder as load20.php you can just call include("tax.php");

 

if tax.php is in a folder ABOVE the folder load20.php is in, you get to it with include("../tax.php");

 

if it's BELOW the folder load20.php is in you include it with include("/folder/tax.php");

 

I can tell you right now the full path is "/usr/www/users/medbqq/jaco/sams2/" from your error you posted before, but like I said, you dont need it.

Link to comment
Share on other sites

A file system path is a path that has meaning to the file system used by the operating system on the server.

 

An absolute file system path can be formed using $_SERVER['DOCUMENT_ROOT'] and your path/filename -

 

include($_SERVER['DOCUMENT_ROOT'] . "/your_path/your_file.php");

Link to comment
Share on other sites

Thanks to all of you  :)


This is the code for tax.php:

<?php

function add_tax($amount) {

$total = $amount * 1.14;

return $total;

}

?>


This is the code for load20.php:

<?php

include ("tax.php");

$amount = 95;

echo "Price beforer tax: $amount <br>";

echo " Price after tax: <br>";

echo add_tax($amount);

echo "To incorporate an external library file into another script: use INCLUDE <br>";

echo "The following includes tax.php so I can call add_tax";

?>


Hope it helps someone in the future...

tax.php is in the same folder as load20.php

http://medbel.co.za/jaco/sams2/load20.php

I just made a small thingie for my own reference...

Cheers

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.