php link forwarding/redirecting

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

megahertzman
Forum Newbie
Posts: 7
Joined: Sun Jan 04, 2004 4:59 pm
Location: C:\ Drive

php link forwarding/redirecting

Post by megahertzman »

i have a problem with php link redirecting/forwarding. i have a lot of links on my HTML pages, and since its hard to change all the links individually for each page, i direct all the links to ONE PHP page, and then in turn the PHP page redirects (using meta tags) to teh page its supposed to go to.

one problem is that wen i end up on the page i clicked, i click back but go to that php page, which directs me again forward. wen i click back, i mean to go to the original HTML page before i clicked the link.

whats the solution?
microthick
Forum Regular
Posts: 543
Joined: Wed Sep 24, 2003 2:15 pm
Location: Vancouver, BC

Post by microthick »

You are using a client-side redirect to go to the appropriate link.

Instead, you have to use a server-side redirect.

Using:

header("Location: http://www.someplace.com");

instead of the meta tag redirect should allow for proper usage of the back button.
megahertzman
Forum Newbie
Posts: 7
Joined: Sun Jan 04, 2004 4:59 pm
Location: C:\ Drive

Post by megahertzman »

ok thanks, but i thought that php was server side.

and how do i use the header code u gave me. where do i place it in the html code in teh php file?
megahertzman
Forum Newbie
Posts: 7
Joined: Sun Jan 04, 2004 4:59 pm
Location: C:\ Drive

Post by megahertzman »

in between <head></head>??
d3ad1ysp0rk
Forum Donator
Posts: 1661
Joined: Mon Oct 20, 2003 8:31 pm
Location: Maine, USA

Post by d3ad1ysp0rk »

you don't place it in the html code

Code: Select all

<?php
$x = $_GET['x'];
header("location:$x");
?>
and then link to:
thepage.php?x=file_you_want_to_go_to.html
microthick
Forum Regular
Posts: 543
Joined: Wed Sep 24, 2003 2:15 pm
Location: Vancouver, BC

Post by microthick »

PHP is server-side.

But if you are outputting a meta tag with PHP, the meta tag redirect occurs at the client-side.

You can have:

header("Location: http://www.whatever.com");

anywhere in your code, but you cannot be outputting ANY HTML before it.

Like, your code on your PHP file might look like:

Code: Select all

<?php
if ($_GET["link"] == "files") {
     header("Location: http://mysite.com/files");
} else if ($_GET["link"] == "forums") {
     header("Location: http://mysite.com/forums");
}
?>
If you specify any actual HTML before the header() functions, it will (usually) not redirect.
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Post by JAM »

Very simple example to help you get started. Back buttons works here. Change to fit your needs.

Code: Select all

<?php
// index.php
$links[] = "www.jam.nu";
$links[] = "forums.devnetwork.net/viewtopic.php?t=16297";

foreach ($links as $val) {
    echo '<a href="links.php?where='.$val.'">'.$val.'</a><br />';
} 
?>

Code: Select all

<?php
// links.php
    if (!empty($_GET['where'])) {
        header('Location: http://'.$_GET['where']);
    }
?>
[Edit: Wow, calm down everyone stop replying at once! ;) --JAM]
megahertzman
Forum Newbie
Posts: 7
Joined: Sun Jan 04, 2004 4:59 pm
Location: C:\ Drive

Post by megahertzman »

Okay, I used the code microthick suggested. It works perfect, butwhat if I wanted to ALSO output a little HTML. For instance, if the file is not found, I want it to print out, "sorry, file not found," etc.
User avatar
DuFF
Forum Contributor
Posts: 495
Joined: Tue Jun 24, 2003 7:49 pm
Location: USA

Post by DuFF »

Well since the code automatically redirects it should just result in a 404 error, Page Not Found. If you want to customize your 404 page, take a look at this page: http://www.plinko.net/404/custom.asp

Otherwise, you could make sure the file exists before redirecting them. Something like this:

Code: Select all

<?php
if (!empty($_GET['link'])) 
{
    if(file_exists($_GET['link']))
    {        
        header('Location: http://'.$_GET['link']);
    }
    else
   {
        echo "Sorry, that page does not exist.";
    }
} 
?>
megahertzman
Forum Newbie
Posts: 7
Joined: Sun Jan 04, 2004 4:59 pm
Location: C:\ Drive

Post by megahertzman »

Oh, man. The more I go deeper into this little thing the more confusing and more catches it has. yes, it does lead to a 'Page Not Found' page when no file exists, and that's why I want a custom page with little graphics and all to represent a broken link or something.

I think the code you gave me will not work because 'link' will equal to something like "contactus." And I want "contactus" to refer to contact.html
And your code,

Code: Select all

http://'.$_GET&#1111;'link']
won't work because it will actually be forwarding to http://contactus/", only grabbing the value of 'link'.

I was wondering if I could use the code microthink gave me, and in the end have a final ELSE statement where I embed some fancy HTML representing the 'Page Not Found' page. Possible?
User avatar
DuFF
Forum Contributor
Posts: 495
Joined: Tue Jun 24, 2003 7:49 pm
Location: USA

Post by DuFF »

Yes, you could add an else statement at the very end. That means that if all the elseif fail, then it will direct the page to wherever you want.

Example:

Code: Select all

<?php
if ($_GET["link"] == "files") 
{
     header("Location: http://mysite.com/files");
} 
else if ($_GET["link"] == "forums") 
{
     header("Location: http://mysite.com/forums");
} 
else
{
     header("Location: http://mysite.com/errorpage.php");
}
?>
Same thing with a switch statement:

Code: Select all

<?php
switch ($_GET["link"]) {
   case files:
       header("Location: http://mysite.com/files");
       break;
   case forums:
       header("Location: http://mysite.com/forums");
       break;
   default:
       header("Location: http://mysite.com/errorpage.php");
       break;
}
?>
d3ad1ysp0rk
Forum Donator
Posts: 1661
Joined: Mon Oct 20, 2003 8:31 pm
Location: Maine, USA

Post by d3ad1ysp0rk »

Any differences in speed with switch vs if?
megahertzman
Forum Newbie
Posts: 7
Joined: Sun Jan 04, 2004 4:59 pm
Location: C:\ Drive

Post by megahertzman »

Yeah, are there any differences in speed?
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Post by JAM »

Using IF is faster than SWITCH in the majority of cases, but the difference is in the area of microseconds using 10.000 loops and a switch/if-then-else between 0 and 4.

If that made any sence.
megahertzman
Forum Newbie
Posts: 7
Joined: Sun Jan 04, 2004 4:59 pm
Location: C:\ Drive

Post by megahertzman »

I understood...
JAM wrote:Using IF is faster than SWITCH
Thank you very much. :D
Post Reply