Solved

Php redirect

Posted on 2011-09-23
6
307 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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

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

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…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
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…
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…

622 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