Solved

Php redirect

Posted on 2011-09-23
6
295 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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 109

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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
This article discusses four methods for overlaying images in a container on a web page
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. 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.: (CODE)

803 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