php link forwarding/redirecting
Moderator: General Moderators
-
megahertzman
- Forum Newbie
- Posts: 7
- Joined: Sun Jan 04, 2004 4:59 pm
- Location: C:\ Drive
php link forwarding/redirecting
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?
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
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.
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
-
megahertzman
- Forum Newbie
- Posts: 7
- Joined: Sun Jan 04, 2004 4:59 pm
- Location: C:\ Drive
-
d3ad1ysp0rk
- Forum Donator
- Posts: 1661
- Joined: Mon Oct 20, 2003 8:31 pm
- Location: Maine, USA
you don't place it in the html code
and then link to:
thepage.php?x=file_you_want_to_go_to.html
Code: Select all
<?php
$x = $_GET['x'];
header("location:$x");
?>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
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:
If you specify any actual HTML before the header() functions, it will (usually) not redirect.
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");
}
?>Very simple example to help you get started. Back buttons works here. Change to fit your needs.
[Edit: Wow, calm down everyone stop replying at once! ;) --JAM]
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']);
}
?>-
megahertzman
- Forum Newbie
- Posts: 7
- Joined: Sun Jan 04, 2004 4:59 pm
- Location: C:\ Drive
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:
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
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, 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?
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ї'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?
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:
Same thing with a switch statement:
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");
}
?>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
-
megahertzman
- Forum Newbie
- Posts: 7
- Joined: Sun Jan 04, 2004 4:59 pm
- Location: C:\ Drive
-
megahertzman
- Forum Newbie
- Posts: 7
- Joined: Sun Jan 04, 2004 4:59 pm
- Location: C:\ Drive