3

I have a site in PHP; I want to redirect to external site after running some code; so I want something in the body tag; to redirect it after my code running; something like following example ; what can i do?

<head>
<meta charset="utf-8" />
<link rel="stylesheet" href="styles.css" />
</head>
<body>
<header>
</header>
<?php
    $myffirend=" ";
$allmyffirend=" ";
$user=$_GET['user'];
    // this info save in my db

   redirect here // after save the information

   other code

?>
user3485110
  • 53
  • 1
  • 7

5 Answers5

3

You can inject javascript script with redirection:

echo "<script>window.location = 'http://www.yourdomain.com'</script>";
mareckmareck
  • 1,560
  • 13
  • 18
1

It's not posible to redirect using PHP after printing some code. I suggest to move the code to the start of the file. If this is not possible, you could do it using javascript:

window.location='your_url.php';
Iñaki Soria
  • 869
  • 7
  • 15
1

If you want redirect after page load

if (window.addEventListener) {
   window.addEventListener("load", afterLoadRedirect, false);
} else {
   window.attachEvent('onload', afterLoadRedirect);
}

function afterLoadRedirect(){
  window.location='your url';
}
Nanhe Kumar
  • 15,498
  • 5
  • 79
  • 71
1

You can try the refresh meta tag (seconds, followed by destination):

<meta http-equiv="refresh" content="5; url=http://example.com/" />

The W3C recommends against using it for this purpose but I'm assuming you want to briefly show some sort of message before it changes. If not, stick with changing the Location header which must be done before any output:

header('Location: destination.php');
Community
  • 1
  • 1
Dissident Rage
  • 2,610
  • 1
  • 27
  • 33
0

Use javaScript inside php:

echo '<script> window.location = "http://www.yoururl.com";</script>';
Barif
  • 1,552
  • 3
  • 17
  • 28