15

I want to change the URL through PHP.

Something like window.location.href = "http://www.something.com" in JavaScript.

I want the same thing but in PHP. How can I do it?

Thomas Orlita
  • 1,554
  • 14
  • 28
Nitz
  • 1,690
  • 11
  • 36
  • 56
  • 1
    The PHP Network Function `header`, can also send `$http_response_code` [php.net/manual](http://us3.php.net/manual/en/function.header.php) – Alan Wells Mar 04 '14 at 02:35

5 Answers5

26

You can use the header function for that:

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

Note that such call must be followed by exit as to stop any other code from execution.

In case it errors out with "Headers already sent", refer to this answer

Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
Sarfraz
  • 377,238
  • 77
  • 533
  • 578
5

You could use the header() command:

<?php
  header("Location: http://www.example.com/");
?>
richsage
  • 26,912
  • 8
  • 58
  • 65
2
<?php
header('Location: http://www.example.com/');
?>

Make sure there is nothing above the line outputted, otherwise it will fail.

TimWolla
  • 31,849
  • 8
  • 63
  • 96
Glycerine
  • 7,157
  • 4
  • 39
  • 65
-1

You could use ob_start along with the header function in order to prevent redirection errors like so:

<?php
   ob_start(); // Starts Output Buffering 
   header(/*Your redirection's location*/); // Redirects user to given location.
?>
Mystical
  • 2,505
  • 2
  • 24
  • 43
-2

You could use:

echo '<script> window.location.href = "http://www.something.com"</script>';