?
Solved

Go to url with php

Posted on 2009-05-15
5
Medium Priority
?
1,936 Views
Last Modified: 2012-06-21
How do you redirect a website to a different url in php?
0
Comment
Question by:walker6o9
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 9

Expert Comment

by:wellhole
ID: 24398918
Add this at the very top of the page.

<?php
header( 'Location: http://newaddress.com' ) ;
?>
0
 
LVL 1

Author Comment

by:walker6o9
ID: 24399112
Will this work for mobile devices as well?
0
 
LVL 9

Expert Comment

by:wellhole
ID: 24399126
I will have to say it should. This is standard html header.
0
 
LVL 111

Accepted Solution

by:
Ray Paseur earned 2000 total points
ID: 24399159
And a WARNING about using the header() to redirect.  It is a synchronous statement.  It gets executed and your SCRIPT KEEPS RUNNING until the browser redirects, or perhaps longer.

So after you have this:

   header( 'Location: http://newaddress.com' ) ;

You almost certainly want to have this:

   exit;

Better safe than sorry! ~Ray
0
 
LVL 3

Expert Comment

by:markh789
ID: 24401068
header() is used to send a raw HTTP  header, When using the Header(); you MUST have it before any output! Becuase Header

Though, you may only want to use Exit(); if you want to only redirect and nothing else.

From php.net site:
Remember that header() must be called before any actual output is sent, either by normal HTML tags, blank lines in a file, or from PHP. It is a very common error to read code with include(), or require(), functions, or another file access function, and have spaces or empty lines that are output before header() is called. The same problem exists when using a single PHP/HTML file.
(http://au.php.net/manual/en/function.header.php)

ALSO: Google chrome won't redirect without status code send so add header("Status: 200"); before you send Header("Location: blah");

Just another note, if you run this:
header("Location: location1.html");
header("Location: location2.html");

location1.html will be replaced with location2.html, so

header("Location: location1.html");
header("Location: location2.html");
header("Location: location3.html");

location1.htm will be replace with location2.html and location2.html will be replaced with location3.html.

To avoid having to send exit(); useob_end_flush();
This is because the headers aren't sent until the buffer is finished.

Now, a much faster way then sending the 200 status is to use this:
header("Location: http://somesite.com", TRUE ,200);

<?php
header("Status: 200");
header("Location: http://somesite.com");
echo("<p>!! ABOVE YOUR HTML !!</p>");
?>
 
<!---
OR If you only want to redirect, and not show HTML or anything...
 --->
 
<?php
header("Status: 200");
header("Location: http://somesite.com");
Exit(); // Stop's the script.
echo("<p>!! ABOVE YOUR HTML !!</p>");
echo("<p>!! THIS WONT BE EXECUTED BECAUSE OF EXIT(); !!</p>");
?>
 
<!---
OB example
 --->
 
<?php
ob_start();
header("Status: 200");
header("Location: http://somesite.com");
ob_end_flush();
echo("<p>!! ABOVE YOUR HTML !!</p>");
echo("<p>!! THIS WONT BE EXECUTED BECAUSE OF EXIT(); !!</p>");
?>
 
<!---
200 Replace Example
 --->
 
<?php
ob_start();
header("Location: http://somesite.com", TRUE ,200);
ob_end_flush();
echo("<p>!! ABOVE YOUR HTML !!</p>");
echo("<p>!! THIS WONT BE EXECUTED BECAUSE OF EXIT(); !!</p>");
?>

Open in new window

0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

SASS allows you to treat your CSS code in a more OOP way. Let's have a look on how you can structure your code in order for it to be easily maintained and reused.
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
The viewer will receive an overview of the basics of CSS showing inline styles. In the head tags set up your style tags: (CODE) Reference the nav tag and set your properties.: (CODE) Set the reference for the UL element and styles for it to ensu…
Suggested Courses

719 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question