Solved

Php redirect

Posted on 2011-09-23
6
293 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
ID: 36591159
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
ID: 36591421
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
ID: 36591945
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 2

Expert Comment

by:GRDT
ID: 36592488
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
ID: 36592565
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
ID: 36592598
@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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

This article discusses four methods for overlaying images in a container on a web page
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

920 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

15 Experts available now in Live!

Get 1:1 Help Now