PDA

View Full Version : Automatically Calling a File for Sublinks?


John_Saunders
11-01-2002, 07:53 PM
I am using includes with PHP call an external .php file that contains sublinks. There's like 6 different sections on my site and each section has different links that show up on the left side of the page.

For example, if you're on the About page you'll see links like, Contact, Resume, Location, etc. Then if you're at the Photos page you'll see links like, Cities, Nature, Friends, etc. I would like to make it so it will pull the correct file up when the visitor is at that section, with only having one call in my script.

Here's the code I'm using:

<?php include ("/home/login/public_html/includes/sub_SECTIONNAMEHERE.php"); ?>

How can I make it so instead of having to go in each file and changing it so it matches each section's links, it will detect which section it's on (maybe through the URL or something) then replace the filename with it and automatically find it in the includes directory? So if I change it to something like:

<?php include ("/home/login/public_html/includes/$section.php"); ?>

The includes directory would have something like:

about.php
photos.php
etc.

Any help would be greatly appreciated!


John

piniyini
11-01-2002, 08:27 PM
you could use $PHP_SELF to get the url of the page.

like so

<?php include ("$PHP_SELF.php"); ?>



so if your page was


http://sitename.com/directory/file.php

you would get

directory/file.php

hope this helps.




Also are you a teacher at a certain collage ?
Private message me for the answer.
Would have messaged you but you had msg's turned off.

John_Saunders
11-01-2002, 08:38 PM
Hi Toseef,

Thanks for your reply. Do you know how I could just get the directory and not the /filename.php?

Here's an example. I would like it so any of the files under any of my folders would call the correct file name by detecting which folder the file is in:

includes
-->about.php
-->photos.php
-->etc.

about
-->contact.php
-->resume.php
-->location.php
-->etc.

photos
-->nature.php
-->friends.php
-->etc


Any idea how to do this?

No, I'm not a college professor.

John_Saunders
11-01-2002, 08:54 PM
I think something close to this would work, but I know it's not correct:

<?php
$requested = getenv('REQUEST_URI');
$file = split("/",$requested);

php include ("/home/login/public_html/includes/$file.php");
?>


How would I call the variable without it detecting the .php after the e in file or will this not mess it up?

Also, is there a way I can tell it what folder to look for to detect which page it should be calling? I need it to be a few folders deep after the domain.com and it will only look at folder 1 instead of folder3. (e.g. http://www.domain.com/folder1/folder2/folder3/index.php).


John

mordred
11-01-2002, 11:56 PM
You can get at the variables passed in a query string by checking the associative array called $_GET. For instance, if you have a URI like index.ph?page=bla.php you access "bla.php" by using $_GET['page']. Does that make sense?

However, make sure that you check values passed by query strings thoroughly for malicious values, to prevent the occasional "../../../etc/passwd" hacking attempt...

John_Saunders
11-02-2002, 12:01 AM
mordred,

I think I get what your saying. You mean to access a page at http://www.domain.com/folder1/folder2/folder3/index.php, I'd need to link to it like: http://www.domain.com/folder1/folder2/folder3/index.php?section=folder3? If so, I would prefer not to do it this way because the ? would hurt the listings in search engines and it looks kinda cheesy having the redundant =folder3 and folder3 directory.

I am however interested in knowing how to check the values like you mentioned for other scripts I would like to make that require this method.

Do you have any idea how I can do this and have it just pull the foldername by telling it where to look?


John