Solved

Php redirect

Posted on 2011-09-23
6
292 Views
Last Modified: 2012-05-12
Hello,
In ASP I have a function response.redirect to send the user to another website. How I can send the user to a wesite from the variable $toWebsite ?

Ocrana
0
Comment
Question by:Ocrana
6 Comments
 
LVL 2

Accepted Solution

by:
GRDT earned 500 total points
Comment Utility
use the PHP function header() http://www.php.net/manual/en/function.header.php

Example would be
header("location: " .  $toWebsite );

Open in new window

0
 
LVL 11

Expert Comment

by:maeltar
Comment Utility
Remember you can only use the header redirect funtion as long as NOTHING has been sent to the browser, including white space(s), you will get an error otherwise.

You could always just do it with a bit of a hack and send a standard html redirect to the browser, not perfect but works...

It can be sent outside the head tags, and as I say, it works...

<meta HTTP-EQUIV="REFRESH" content="0; url=http://www.yourdomain.com/index.html">

Open in new window

0
 
LVL 34

Expert Comment

by:Beverley Portlock
Comment Utility
OK - here is the low down on using header()

First - the redirect example given by GRDT will not work as expected. This is because after executing a header("Location: http://....... url"); statement, PHP will continue executing the rest of the script. Executing a header/location does NOT redirect the browser there when the statement is executed. For this reason, always put an exit after the header like so

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

Open in new window

The reason for this behaviour is that header is used to send HTTP directives to the browser and as along as PHP is streaming output to the browser then the browser will do nothing. Once PHP finishes the browser will then execute its headers. Without the exit the script will carry on and if it executes any other header/location statements then the last one executed is where it will go. So to force the browser to execute the header/location you have just output always place an exit immediately after it.

If you omit the exit and the header/location was the last one the script encountered then things will occur as expected, but why take the risk? Code gets altered and added to all the time and if some added another header/location after yours then you will have no end of fun tracking that bug down.

Headers MUST be sent to the browser BEFORE other output. The simplest way to achieve this is to use another PHP function called ob_start. This buffers the non-header output into a holding area and then releases it on execution of ob_end_flush. Your code would be structured like so

<?php
     ob_start();

     ..... code

    ob_end_flush();
?>

Open in new window


See the following manual pages

http://www.php.net/header
http://www.php.net/ob_start
http://www.php.net/ob_end_flush

FYI, any PHP function can be checked in the on-line manual by typing http://www.php.net/function_name where function_name is the function you want to look up. So, as above, for ob_start() I would type http://www.php.net/ob_start

Hope that helps

0
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 2

Expert Comment

by:GRDT
Comment Utility
bportlock - I learned something new today, thank you.  I always assumed that the header function didn't require exit after it based on my experiences, but what you say makes sense. Nice informative post.
0
 
LVL 108

Expert Comment

by:Ray Paseur
Comment Utility
Great post at ID:36591945.

header("Location") is widely misunderstood and the use of die() or exit is exactly the right thing.

The meta-refresh tag is deprecated (since the year 2000!), but it still works perfectly in all modern browsers.
http://www.w3.org/TR/WCAG10-HTML-TECHS/

The header 301 may be useful to you, too.
http://phpprogrammingguide.blogspot.com/2005/11/php-header-301-redirect-moved.html
0
 
LVL 34

Expert Comment

by:Beverley Portlock
Comment Utility
@GRDT - happy to help. header("Location: ......") is an odd one. If you only have one header/location per script then there are never any problems with it. Problems start when you have multiple header/location statements in a script.

We see a lot of questions on here caused by missing exit statements.

@Ray - thanks for the kind comments
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL (http://www.experts-exchange.com/articles/201/Handling-Date-and-Time-in-PHP-and-MySQL.html) several years ago, it seemed like now was a good time to updat…
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.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn the benefit of using external CSS files and the relationship between class and ID selectors. Create your external css file by saving it as style.css then set up your style tags: (CODE) Reference the nav tag and set your prop…

762 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now