Php link code

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
fredski
Forum Newbie
Posts: 7
Joined: Tue Sep 13, 2005 10:15 am

Php link code

Post by fredski »

Is ther any code I can put into my php pages that will create a link as follows:-

The domain the link will be on is domain1.com

and for each page within that domain I need it to produce a link for the same page but on a different domain.

for example, say I had a page http://www.domain1.com/foobar.php

I need a link at the bottom of the page that links to http://www.domain2/foobar.php with the text 'old page'

As my site has thousands of pages genrated from a template I can't create a manual link for each one so I need some code that will generate it.

Any help would be fantastic and much appreciated.

Thanks
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

look into using parse_url()
fredski
Forum Newbie
Posts: 7
Joined: Tue Sep 13, 2005 10:15 am

Post by fredski »

Thanks for the reply Feyd,

But that page went right over my head, Im a bit of a noob when it comes to php and only just getting past cutting and pasting and getting my hands dirty.

Could you please expand on it a little?

Cheers
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Example 1. parse_url() example

Code: Select all

$ php -r 'print_r(parse_url("http://username:password@hostname/path?arg=value#anchor"));'
Array
(
    [scheme] => http
    [host] => hostname
    [user] => username
    [pass] => password
    [path] => /path
    [query] => arg=value
    [fragment] => anchor
)

$ php -r 'print_r(parse_url("http://invalid_host..name/"));'
Array
(
    [scheme] => http
    [host] => invalid_host..name
    [path] => /
)
fredski
Forum Newbie
Posts: 7
Joined: Tue Sep 13, 2005 10:15 am

Post by fredski »

A little too complex for my needs.

Code: Select all

<a href="<?php echo "http://www.domain2.com".$_SERVER['PHP_SELF'] ?>">Old Page
</a>
Worked for my needs.

:)
fredski
Forum Newbie
Posts: 7
Joined: Tue Sep 13, 2005 10:15 am

Post by fredski »

Drat
Found a flaw in it.

Although that code works on a lot of my pages. On my store section my pages use mod rewrite so it takes a http://www.domain1.com/product_info.php?products_id=1 page and rewrites it to http://www.domain1.com/product-name.html but the code generated on the page for my link using the above code just states the http://www.domain2.com/product_info.php which isn't what I need.

Anyone know a work around for this?
yakasha
Forum Newbie
Posts: 10
Joined: Wed Aug 03, 2005 1:17 pm
Location: Las Vegas, NV
Contact:

Post by yakasha »

$_SERVER['QUERY_STRING']
fredski
Forum Newbie
Posts: 7
Joined: Tue Sep 13, 2005 10:15 am

Post by fredski »

Thanks for the help so far. :)

Still not quite right though... :(

If I use the following code:-

Code: Select all

<a href=" <?php echo "http://www.domain2.com".$_SERVER['PHP_SELF'].$_SERVER['QUERY_STRING'] ?>" class="emailNav1">Old page</a>
It returns the following link http://www.domain2/catalog/index.phpcPath=1_31

Thats wrong on two accounts

1) there should be a ? between php and cpath like index.php?cPath=1_31

2) Thats the un altered link before modrewrite changes catalog/index.php?cPath=1_31 to /catalog/category_name.html so the link that its generating to the old page is one that the search engines would of had indexed anyway so is irrelevent.

Anyone know of a fix? I need it to link to exactly whats shown in the adress bar with the only difference being the domain name.
Last edited by fredski on Mon Oct 10, 2005 8:04 am, edited 1 time in total.
User avatar
Nathaniel
Forum Contributor
Posts: 396
Joined: Wed Aug 31, 2005 5:58 pm
Location: Arkansas, USA

Post by Nathaniel »

$_SERVER['REQUEST_URI'] might help you out.

- Nathaniel
fredski
Forum Newbie
Posts: 7
Joined: Tue Sep 13, 2005 10:15 am

Post by fredski »

Cheers Nathaniel,

That did the trick just fine :)

The following code gives me exactly whats displayed in the address bar with a different domain name

Code: Select all

<a href=" <?php echo "http://www.domain2.com".$_SERVER['REQUEST_URI'] ?>" class="emailNav1">Old page</a>
Magic, now when the search engine comes to my new pages it will see the link to the old page, follow it and find a 301 back to the page on new domain. That way it will know to take out the old pages from its index and replace with the new ones much better than if I just had a 301 on the old domain without any links on the new.
Post Reply