Welcome to WebmasterWorld Guest from 54.144.206.214

Forum Moderators: coopster & jatar k

Newbie question about includes

Will variables from the main page be used in the include pages?

   
7:33 pm on Jan 17, 2005 (gmt 0)

10+ Year Member



I am preparing to build my first PHP web page and plan to grab a variable from the server environment to be used in links.

If these links are written on the pages that are called by the $include function, will the variable created in the wrapper portion be recognized throughout the completed document?

For example, In my base document I have ...

<html>
<head>
<title>Some title</title>
</head>

Many Thanks for any help.
<body>
<?php $my_data = explode("/", $REQUEST_URI,3);?>
<? include("filename.php")?>
</body>
</html>

And in my "filename.php," I have ...

.... some content ....
<a HREF="'http://mydomain.com/' . $my_data [2]">Some Text</a>
.... more content ....

Would that variable be replaced with the value, and did I put the quoation marks in sufficiently and in the right places?

Many thanks.

7:44 pm on Jan 17, 2005 (gmt 0)

10+ Year Member



variables not used in a function will be set.
you should use
getenv("REQUEST_URI") instead of $REQUEST_URI
You must echo the variable to use it :
<?php
echo "<a HREF=\"http://mydomain.com/$my_data[2]\">Some Text</a>";
?>
8:49 pm on Jan 17, 2005 (gmt 0)

10+ Year Member



Many thanks. Many, many thanks.
9:39 pm on Jan 17, 2005 (gmt 0)

10+ Year Member



Another question - should the pages I call with the $include be .htm/html pages, or should they be .txt, or, if they are using the variables, should they be .php, which would mean nesting a .php inside a .php?

Many thanks again.

11:00 pm on Jan 17, 2005 (gmt 0)

10+ Year Member



I like to think of the include statement as a "copy and paste" operation where the code in the filename is being copied directly into the location of the include (keep in mind that the included php code still needs to be inside the <?php?>, otherwise php will treat it as text output/html). So it doesnt matter what the extension of the include file is, it uses the content of the file. However, if you use an extension that apache doesn't parse as php (like .txt for example) someone could view your code. And you could get around that by editing your .htaccess file. Hope this helps...
3:53 am on Jan 18, 2005 (gmt 0)

10+ Year Member




Another question - should the pages I call with the $include be .htm/html pages, or should they be .txt, or, if they are using the variables, should they be .php, which would mean nesting a .php inside a .php?

stick to .PHP, by far the safest

 

Featured Threads

Hot Threads This Week

Hot Threads This Month