Problem with links

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
gstefan
Forum Newbie
Posts: 11
Joined: Thu Aug 21, 2003 2:29 pm

Problem with links

Post by gstefan »

I have the following problem:
(something like this)

menu.php:
<?php
print '<a href="page2/page2.php">Page2</a>';
?>

index.php: //main page
<?php
include ('menu.php');
print '<br>index';
?>

page2.php
<?php
include ('../menu.php');
print '<br>page2';
?>

page2.php is in a subdirectory and when I use the link from the menu this is like this:
page2/page2/page2.php

How can I use menu.php and the link do the same thing always no matter in what subdirectory I am.
Thanks.
m3rajk
DevNet Resident
Posts: 1191
Joined: Mon Jun 02, 2003 3:37 pm

Post by m3rajk »

best way to get absolute path is to prepend what you have with

Code: Select all

$pathstart=getcwd();.'/';
$pathToFIle=$pathstart.'menu.php';
User avatar
gstefan
Forum Newbie
Posts: 11
Joined: Thu Aug 21, 2003 2:29 pm

Post by gstefan »

I've just solved this problem:

menu.php:

<?php
print '<a href="/root/page2/page2.php">Page2</a>';
?>


index.php: //main page
<?php
include ('menu.php');
print '<br>index';
?>

page2.php
<?php
include ('../menu.php');
print '<br>page2';
?>

Thankx for your answers!
m3rajk
DevNet Resident
Posts: 1191
Joined: Mon Jun 02, 2003 3:37 pm

Post by m3rajk »

what happens if you change directories? that's why the getcwd() is handy
User avatar
gstefan
Forum Newbie
Posts: 11
Joined: Thu Aug 21, 2003 2:29 pm

Post by gstefan »

I try with getcwd() as you said. The problem was that when I click on the link the browser ask me if I want to download entirepath\page2\page2.php
I test this on my computer. I have PHPEd from nuSphere and I have also Apache 1.3.27 and use http://localhost/root/site/index.php and the link was http://localhost/root/site/page2/page2.php
m3rajk
DevNet Resident
Posts: 1191
Joined: Mon Jun 02, 2003 3:37 pm

Post by m3rajk »

can we see your full code? this time in [ php] [/ php] around the code (but without the space)?

i think there might be an issue
jason
Site Admin
Posts: 1767
Joined: Thu Apr 18, 2002 3:14 pm
Location: Montreal, CA
Contact:

Post by jason »

This was not a PHP issues. Had nothing (read as 0%) to do with PHP. It is merely the way HTML works.

If your on this page:

http://www.example.com/page2/page2.php

And you click on a link that points to:

page2/page2.php

You go here:

http://www.example.com/page2/page2/page2.php

Now, the most efficient way to solve this problem is not even the way gstefan want's to solve it. Rather, use this:

Code: Select all

<base href="http://www.example.com/">
And then simply create all your links as if you were linking to them from the index page. It's that simple.
Last edited by jason on Sat Sep 20, 2003 5:32 pm, edited 1 time in total.
User avatar
gstefan
Forum Newbie
Posts: 11
Joined: Thu Aug 21, 2003 2:29 pm

Post by gstefan »

I try with
<base href="http://localhost/...">
and it works.
It's really 100% html only.
Thankx.
Post Reply