Page 1 of 2

php link forwarding/redirecting

Posted: Sun Jan 04, 2004 4:59 pm
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?

Posted: Sun Jan 04, 2004 5:20 pm
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.

Posted: Sun Jan 04, 2004 5:31 pm
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?

Posted: Sun Jan 04, 2004 5:33 pm
by megahertzman
in between <head></head>??

Posted: Sun Jan 04, 2004 5:56 pm
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

Posted: Sun Jan 04, 2004 5:59 pm
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.

Posted: Sun Jan 04, 2004 5:59 pm
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]

Posted: Sun Jan 04, 2004 6:38 pm
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.

Posted: Sun Jan 04, 2004 7:45 pm
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.";
    }
} 
?>

Posted: Sun Jan 04, 2004 9:07 pm
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?

Posted: Sun Jan 04, 2004 9:21 pm
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;
}
?>

Posted: Sun Jan 04, 2004 9:59 pm
by d3ad1ysp0rk
Any differences in speed with switch vs if?

Posted: Sun Jan 04, 2004 10:11 pm
by megahertzman
Yeah, are there any differences in speed?

Posted: Sun Jan 04, 2004 10:23 pm
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.

Posted: Sun Jan 04, 2004 10:26 pm
by megahertzman
I understood...
JAM wrote:Using IF is faster than SWITCH
Thank you very much. :D