JavaScript redirection can be done in 3 ways

Moderator: Lixas

sahilamin
Posts: 9
Joined: Wed Jan 30, 2008 4:17 am

Post by sahilamin »

Lixas wrote:i guess it's the best way to redirect. but sometimes php is just not able to do that. so in this case JS will do the job
I find PHP better than JS when redirecting, though mainly because I don't know JS :D


Lixas
Posts: 750
Joined: Wed Feb 16, 2005 4:21 pm

Post by Lixas »

Flipper3 wrote:In what cases is PHP not able to do that?
[PHP]function redirect($location)// Redirect browser using the header function if it fails, use javascript
{
if(!headers_sent())
{
header("Location: " . $location);
exit;
}
else
{
echo "
<html>
<head>
<title>Redirecting...</title>
<meta http-equiv=\"Content-Type\" content=\"text/html; charset=windows-1257\">
</head>
<body>
<script type='text/javascript'>****.location.href='".$location ."'</script>
You are being redirected to $location. Please <a href=\"$location\">**** this link</a>if you are not being redirected in 5 seconds!
</body></html>";
exit;
}
}[/PHP]

header("Location: " . $location); function may fail if there is send some text to user's browser, it may be a part of page or just a simple single space
Image
Alwahsh
Posts: 292
Joined: Sat Aug 16, 2008 7:05 am

Post by Alwahsh »

thanks for sharing this but I think it's nice to redirect using the cpanel !
Post Reply