Solved

Php redirect

Posted on 2011-09-23
6
297 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
[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
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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 110

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
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.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

735 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